Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect: Socket operation on non-socket

I'm new to unix network programming and I have tried to write a program to connect to Google's server. However, I got a error while using the connect() function. (OS: OS X)

Connect error: Socket operation on non-socket

I have worked at it for 4 hours but I could not find out the problem. Here is my code:

#define SERVPORT 80

int main (int argc, char **argv)
{
  int i, sockfd;
  struct hostent *host;
  struct sockaddr_in serv_addr;

  if ( (host = gethostbyname(argv[1])) == NULL) {
    printf("gethostbyname error\n");
    exit(1);
  }

  for (i = 0; host->h_addr_list[i]; i++) {
    if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0) == -1)) {
    printf("socket error\n");
    exit(1);
    }

    bzero(&serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(SERVPORT);
    serv_addr.sin_addr = *( (struct in_addr *)host->h_addr_list[i]);
    const char *ip = inet_ntoa(serv_addr.sin_addr);
    printf("connect to %s\n", ip);

    if (connect(sockfd, (struct sockaddr *) &serv_addr,
            sizeof(struct sockaddr)) == -1) {
      printf("connect error:%s\n", strerror(errno));
      exit(1);
    }

 }
  return 0;
}
like image 469
Nmzzz Avatar asked Feb 06 '13 03:02

Nmzzz


People also ask

What is socket operation on non socket error?

The “Socket operation on a non-socket” error means that, for some reason, the Windows TCP-IP stack has been overloaded, and the socket channel (which is used for communicating with the Internet) has been shut down abruptly.

What is a socket operation?

Definition: A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to. An endpoint is a combination of an IP address and a port number.

How to solve socket error 10038?

Error 10038 means its an Invalid socket. This error usually occurs if any third party service providers are installed over TCP/IP. This can be checked with a utility called sporder.exe. If TCP/IP is not on top of the stack this error is generated, move TCP/IP to the top using sporder.exe and reboot the machine.

What does Socket Error 10060 mean?

10060 is a connection-timeout error that usually appears when the client does not receive a response from the server for a specific command. This error often occurs when you try to connect in PASV mode to a server that prefers PORT for data connections.


1 Answers

I see the problem. It's this line:

if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0) == -1))

The == operator has precedence over the = operator. Look at the way you have the parentheses structured on that expression a bit more carefully to see what I mean. sockfd is getting initialize to "0" as a result of being assigned a boolean expression (socket(...) == -1).

Change the socket initialization to this:

  for (i = 0; host->h_addr_list[i]; i++) 
  {

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1)
    {
        printf("socket error\n");
        exit(1);
    }

Or if you prefer the "assign and compare" on the same line approach, you can probably say this:

if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)

Notice the subtle difference.

like image 89
selbie Avatar answered Oct 09 '22 01:10

selbie