jp2code jp2code - 3 years ago 109
C# Question

No connection could be made because the target machine actively refused it - using Socket or TcpClient

Many people have this same problem, but everyone's implementation is different.

I need help with my implementation of it.

void sendUsingTcp()
{
try
{
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(172.16.8.200), 8000);
sock.Connect(endPoint);
// code past this never hits because the line above fails
}
catch (SocketException err)
{
MessageBox.Show(err.Message);
}
}


I have also tried the TCP Client directly with the same error results:

void sendUsingTcp()
{
try
{
using (TcpClient client = new TcpClient())
{
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(172.16.8.200), 8000);
client.Connect(endPoint);
// code past this never hits because the line above fails
}
}
catch (SocketException err)
{
MessageBox.Show(err.Message);
}
}


The IP Address of my PC is
172.16.11.144
.

Answer Source

i) it's quite possible that port 8000 is used for something else. Pick another (large) number and see whether the same thing occurs ii) use the loopback address to connect to your own machine - IPAddress.Loopback

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download