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

24,153

Solution 1

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

Solution 2

For me it turned out, my Server was listening for incoming connections only from the same IP. I had to change the code for it to listen from any IP.

Before and Wrong:

IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(172.16.8.200), 8000);

After with problem fixed:

IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 8000);

Solution 3

Have you checked to make sure port 8000 is open on the remote machine? If so, it could be a firewall issue or even something with the network. More details about what's running on the remote machine would help.

Solution 4

Not sure if this was ever answered properly BUT you cannot use a TCPClient to set up a listener - you need a TCPListener class - https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.tcplistener?redirectedfrom=MSDN&view=netframework-4.7.2

Otherwise, you just get an exception when the TCP handshake fails.

Solution 5

Commonly the problem is that the port is closed. Nothing is listening on the other side.

Share:
24,153
jp2code
Author by

jp2code

Software Developer @ IPKeys and jp2code.net Long ago, I left home and joined the United States Marine Corps where I was assigned the Military Occupational Specialty (MOS) of Basic Metal Worker (MOS 1316). I loved building things as a welder, but could not see myself doing this until I reached age 65. After my Honorable Discharge in the early 1990s, I used my G.I. Bill to go to school and received my Bachelor of Science Degree in Physics four years later. Using a love of computers since I played on a Commodore 64 at 10 years old and two (2) C++ electives that I had taken in school, I began working in the computer programming field. The recession following ENRON in 2001 left me working at a remedial job for a little over two years while I brushed up on my skills at a local community college in SQL and first learned what the new .NET Framework was all about. I have had the pleasure of working as a Software Developer since 2003. For a short while, I pulled in a little extra income by running a moonlighting business under the name Joe's Welding. Dangerous working conditions and non-paying customers prompted me to sell my mobile welding equipment about the time my son was born. Today, I am exclusively a Software Developer. My language of choice is C#, and I enjoy working with databases. If you care to find me anywhere else, just do a search on my screen name: jp2code

Updated on November 26, 2020

Comments

  • jp2code
    jp2code over 3 years

    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.