简体   繁体   中英

connect is unsuccessful in my socket programming function in C

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM