I am writing a simple C# tcp client and server program. The server will eventually be on a different machine but now I am just testing on the same machine. I am getting an exception error "Only one usage of each socket address (protocol/network address/port) is normally permitted" when I create my TcpClient using the ip address. But it works if I use "localhost" instead of ip address. I get the ip address from ipconfig. I also try having the server running on a different machine and my TcpClient gets the same exception when I specify the remote machine's ip address. How do I make the connection using ip address? thanks.
Bel开发者_开发知识库ow is the code where I create the TcpClient
IPAddress ipaddr = IPAddress.Parse("192.168.128.100");
int port = 3000;
int tries = 0;
client = null;
while (tries < 6) // try for 3 seconds
{
try
{
tries++;
IPEndPoint ipEndpt = new IPEndPoint(ipaddr, port);
client = new TcpClient(ipEndpt);
}
catch (Exception ex)
{
client = null;
Thread.Sleep(500);
}
}
when you create tcp client, are you defining a port? if so, you should not. instead, you will connect to the port of the server.
There is probably another service listening on the interface on that port.
You should try
netstat -oan
and search for port 3000. If you found a line in the result the last file is the pid of the process which you can also find in the taskmanager.
TCP 192.168.128.100:3000 0.0.0.0:0 LISTEN 4711
4711 = PID
精彩评论