I am working on a P2P Chat Application using TcpClient and sockets.
I have written the following code to accept tcpclient:
IPAddress[] ip = Dns.GetHostAddresses(Dns.GetHostName());
IPAddress ip_local = Dns.GetHostAddresses(Dns.GetHostName())[0];
// IPAddress ip_local = IPAddress.Parse(ip_local);
TcpListener tcpl = new TcpListener(new IPEndPoint(ip_local, 9277));
while (true)
{
try
{
tcpl.Start();
TcpClient tcpClient = tcpl.AcceptTcpClient();
StateObject state = new StateObject();
state.workSocket = tcpClient.Client;
tcpClient.Client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(OnReceive), state);
}
catch (Exception ex)
{
}
}
The problem is that it picks different network [as I have 1 LAN and 2 VMWARE networks] every time. So the question is how to force it to take the network address of LAN, i.e. a particular network?