fahimalizain
fahimalizain

Reputation: 844

No connection could be made because the target machine actively refused it - Socket Program

The problem that im facing is given in the Headline. This is where i am :

Pic :

http://img189.imageshack.us/img189/1777/x07w.png

Here is the Code that I use for Connection in Client :

private static void LoopConnect()
    {
        while (!_client.Connected)
        {
            try
            {

                IPAddress ip = IPAddress.Parse("**PUBLIC IP**");
                IPEndPoint remoteEP = new IPEndPoint(ip,61911);
                _client.Connect(remoteEP);
            }
            catch (SocketException e) { Console.WriteLine(e.Message); }
        }

        Console.WriteLine("Connected!");
    }

And this Code that handles Accepting in the Server :

private static void SetupServer()
    {
        Console.WriteLine("Setting up Server.. ");
        _server.Bind(new IPEndPoint(IPAddress.Any, 61911));
        _server.Listen(4);
        Console.WriteLine("Server UP!");
        _server.BeginAccept(new AsyncCallback(AcceptCallBack), null);
    }

    private static void AcceptCallBack(IAsyncResult AR)
    {
        Socket socket = _server.EndAccept(AR);
        _clients.Add(socket);
        socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket);
        _server.BeginAccept(new AsyncCallback(AcceptCallBack), null);
    }

So i think thats all the information regarding the problem i can give ..

*Evrything regarding Malware Protection, Firewalls, Port Forwarding.. is done. *And port is open in netstat -anb

Solutions appreciated :)

EDIT : The client sends request and then the server acknowledges. But i think that doesnt reach the client. For which i should do something else in the router like porting from 'inside'. How is that possible ???

Upvotes: 0

Views: 5380

Answers (1)

Bart Friederichs
Bart Friederichs

Reputation: 33491

What happens is this:

  1. Client tries to connect to public IP.
  2. In doing so, it will connect to the gateway (which is your router)
  3. Your router denies access on that port, because you only opened it from the outside, not the inside.

To test your application, just use your local IP (192.168.1.2). when deployed, you can use the outside address. You could also open the port forwarding from the inside in your router, but that would be outside the scope of this answer.

Upvotes: 1

Related Questions