System.net.Sockets.SocketException (0x80004005): No connection could be made because the target machine actively refused it

0

I have a simple program used for a couple of years to connect from a laptop to a remote server on a specific TCP port. All works fine with the laptop on a LAN or using my O2 phone, but if I use exactly the same program and configuration connecting through an EE account I get the connection refused message above.

However, I can open a telnet session and connect on the port successfully so it is not a firewall issue.

The exception comes from senderSock.Connect(ipEndPoint);

What is Telnet doing differently? are there any other permissions i need to set?

    public static void Connect(string address, int port)
    {
        try
        {
            // Create one SocketPermission for socket access restrictions 
            SocketPermission permission = new SocketPermission(
                NetworkAccess.Connect,    // Connection permission 
                TransportType.Tcp,        // Defines transport types 
                "",                       // Gets the IP addresses 
                SocketPermission.AllPorts // All ports 
                );

            // Ensures the code to have permission to access a Socket 
            permission.Demand();

            // Resolves a host name to an IPHostEntry instance            
            IPHostEntry ipHostInfo = Dns.GetHostEntry(address);

            // Gets first IP address associated with a localhost 
            IPAddress ipAddress = ipHostInfo.AddressList[0];

            // Creates a network endpoint 
            IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, port);

            // Create one Socket object to setup Tcp connection 
            senderSock = new Socket(
                ipAddress.AddressFamily,// Specifies the addressing scheme 
                SocketType.Stream,   // The type of socket  
                ProtocolType.Tcp     // Specifies the protocols  
                );

            senderSock.NoDelay = false;   // Using the Nagle algorithm 

            // Establishes a connection to a remote host 
            senderSock.Connect(ipEndPoint);
            Console.WriteLine("Socket connected to " + senderSock.RemoteEndPoint.ToString());

        }
        catch (Exception exc)
        {
            MessageBox.Show(exc.ToString());
        }
    }
c#
.net
sockets
asked on Stack Overflow Jan 1, 2021 by Chrons42 • edited Jan 3, 2021 by halfer

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0