6

I'm trying to make a program (client) which kan send a message to a server upon request from user. Stripped down code follows:

Client:

int main(int argc, char **argv) {

  struct sockaddr_in servaddr;
  int sock = socket(AF_INET, SOCK_STREAM, 0);

  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(6789);
  servaddr.sin_addr.s_addr = inet_addr(<ip_address_of_server>);

  while(1) {

    char message[161];
    fgets(message, 161, stdin);

    /* Replacing '\n' with '\0' */
    char *tmp = strchr(message, '\n');
    if (tmp) *tmp = '\0';

    connect(sock, (struct sockaddr *)&servaddr, sizeof(servaddr));
    send(sock, message, strlen(message), 0);
    close(sock);
  }
}

Server:

int main(int argc, char **argv) {

  struct sockaddr_in servaddr;  
  int sock = socket(AF_INET, SOCK_STREAM, 0);

  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(6789);

  bind(sock, (struct sockaddr *)&servaddr, sizeof(servaddr));  
  listen(sock, 5);

  while(1) {
    int clisock = accept(sock, (struct sockaddr *) NULL, NULL);

    if (clisock >= 0) {
      int messageLength = 160;
      char message[messageLength+1];
      int in, index = 0, limit = messageLength;

      while ((in = recv(clisock, &message[index], messageLength, 0)) > 0) {
        index += in;
        limit -= in;
      }

      printf("%s\n", message);
    }

    close(clisock);
  }
}

Now, this works for the first message I send. But then it is not able to make another connection (I get the error message "Bad file descriptor" when trying to connect in the Client program.) Can anyone see what I have misunderstood? Thank you :)

ragnaroh
  • 344
  • 2
  • 3
  • 12
  • Either the accept call or the recv call will be returning an error. Check for it and the cause will become clearer. perror() is very handy for this. (Note: close is probably returning an error here too if accept returns an error and it's good practice to check the bind and listen return values too.) – Flexo Nov 13 '10 at 12:51
  • I get an error as I call connect() in the Client, and an error message "Bad file descriptor" – ragnaroh Nov 13 '10 at 13:42
  • 1
    Keep in mind that TCP is a stream. It does not have messages. You're receiving 160 bytes, that could be several application messages that the client has sent. – nos Nov 13 '10 at 13:57
  • it's sending 160 bytes even if the message is terminated with a '\0', and i use strlen(message) as 3dr argument in send()? – ragnaroh Nov 13 '10 at 14:11
  • I should btw send strlen(message)+1 as 3rd argument. Or else it does not send the '\0' terminator. – ragnaroh Nov 13 '10 at 14:14
  • It's not sending 160 bytes, it's sending strlen(message) bytes. – nos Nov 13 '10 at 14:53

3 Answers3

4

your client programme also does the same mistake, first time you open the socket but after the first connection is done you close the socket, so the next time in the loop the socket descriptor is not valid, you need to re-open the socket but that's missing, please remove the socket call from top and add the below line in the start of while loop

int sock = socket(AF_INET, SOCK_STREAM, 0);
Anjan Biswas
  • 7,746
  • 5
  • 47
  • 77
Yusuf Khan
  • 409
  • 3
  • 9
2
servaddr.sin_addr.s_addr = inet_addr(<ip_address_of_server>);

instead of the above lines in your client code use the following

inet_pton(AF_INET,"<ipofserver>",&servaddr.sin_addr);

perform an error check for the fllowing function also.

Sreevisakh
  • 1,896
  • 2
  • 16
  • 23
2

The problem is that you're closing the listening socket sock, instead of the client socket clisock.

caf
  • 233,326
  • 40
  • 323
  • 462
  • Thanks for your response. I corrected the error. However, it still does not work. It does not run in an infinite loop anymore, but it still does not seem to be able to connect. I've checked all the return values of socket, bind, listen, etc. It seems I get an error as I call connect() in the Client, and an error message "Bad file descriptor". – ragnaroh Nov 13 '10 at 13:40