9

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;
}
2
  • The socklen_t value for connect should be sizeof(struct sockaddr_in).
    – Brett Hale
    Commented Feb 6, 2013 at 3:54
  • You have broken assignment-inside-if. A debugger would show you that in about 45 seconds. Fix parentheses or, better, move the assignment out of if. Commented Feb 6, 2013 at 4:01

1 Answer 1

30

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.

9
  • 2
    If you're using gcc, this is yet another reason to turn on -Wall, which would give you "warning: suggest parentheses around assignment used as truth value" Commented Feb 6, 2013 at 4:06
  • 2
    Don't you think it's time you knew the operator precedence off by heart?
    – user207421
    Commented Feb 6, 2013 at 6:38
  • 1
    @EJP - I keep a copy of the operator precedence table on a 3x5 card hidden up my sleeve while at work. On the other side of that same card are all the command line parameters for "ls" and the listing to "HelloWorld.c". I'm always forgetting if it's argc or argv as the first parameter to main. :)
    – selbie
    Commented Feb 6, 2013 at 7:07
  • 1
    @JonathonReinhart, thanks for reminding me to insert -Wall in my return to C after 30 years away from it. It found several more problems before I did.
    – Sinc
    Commented Apr 17, 2019 at 19:56
  • 2
    @selbie I keep my K&R (first edition) to hand. The precedence table is on page 49. :) Full bracketing is safest, and makes a vi "%" work well, especially d% to kill a term of the expression.
    – Sinc
    Commented Apr 17, 2019 at 19:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.