Problems setting a TcpClient local endpoint in C#
Question
Why won't the following code work in C#?
var c1 = new TcpClient(new IPEndPoint(IPAddress.Any, 8787));
var c2 = new TcpClient(new IPEndPoint(IPAddress.Any, 8788));
c1.Connect("localhost", 8788);
I get a "connection cannot be made because the target machine actively refused it". So, the TcpClient constructor doesn't appear to be binding the port, but I tried the Socket.Bind() function with no luck either.
Solution
Two TcpClient
's can't talk to each other. You need one TcpClient
and one TcpListener
.
OTHER TIPS
The problem is that you're not listening for connections. You have to use a TcpListener or similar.
I agree with Sam. You can find an example here.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow