2

I dont understand whats happening. If I create a socket to anywhere else other than localhost (either "localhost", "127.0.0.1" or the external ip of the machine) it works fine. If I create a socket to an address without something listening in that port i would get a 10060 (timeout) but not a 10061 which makes sense. Why is it that I am getting connection refused when going to localhost. I tried disabling the firewall just in case it was messing things up, but that is not it

I am doing all the WSA initialize stuff before this.

    _socketToServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if(_socketToServer == -1){
    return false;
    }

   p_int = (int*)malloc(sizeof(int));
   *p_int = 1;
   if( (setsockopt(_socketToServer, SOL_SOCKET, SO_REUSEADDR, 
       (char*)p_int, sizeof(int)) == -1 )||
       (setsockopt(_socketToServer, SOL_SOCKET, SO_KEEPALIVE, (char*)p_int,
               sizeof(int)) == -1 ) ){
        free(p_int);
        return false;
   }
   free(p_int);


   struct sockaddr_in my_addr;

   my_addr.sin_family = AF_INET ;
   my_addr.sin_port = htons(_serverPort);

   memset(&(my_addr.sin_zero), 0, 8);
   my_addr.sin_addr.s_addr = inet_addr(_serverIP);


   if( connect( _socketToServer, (struct sockaddr*)&my_addr, sizeof(my_addr)) 
        == SOCKET_ERROR ){
    DWORD error = GetLastError(); //here is where I get the 10061
    return false;
}

Any ideas?

cloudraven
  • 2,484
  • 1
  • 24
  • 49
  • That does sound like a firewall or anti-malware. Are you actually running a program that is listening on the port? – Hans Passant Apr 25 '12 at 00:40
  • what is the port number? do you expect to receive 10060, or you want to actually make connection? – marcinj Apr 25 '12 at 00:55
  • I have tried 9001,9002...9033. None of them worked. I have a program listening, but even if i don't I get the same result. It works from a remote client. Actually just to test, I tried opening the port through putty from within the same machine, and I am able to start a connection! (The server code automatically sends data, and thats what I see in the terminal). I am very confused – cloudraven Apr 25 '12 at 01:56

1 Answers1

4

You are not guaranteed to get a WSAETIMEDOUT error when connecting to a non-listening port on another machine. Any number of different errors can occur. However, a WSAETIMEDOUT error typically only occurs if the socket cannot reach the target machine on the network before connect() times out. If it can reach the target machine, a WSAECONNREFUSED error means the target machine is acknowledging the connect() request and is replying back saying the requested port is not able to accept the connection at that time because either it is not listening or its backlog is full (there is no way to differentiate which). So, when you are connecting to the localhost, you will pretty much always get a WSAECONNREFUSED error when connecting to a non-listening port because you are connecting to the same machine and there is no delay in determining the port's listening status. It has nothing to do with firewalls or anti-malwares. This is just normal behavior.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thanks. It is actually very useful to know that. I was getting confused because I was getting the same error message no matter the port. In the end the problem was solved with a reboot. I guess the server socket didnt really close. I should have tried that early. – cloudraven Apr 25 '12 at 02:03
  • 2
    Only Windows servers cause ECONN/WSAECONNREFUSED when the backlog fills. Unix and Linux servers cause a timeout. – user207421 Apr 25 '12 at 09:57