Create socket server using custom Network IPAddress - c#

Can i create socket server using custom Network IPAddress like (10.1.0.1 or 10.1.0.2)
ie
1.
TcpListener listener = new TcpListener(System.Net.IPAddress.Parse("10.1.1.1"), 8001);
2.
TcpListener listener = new TcpListener(System.Net.IPAddress.Parse("10.1.1.2"), 8002);

Related

Can not connect thrift c# client to python server

I have Thrift client-server application C# client and Python server. All in the same machine-Windows7. Debugging into the Thrift code I saw the client's socket cannot connect to the server's local server. BTW, The same C# client connects to C+ server and python and c++ clients connect to the same python server. Just C#-->Python combination fails.
The problem looks similar to Connecting Python SocketServer with C# Client. I tried to modify the code following the answer in the link above but still, C# socket throws "No connection could be made because the target machine actively refused it 127.0.0.1:9091"
Client(C#):
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[1];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
Socket client = new Socket(ipAddress.AddressFamily,SocketType.Stream,
ProtocolType.Tcp);
TcpClient tcpClient = new TcpClient(AddressFamily.InterNetwork);
tcpClient.Client = client;
tcpClient.Connect(ip, port);
Why ipHostInfo.AddressList[1]? This selection takes IPv4 adapter from
ipconfig. I tries other indices as well. Server(Python, Inside
to Thrift-sever library):
res0 = socket.getaddrinfo(self.host,
self.port,
self._socket_family,
socket.SOCK_STREAM,
0,
socket.AI_PASSIVE |
socket.AI_ADDRCONFIG)
...
self.handle = socket.socket(res[0], res[1])
self.handle.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if hasattr(self.handle, 'settimeout'):
self.handle.settimeout(None)
self.handle.bind(res[4])
self.handle.listen(self._backlog)
client, addr = self.handle.accept()
It never exits from accept in case of C# client
You have too much duplicate code. Try following :
//client
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[1];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(remoteEP);
//Server
IPEndPoint localEP = new IPEndPoint(IPAddress.Any, port);
TcpListener listener = new TcpListener(localEP);
listener.Start();

TcpListener constant error Only one usage of each address

I am trying to use a TcpListener. Every time I try to start the listener I get the error that the Address is already in use. I have looked at netstat and cant see anything on that endpoint(IP Address, Port).
class Program
{
static void Main(string[] args)
{
IPAddress ip = IPAddress.Parse("127.0.0.1");
TcpListener listener = new TcpListener(ip, 58000);
listener.Start();
}
}
When I run that I the error every time.
The error is pretty clear that another process (it's probably because of unfinisted exe of your program) bind the same port which you want to listen. Try to listen different port and see the case;
IPAddress ip = IPAddress.Parse("127.0.0.1");
TcpListener listener = new TcpListener(ip, 58001);
listener.Start();
Also, I strongly suggest you to use TcpView to inspect allocated ports.
You have for sure another process(most probably the same you are trying to run) running in background so you cannot open the port. Try to open and and ensure to close the connections:
public static void Main()
{
TcpListener server=null;
try
{
Int32 port = 58000;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
// TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port);
// Start listening for client requests.
server.Start();
// DO ALL YOUR WORK
}
catch(SocketException e)
{
Console.WriteLine(e.ToString());
}
finally
{
// Stop listening for new clients.
server.Stop();
}
}

C# Using tcp and udp protocols

I need to use udp and tcp connections in my application,the TcpClient/TcpListener would rarely be active,but the udp one would be the main usage.
This is the server code:
static void Main(string[] args)
{
TcpListener listener = new TcpListener(IPAddress.Any, 25655);
listener.Start();
Socket sck = listener.AcceptTcpClient().Client;
UdpClient udpServer = new UdpClient(1100);
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
var data = udpServer.Receive(ref remoteEP);
string result = Encoding.UTF8.GetString(data);
Console.WriteLine(result);
Console.Read();
}
And this is the Client:
static void Main(string[] args)
{
TcpClient client = new TcpClient("127.0.0.1", 25655);
Socket sck = client.Client;
UdpClient udpclient = new UdpClient();
IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1100); // endpoint where server is listening
udpclient.Connect(ep);
byte[] data = UTF8Encoding.UTF8.GetBytes("Hello");
udpclient.Send(data,data.Length);
}
I'm establishing the Tcp connection at first,then i'm trying to connect and send data from the client to the server.
From a breakpoint i add, i can see that the Tcp part works properly,the client finishes the program but in the server,it's hangs on the receiving part var data = udpServer.Receive(ref remoteEP);
like no data arrived..when i remove the tcp code part(the first 2 lines from the server and the client) it works great,shows the result message.
Does anyone know why im unable to get the data from the client?
Thanks in advance.
The main difference between UDP and TCP is that TCP is going to try to resend the message until the server tells the client it has received it. UDP is going to send and forget it even if the packet never reach or the host doesn't exist at all
Here is the flow of your code
Server starts up TCP
Client sends TCP
Server Receives TCP
Client sends UDP (server did not listen yet, packet lost but UDP doesn't care)
Server starts to listen to UDP
Server waiting for UDP to come <--- hang
You would like to do some multithread programming and start both of them at the same time before you tries to send message from the client.
The thing is that listener.AcceptTcpClient() blocks your current thread and UdpClient on server side is not established before Tcp connection created. In fact, your server is waiting for Tcp connection and only after that starting listening of Udp connections, while your client creates 2 connections one by one. My suggestions is - your server starting listening Udp port after the moment client actually sent data. The easiest way to check my suggestions - for client code add Thread.Sleep(1000) before sending data via UDP. In order to make that working you probably need to modify your code not blocking main thread but separate Tcp and Udp in the way similar to this:
static void Main(string[] args)
{
Task.Factory.StartNew(() =>
{
TcpListener listener = new TcpListener(IPAddress.Any, 25655);
listener.Start();
Socket sck = listener.AcceptTcpClient().Client;
// ToDo: further actions related to TCP client
}, TaskCreationOptions.LongRunning);
UdpClient udpServer = new UdpClient(1100);
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
var data = udpServer.Receive(ref remoteEP);
string result = Encoding.UTF8.GetString(data);
Console.WriteLine(result);
Console.Read();
}
client code can probably stay as it is for this example, but for real project I would recommend to separate that as well, as for sure you would like to get back some data from server via Tcp.
What if you first start the UDP client on server side and then establish the TCP connection between client and server?!
Server
static void Main(string[] args)
{
UdpClient udpServer = new UdpClient(1100);
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
TcpListener listener = new TcpListener(IPAddress.Any, 25655);
listener.Start();
Socket sck = listener.AcceptTcpClient().Client;
var data = udpServer.Receive(ref remoteEP);
string result = Encoding.UTF8.GetString(data);
Console.WriteLine(result);
Console.Read();
}
Client
static void Main(string[] args)
{
TcpClient client = new TcpClient();
client.Connect("127.0.0.1", 25655);
UdpClient udpclient = new UdpClient();
IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1100); // endpoint where server is listening
udpclient.Connect(ep);
byte[] data = UTF8Encoding.UTF8.GetBytes("Hello");
udpclient.Send(data, data.Length);
}

Hole punching — where to put external IPs?

I'm trying to implement Hole punching using C# but unfortunately I spent hours to figure out why it doesn't work for me. Here is my problem:
I have a third part server that I can manage all external IPs for the peers on, and of course my router is behind a NAT, and I assume all my peers are also behind NATs.
Say if I managed to figure out all slaves (peers), End points (external ips). Where to put the external ip address of my server which runs on my pc (not the third party one), and where to put the peer ip address?
This is my code:
public void HolePunch(String ServerIp, Int32 Port) // if I put my external ip here I get Exception (An address incompatible with the requested protocol was used)
{
IPEndPoint LocalPt = new IPEndPoint(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0], Port);
UdpClient Client = new UdpClient();
Client.ExclusiveAddressUse = false;
Client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
Client.Client.Bind(LocalPt);
IPEndPoint RemotePt = new IPEndPoint(IPAddress.Parse(ServerIp), Port);
// This Part Sends your local endpoint to the server so if the two peers are on the same nat they can bypass it, you can omit this if you wish to just use the remote endpoint.
byte[] IPBuffer = System.Text.Encoding.UTF8.GetBytes(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0].ToString());
byte[] LengthBuffer = BitConverter.GetBytes(IPBuffer.Length);
byte[] PortBuffer = BitConverter.GetBytes(Port);
byte[] Buffer = new byte[IPBuffer.Length + LengthBuffer.Length + PortBuffer.Length];
LengthBuffer.CopyTo(Buffer,0);
IPBuffer.CopyTo(Buffer, LengthBuffer.Length);
PortBuffer.CopyTo(Buffer, IPBuffer.Length + LengthBuffer.Length);
Client.BeginSend(Buffer, Buffer.Length, RemotePt, new AsyncCallback(SendCallback), Client);
// Wait to receve something
BeginReceive(Client, Port);
// you may want to use a auto or manual ResetEvent here and have the server send back a confirmation, the server should have now stored your local (you sent it) and remote endpoint.
// you now need to work out who you need to connect to then ask the server for there remote and local end point then need to try to connect to the local first then the remote.
// if the server knows who you need to connect to you could just have it send you the endpoints as the confirmation.
// you may also need to keep this open with a keepalive packet untill it is time to connect to the peer or peers.
// once you have the endpoints of the peer you can close this connection unless you need to keep asking the server for other endpoints
Client.Close();
}
public void ConnectToPeer(String PeerIp, Int32 Port)
{
IPEndPoint LocalPt = new IPEndPoint(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0], Port);
UdpClient Client = new UdpClient();
Client.ExclusiveAddressUse = false;
Client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
Client.Client.Bind(LocalPt);
IPEndPoint RemotePt = new IPEndPoint(IPAddress.Parse(PeerIp), Port);
Client.Connect(RemotePt);
//you may want to keep the peer client connections in a list.
BeginReceive(Client, Port);
}
public void SendCallback(IAsyncResult ar)
{
UdpClient Client = (UdpClient)ar.AsyncState;
Client.EndSend(ar);
}
public void BeginReceive(UdpClient Client, Int32 Port)
{
IPEndPoint ListenPt = new IPEndPoint(IPAddress.Any, Port);
Object[] State = new Object[] { Client, ListenPt };
Client.BeginReceive(new AsyncCallback(ReceiveCallback), State);
}
public void ReceiveCallback(IAsyncResult ar)
{
UdpClient Client = (UdpClient)((Object[])ar.AsyncState)[0];
IPEndPoint ListenPt = (IPEndPoint)((Object[])ar.AsyncState)[0];
Byte[] receiveBytes = Client.EndReceive(ar, ref ListenPt);
}
EDIT :
the code works will if I give it my local ip and if my clients in my pc .. but if I supply it an external ip it doesn't work ( the exception in the first line comment )

How do I reuse a TCP endpoint address with .Net sockets

I have some client-server socket code that I want to be able to construct and (re)connect to periodically the same endpoint address: localhost:17999
Here is the server:
// Listen for a connection:
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Loopback, 17999);
Socket listener = new Socket(IPAddress.Loopback.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
listener.Bind(localEndPoint);
listener.Listen(1);
// Accept the connection and send a message:
Socket handler = listener.Accept();
byte[] bytes = new byte[1024];
bytes = Encoding.ASCII.GetBytes("The Message...");
handler.Send(bytes);
// Clean up
handler.Shutdown(SocketShutdown.Both);
handler.Close();
handler.Dispose();
listener.Shutdown(SocketShutdown.Both);
listener.Close();
listener.Dispose();
And here is the client:
byte[] bytes = new byte[1024];
Socket receiver = new Socket(IPAddress.Loopback.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
receiver.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
receiver.Connect(new IPEndPoint(IPAddress.Loopback, 17999));
int num_bytes_received = receiver.Receive(bytes);
string result = Encoding.ASCII.GetString(bytes, 0, num_bytes_received);
receiver.Shutdown(SocketShutdown.Both);
receiver.Close();
receiver.Dispose();
When I create the client and server for the first time, it works fine. However when I create it again, I get an error:
"A request to send or receive data was disallowed because the socket is
not conne cted and (when sending on a datagram socket using a sendto
call) no address was supplied"
I would like to be able to spin up this mechanism arbitrarily whenever I need to with the following order of events:
Launch the server and wait to accept a connection
Launch the client and connect to the server
Accept the client connection at the server
Send a message to the client
Repeat when necessary
How can I do this?
Thx in Advance!
EDIT: Each time I build the client and server objects it is from a different process.
You have two issues:
1) You're closing the listener. Just leave it open.
2) You're setting ReuseAddress on the wrong socket and way too late. Set it on the listening socket before you call bind (since that's when you use the address).
Setting ReuseAddress on a socket you aren't going to bind doesn't do anything. You can remove that from the client.
I tried what Gene suggested and it seems to work:
// Listen for a connection:
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Loopback, 17999);
using (Socket listener = new Socket(IPAddress.Loopback.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
{
listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
listener.Bind(localEndPoint);
listener.Listen(1);
Thread t = new Thread(() =>
{
// Accept the connection and send a message:
using (Socket handler = listener.Accept())
{
byte[] bytes = new byte[1024];
bytes = Encoding.ASCII.GetBytes("The Message...");
handler.Send(bytes);
}
});
t.Start();
t.Join();
}
Thanks, all!

Categories

Resources