8

I'm trying to bind the server socket so I can receive and listen for incoming messages from other clients. But I can't bind, it returns an error - Socket bind failed: 99. I read up what does it mean and it says that errno 99 indicates that the socket does not exist? Any ideas? Thanks

  UDP_socketID = socket(AF_INET, SOCK_DGRAM, 0);
  if (UDP_socketID < 0)
  {
    printf("Socket creation failed! Error = %d\n\n", errno);
    exit(0);
  }

  //specify server address, port and IP
  bzero((char *)&serverAddr, sizeof(serverAddr));
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  serverAddr.sin_port = htons(SERV_PORT);
  check = inet_aton(SERVER_IP, &serverAddr.sin_addr);
  if (check == 0)
printf("IP conversion error!\n\n");

  start = bind(UDP_socketID, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
  if (start < 0) {
    printf("Socket bind failed = %d\n", errno);
    exit(0);
}
  else
    printf("Socket bind successful!\n"); 
Broccoli
  • 81
  • 1
  • 1
  • 2

1 Answers1

11

99 is EADDRNOTAVAIL. Which means (from man bind(2)):

A nonexistent interface was requested or the requested address was not local.

Maybe the SERVER_IP is not IP of your host.

SKi
  • 8,007
  • 2
  • 26
  • 57
  • 1
    Use `perror` or `strerror(errno)` to get a human message associated to `errno` – Basile Starynkevitch May 28 '13 at 05:44
  • Oh no! It returned: "Cannot assign requested address." The server IP is on another host, not me. How can I fix this? – Broccoli May 28 '13 at 05:49
  • @Broccoli : Assuming that this is server side code. If you don't care which interface your server listens, then you can remove SERVER_IP stuff and simply bind the server socket with INADDR_ANY. – SKi May 28 '13 at 06:40
  • @Broccoli : And give destination host IP to sendto() function. – SKi May 28 '13 at 06:47