0

I have a function which initiates the socket connection. I am trying to a server with a name let's say xp. Socket() is working fine. But when it comes to connect, I am getting an error. I tried to get the IP of that server, I got some random IP. I passed the parameters to the connect API. I print the results of these in a log file. I think error lies within the connect(). I am working on Linux Ubuntu. Here is my program for SocketInit(). I can't get the error with that.

I call the SocketInit function as SocketInit(argv[2]); argv[2] has my server name.

short SocketInit(char *xp)
{
      if ( (local_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
        printf("socket creation is unsuccessful check in SocketInit() \n");
        sprintf(log_msg, "create socket descriptor error = %d", errno);
        LogMsg('E', log_msg);
        return(-1);
      }
      else
      {
        printf("socket connection is success\n");
      }

      pos_socket.sin_family = AF_INET;
      pos_socket.sin_port   = htons(port_no);
      pos_socket.sin_addr.s_addr = inet_addr(xp);

      if ( connect( local_socket, (struct sockaddr *) &pos_socket, sizeof(pos_socket) ) < 0 ) {
        sprintf(log_msg, "connect on socket error=%d", errno);
        printf("socket connect api is unsuccessful check in SocketInit() \n");
        LogMsg('E', log_msg);
        return(-1);
      }
      else{
        printf("connect is successful\n");
        return 0;
      }

}

How can I connect to the server. How can I pass the address to the pos_socket.sin_addr.s_addr ? Sometimes I get connect error 110 and 111. But still I can't connect.

6
  • 2
    What is the actual string pointed to by xp? Is it an IP address? A host-name that needs to be resolved to an IP address? And what is the error you get? Please use e.g. strerror to get a printable error message from errno (the value of errno can differ between systems, and remember that you have to check the value of errno before you use any other function which might set it). Commented May 23, 2017 at 17:44
  • Use perror to understand what error it is. Commented May 23, 2017 at 17:44
  • Yes xp is a server, it has an Ip address of 10.254.xx.xx. I tried to print the ip address of my server xp using gethostbyname in my actual program. It is showing exactly the same IP. Commented May 23, 2017 at 17:50
  • So if you call your function like SocketInit("10.254.x.x") it still doesn't work? Have you tried printing errno as a string yet? Commented May 23, 2017 at 18:08
  • Yes, I tried. Same result. @Someprogrammerdude Commented May 23, 2017 at 18:17

1 Answer 1

1

Use perror() to print the human-readable error string when connect() or most other unix-like system calls return an error. But since you told us the value of errno, I looked in errno.h for the meaning, and found:

#define ETIMEDOUT   110 /* Connection timed out */
#define ECONNREFUSED    111 /* Connection refused */

(BTW, you cannot count on errno's being the same from one unix to another which is why you need to use these defines when checking for specific errors. Never hard-code numeric errno values into your code. It worked out for me this time, but it won't necessarily every time).

ECONNREFUSED means that there was a machine listening at the specified IP address, but that no process was listening for connections on the specified port number. Either the remote process is not running, it is not binding or accepting connection properly, or it potentially could be blocked by some sort of firewall.

In any case, this points to a problem with the server.

So, check to make sure your remote process is actually ready to accept the connection. You can use telnet or netcat as a test client to see if other client programs that are known to work are able to connect to your server.

Also, I notice that your variable port_no is not declared, so we have no way of knowing what port you are trying to connect to.. but make sure that this variable is of the correct type and has the correct value for the service you are trying to connect to. If port_no doesn't specify the correct port you will get the same type of error.

Sign up to request clarification or add additional context in comments.

11 Comments

I know the tcp socket errors. Problem is why I'm not getting connect to my server xp?
Read my explanation.. are you sure that the server process is listening on that port? What is the type of service you are trying to connect to, and on what port number.. last but not least what is the operating system of the server?
I think it is listening. How can i know that my server is listening?
I told you.. use telnet. What are you trying to connect to?
@emb-pro It is not listening. That's what ECONNREFUSED means.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.