Connection actively refused

I'm writing a C# Sockets network application. It worked perfectly while I was testing by connecting to the server on "127.0.0.1", but when I got to the point where I decided that I want to test from other machines and switched the address of the connection to my actual IP address, I started getting the

No connection could be made because the target machine actively refused it -my IP address:port-

exception.

I was looking for solutions in other posts, one I found was to disable my firewall, but that didn't solve the problem.

The ports do match in the client and the server, so that's not the issue.

Another post suggested to run netstat -anb and see if the server is listening on the specified port, and to my surprise the application didn't even show up among the results.

Here's the code for establishing the connection:

The connection method from my class that deals with the connection on the client side:

public bool Connect()
            {
            if (!connected)
                {
                try
                    {
                    sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    sock.Connect(new IPEndPoint(IPAddress.Parse(address), port));
                    connected = true;
                    return true;
                    }
                catch(Exception e)
                    {
                    Console.WriteLine(e.Message);
                    connected = false;
                    return false;
                    }
                }
            return false;
            }

Server side I use this simple listener:

public class Listener
        {
        Socket sock;
        bool listening = false;
        int port;

        public delegate void socketAcceptedHandler(Socket s);
        public event socketAcceptedHandler socketAccepted;

        public Listener(int port)
            {
            this.port = port;
            }

        public void start()
            {
            if (listening)
                {
                return;
                }

            sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            sock.Bind(new IPEndPoint(0, port));
            sock.Listen(0);
            sock.BeginAccept(callback, null);

            listening = true;
            }

        public void stop()
            {
            if (!listening)
                {
                return;
                }

            if (sock != null)
                {
                sock.Close();
                sock.Dispose();
                }

            listening = false;
            }

        public void callback(IAsyncResult result)
            {
            try
                {
                Socket s = sock.EndAccept(result);

                if (socketAccepted != null)
                    {
                    socketAccepted(s);
                    }

                sock.BeginAccept(callback, null);
                }
            catch (Exception e)
                {
                Console.WriteLine("Something went wrong:\n{0}", e.Message);
                }
            }
        }

UPDATE: I checked if the connection is established if I use my local IP address, it is. Must be a router thing.

Upvotes: 0

Views: 1571

Answers (2)

As per the edit: since it works with my local address I assume it must be a router thing, most likely I just need for forward the port. If that doesn't turn out to be the answer I'll repost, but for now I'd like to thank everybody who commented, and treat this as the solution.

Upvotes: 0

Boxcomeaux
Boxcomeaux

Reputation: 63

A firewall could be blocking the port, some other program could be listening on the port or the port is restricted. You can run "netstat -aon" at command prompt to see which process is listening on the port you want to access.

Upvotes: 1

Related Questions