2

I'm trying to connect to a server and always get the same exception. I tried everything, check the port, change the IP by ipconfig and not localhost, check the firewall. when I tried to connect the server by telnet it worked. I send the code to my friend and she succeeded to connect on her computer. I don't know what else I can do. please help!

public void connect(string ip, int port)
    {

        byte[] bytes = new byte[1024];

        try
        {

            // Connect to a Remote server  
            // Get Host IP Address that is used to establish a connection  
            // In this case, we get one IP address of localhost that is IP : 127.0.0.1  
            // If a host has multiple addresses, you will get a list of addresses  
            IPHostEntry host = Dns.GetHostEntry(ip);
            IPAddress ipAddress = host.AddressList[0];
            IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);

            // Create a TCP/IP  socket.    
            sender = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            // Connect the socket to the remote endpoint. Catch any errors.    
            try
            {
                // Connect to Remote EndPoint  
                sender.Connect(remoteEP);

                Console.WriteLine("Socket connected to {0}", sender.RemoteEndPoint.ToString());
            }
            catch (ArgumentNullException ane)
            {
                Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
            }
            // this is the exception I get
            catch (SocketException se)
            {
                Console.WriteLine("SocketException : {0}", se.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine("Unexpected exception : {0}", e.ToString());
            }

        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

the exception :

SocketException : System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (10061): No connection could be made because the target machine actively refused it. [fe80::d540:ed65:27f9:1987%15]:5402
   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
   at System.Net.Sockets.Socket.Connect(IPAddress[] addresses, Int32 port)
--- End of stack trace from previous location where exception was thrown ---
   at System.Net.Sockets.Socket.Connect(IPAddress[] addresses, Int32 port)
   at System.Net.Sockets.Socket.Connect(String host, Int32 port)
   at flight.Model.TelnetClient.connect(String ip, Int32 port)

thanks!

Fred
  • 3,365
  • 4
  • 36
  • 57
aluma
  • 21
  • 2
  • This thread may help you. https://stackoverflow.com/questions/2972600/no-connection-could-be-made-because-the-target-machine-actively-refused-it – Philippe Matray Apr 03 '20 at 07:28
  • Does this answer your question? [No connection could be made because the target machine actively refused it?](https://stackoverflow.com/questions/2972600/no-connection-could-be-made-because-the-target-machine-actively-refused-it) – Cleptus Apr 03 '20 at 07:29

1 Answers1

0

I issued the same problem using this example for creating both server side and client side Socket Programming In C#

This worked perfectly if both the server and the client are on the same machine, but returned the same error if they aren't. After many tries I solved modifyend the row inside the method that creates the server:

IPHostEntry host = Dns.GetHostEntry("localhost");

with the following:

IPHostEntry host = Dns.GetHostEntry("127.0.0.1");

And now it works great in all the cases.

Luc

Dharman
  • 30,962
  • 25
  • 85
  • 135
lucdm
  • 93
  • 1
  • 8