Search code examples
csocketsserversocketrecv

TCPconnection: server doesn't 'understand' that receiving is over


I'm dealing with a problem on the tcp connection I'm building. When, sending datas from client to server, the server seems to "wait"(not so obvious to me why) for another 'recv()' and the client never exits, as it has finished the sending part and waits to 'recv()' an answer from the server. That comes to a kind of deadlock.

The following code is the part where,I think,the problem is.

Server code:

  #define BUFFSIZE 1024;
  char buffer[BUFFSIZE];
  /*...*/
  bzero(buffer,BUFFSIZE);
  while(received > 0) {     /* indicates end of connection */   

      /* recv: Check for more data coming from client*/
          printf("before recv\n");
      if( (received = recv(sock,buffer,BUFFSIZE-1,0) ) < 0){
        perror("Failed to recv bytes from client");
        exit(1);
      } 
      printf("received %d bytes \n", received); 
      sum_bytes += received;

      printf("after recv\n");
      if( strstr(buffer,"End")) {
        printf("strstr: %s\n",buffer);
        break;
      }               
      bzero(buffer,BUFFSIZE);
}
printf("\ntotal bytes received are %d \n", sum_bytes);

Client Code:

for(i=0; i<num_packets; i++){          

          /* Send the packet to the server */
          char packet[packet_size];
          bzero(packet,packet_size);
          if( (sent = send(sockfd,packet,packet_size,0)) != packet_size){
              perror("Client: send() sent a diff num of bytes than expected");
              exit(1);
          }       
          bytes += sent;
    }
    /* send another final packet("End")in order to inform server that sending is over */
    if( (sent = send(sockfd,"End",3, 0)) != 3) {
          perror("Client:send() sent a diff num of bytes than expected");
    }
    bytes += sent;
    printf("total bytes sent are %d \n", bytes);

}

As you can observe, I don't mind about the sending datas but only about the packet sizes. This code works perfect for 1 packet sending... but not for more! :/ if there are more packets than one to be sent, then the client "blocks" (needs ^C to exit) and the server stops on the beginning of the loop, after he has received all the datas (he does one more loop!).

One more query that I have is that the same exact code works fine if I use 'sizeof(BUFFSIZE)' instead of 'BUFFSIZE'. And I am wondering why... :/ Any ideas?

e.g ./client:

END
total bytes sent are 1034 
/* press ^C */

./server:

before recv
received 358 bytes
after recv
before recv
received 676 bytes
after recv
bef recv /* stucks here*/

(...the following comes when i press ctr-c on client...)
received 0 bytes
after recv

total bytes received are 1034 /** right amount of bytes received!! **/


Solution

  • PROBLEM SOLVED!!:

    I send data packets with so many datas as the packet-size! Before, I sended for example "abcde" with packet-size=1024. With using memset in the client, problem solved! Thanks for helping!

    Correct code (altered only in client):

    for(i=0; i<num_packets; i++){          
              /* Send the packet to the server */
              char packet[packet_size];
              memset(packet,'-',packet_size);
              if( (sent = send(sockfd,packet,packet_size,0)) != packet_size){
                  perror("Client: send() sent a different number of bytes than expected");
                  exit(1);
              }       
              bytes += sent;
        }