title: Socket Programming -- recv() is not receiving data correctly
body: <p>I have looked at the similar threads but can't seem to find anything that could solve my problem. </p>
  
  <p>I am programming a server that could send an image(jpg) file from the path sent to it from the client. I am using send/recv functions in C for that. </p>
  
  <p>I read files one chunk of data at a time and send it to the client that receives the contents and writes them at some location to build the file.</p>
  
  <p>Problem is 'recv' doesn't receive the number of bytes sent by the 'send'. </p>
  
  <p>As part of debugging, I have tried different buffer sizes and '128' buffer size doesn't give me any problem, and the file is successfully transferred and built.</p>
  
  <p>However, for '32' and '64' bit buffers, 'recv' receives '32' bit or '64' bit data at the last chunk, even though the data sent by the server is less than either '32' bit or '64' bit. And, for '256', '512', '1024' so on, 'recv' returns ONLY '128' bits at EXACTLY one of the responses, even though the server sends complete chunk, i.e. '256' or'512', depending on the buffer size.</p>
  
  <p>I'll appreciate any advise for debugging. Following code is for the relevant parts only, but I can provide more, should someone requires it.</p>
  
  <p>//Client Code</p>
  
  <pre><code>#define BUFFER_SIZE 4096
  
  #define   HEADER_LEN 512
  
  const char * const scheme = "GETFILE";
  const char* const method = "GET";
  const char * const end_marker = "\\r\\n\\r\\n";
  
  struct gfcrequest_t
  {
      int filelen;
      char cport[12];
      char servIP[50];
      gfstatus_t ret_status;
      char spath[1024];
      int tot_bytes;
      char filecontent[BUFFER_SIZE];
  
      void (*fl_handler)(void *, size_t, void *);
      void * fDesc;
  
      void (*head_handler)(void *, size_t, void *);
      void * headarg;
  
  };
  
  static pthread_mutex_t counter_mutex;
  
  gfcrequest_t *gfc;
  
  // get sockaddr, IPv4 or IPv6:
  void *get_in_addr(struct sockaddr *sa)
  {
      if (sa-&gt;sa_family == AF_INET)
      {
          return &amp;(((struct sockaddr_in*)sa)-&gt;sin_addr);
      }
      return &amp;(((struct sockaddr_in6*)sa)-&gt;sin6_addr);
  }
  
  static char *stringFromError(gfstatus_t stat)
  {
      static const char *strings[] = {"GF_OK", "GF_FILE_NOT_FOUND", "GF_ERROR", "GF_INVALID"};
  
      return strings[stat];
  }
  
  
  int getFileRequestHeader(char * req_header)
  {
      return snprintf(req_header,HEADER_LEN, "%s%s%s%s", scheme, method,gfc-&gt;spath, end_marker);
     // return snprintf(req_header,HEADER_LEN, "%s%s%s%s", scheme, method,"/courses/ud923/filecorpus/yellowstone.jpg", end_marker);
  }
  
  
  gfcrequest_t *gfc_create()
  {
  
      gfc = (gfcrequest_t*)malloc(1* sizeof(gfcrequest_t));
  
      return gfc;
  }
  
  void gfc_set_server(gfcrequest_t *gfr, char* server)
  {
      strcpy(gfr-&gt;servIP, server);
  }
  
  void gfc_set_path(gfcrequest_t *gfr, char* path)
  {
      strcpy(gfr-&gt;spath, path);
  }
  
  void gfc_set_port(gfcrequest_t *gfr, unsigned short port)
  {
      snprintf(gfr-&gt;cport,12, "%u",port);
  }
  
  void gfc_set_headerfunc(gfcrequest_t *gfr, void (*headerfunc)(void*, size_t, void *))
  {
  
      gfr-&gt;head_handler = headerfunc;
  
  }
  
  void gfc_set_headerarg(gfcrequest_t *gfr, void *headerarg)
  {
  /*have to change this...*/
      gfr-&gt;headarg = headerarg;
  
  }
  
  int isEndMarker(char *iheader, int start)
  {
      char *marker = "\\r\\n\\r\\n";
      int i = 0; int ind=0;
  
  
      while (ind &lt;= 7)
      {
          if (iheader[start++] == marker[ind++])
          {
              continue;
          }
  
          return 0;
      }
  
      return 1;
  
  }
  
  int getFileLen(char *resp_header, gfcrequest_t *gfr)
  {
      char scheme[8];
      char status[4];
      int istatus;
      char filelen[12];
  
      int contentlen = 0;
  
      int fileindex = 0;
      char end_marker[12];
      int fexit=0;
      int end=0;
  
      sscanf(resp_header, "%7s%3s", scheme, status);
  
  
      istatus = atoi(status);
  
      if (istatus == 200)
      {
          gfr-&gt;ret_status = GF_OK;
      }
      else if (istatus == 400)
      {
          gfr-&gt;ret_status = GF_FILE_NOT_FOUND;
      }
      else if (istatus == 500)
      {
          gfr-&gt;ret_status = GF_ERROR;
  
      }
  
      if (!strcmp(scheme, "GETFILE") &amp;&amp; (istatus == 200 || istatus == 400 || istatus == 500))
      {
          int index = 10;
          while(1)
          {
  
              if (resp_header[index] == '\\')
              {
                  end = isEndMarker(resp_header, index);
              }
  
              if (end)
                  break;
  
              filelen[fileindex++] = resp_header[index++];
  
          }
  
          filelen[fileindex] = '\0';
      }
  
      int head_len = strlen(scheme) + strlen(status) + strlen(filelen) + 8;
  
      return atoi(filelen);
  
  }
  
  void gfc_set_writefunc(gfcrequest_t *gfr, void (*writefunc)(void*, size_t, void *))
  {
  
      gfr-&gt;fl_handler = writefunc;
  
  }
  
  void gfc_set_writearg(gfcrequest_t *gfr, void *writearg)
  {
      gfr-&gt;fDesc = writearg;
  }
  
  int gfc_perform(gfcrequest_t *gfr){
  
      struct addrinfo hints, *servinfo, *p;
  
      int sockfd, rv, totalBytesRcvd;
      char req_header[HEADER_LEN];
  
      int bytesRcvd;
  
      int header_len;
  
      char s[INET6_ADDRSTRLEN];
  
      memset(&amp;hints, 0, sizeof(hints));
      memset(req_header,0,sizeof(req_header));
  
      hints.ai_family = AF_UNSPEC;
      hints.ai_socktype = SOCK_STREAM;
      hints.ai_flags = AI_PASSIVE; //use my IP
  
      if ((rv = getaddrinfo(NULL, gfr-&gt;cport, &amp;hints, &amp;servinfo)) != 0)
      {
          fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
          return 1;
      }
  
      // loop through all the results and connect to the first we can
  
      for(p = servinfo; p != NULL; p = p-&gt;ai_next)
      {
          if ((sockfd = socket(p-&gt;ai_family, p-&gt;ai_socktype, p-&gt;ai_protocol)) == -1)
          {
              perror("client: socket");
              continue;
          }
  
          if (connect(sockfd, p-&gt;ai_addr, p-&gt;ai_addrlen) == -1)
          {
              close(sockfd);
              perror("client: connect");
              continue;
          }
  
          break;
      }
  
      if (p == NULL)
      {
          fprintf(stderr, "client: failed to connect\n");
          return 2;
      }
  
      //printf("connected...\n");
  
      inet_ntop(p-&gt;ai_family, get_in_addr((struct sockaddr *)p-&gt;ai_addr), s, sizeof(s));
  
  //Ahsan
     // printf("Before getFileRequestHeader...\n");
      header_len = getFileRequestHeader(req_header);
  
      //printf("Header Description:%s, Header Len: %u\n", req_header, header_len);
  
      if (send(sockfd, req_header, header_len, 0) != header_len)
          perror("send() sent a different number of bytes than expected");
  
      if ((bytesRcvd = recv(sockfd, gfr-&gt;filecontent, BUFFER_SIZE, 0)) &lt;= 0)
          perror("recv() failed or connection closed prematurely");
  
      //printf("Header Received: %s\n", gfr-&gt;filecontent);
  
      gfr-&gt;filelen = getFileLen(gfr-&gt;filecontent, gfr);
  
      //printf("File Length: %d\n", gfr-&gt;filelen);
  
      /* Receive the same string back from the server */
      int req_no=1;
      gfr-&gt;tot_bytes = 0;
  
      while ( 1 )
      {
          printf("Request: %d ", req_no++);
          ssize_t nb = recv( sockfd, gfr-&gt;filecontent, BUFFER_SIZE, 0 );
          if ( nb == -1 ) err( "recv failed" );
          if ( nb == 0 ) {printf("zero bytes received...breaking");break;} /* got end-of-stream */
  
          gfr-&gt;fl_handler(gfr-&gt;filecontent, nb, gfr-&gt;fDesc);
          gfr-&gt;tot_bytes += nb;
  
          printf("Received Bytes: %zd Total Received Bytes: %d\n", nb, gfr-&gt;tot_bytes);
      }
  
      return 0;
  }
  
  
  /*
   * Returns the string associated with the input status
   */
  
  char* gfc_strstatus(gfstatus_t status)
  {
      return stringFromError(status);
  }
  
  
  gfstatus_t gfc_get_status(gfcrequest_t *gfr){
      return gfr-&gt;ret_status;
  }
  
  size_t gfc_get_filelen(gfcrequest_t *gfr)
  {
      return gfr-&gt;filelen;
  }
  
  size_t gfc_get_bytesreceived(gfcrequest_t *gfr)
  {
  
      return gfr-&gt;tot_bytes;
  
  }
  
  void gfc_cleanup(gfcrequest_t *gfr)
  {
      free(gfr);
      gfr=NULL;
  }
  
  void gfc_global_init()
  {
  ;
  //  pthread_mutex_lock(&amp;counter_mutex);
  //
  //  gfc = (gfcrequest_t*)malloc(1* sizeof(gfcrequest_t));
  //
  //  pthread_mutex_unlock(&amp;counter_mutex);
  
  
  }
  
  void gfc_global_cleanup()
  {
  ;
  //    pthread_mutex_lock(&amp;counter_mutex);
  //
  //    free(gfc);
  //
  //    pthread_mutex_unlock(&amp;counter_mutex);
  
  }
  </code></pre>
  
  <p>//Server Code</p>
  
  <pre><code>struct gfcontext_t
  {
      int sockfd;
      int clntSock;
  };
  
  struct gfserver_t
  {
      char port[12];
      unsigned short max_npending;
      char fpath[256];
      ssize_t (*fp_handler)(gfcontext_t *ctx, char *, void*);
      int *handler_arg;
  };
  
  
  /*Variable decalation*/
  
  
  static gfserver_t *gfserv;
  
  static gfcontext_t *gfcontext;
  
  int isEndMarker(char *iheader, int start)
  {
      char *marker = "\\r\\n\\r\\n";
      int i = 0; int ind=0;
  
  
      while (ind &lt;= 7)
      {
          if (iheader[start++] == marker[ind++])
          {
              //printf("Header Char:%c Marker:%c\n", iheader[start], marker[ind]);
              //start++;
              continue;
          }
  
          return 0;
      }
      //printf("Its a marker!!!\n");
      return 1;
  
  }
  
  int parseHeader(char *iheader, gfserver_t *gfs, int hlen)
  {
  //"GETFILEGET/courses/ud923/filecorpus/road.jpg\r\n\r\n"
  
      char scheme[8];
      char method[4];
  //    char path[256];
      int pathindex = 0;
      char end_marker[12];
  
      int end = 0;
      int fexit=0;
  
      sscanf(iheader, "%7s%3s", scheme, method);
  
      int i=10;
          if (iheader[i] == '/')
          {
  //            printf("Path has started...\n");
              if (!strcmp(scheme, "GETFILE") &amp;&amp; !strcmp(method, "GET"))
              {
  
                  while(1)
                  {
  
                      if (iheader[i] == '\\')
                      {
                          end = isEndMarker(iheader, i);
                      }
  
                      if (end)
                          break;
  
                      gfs-&gt;fpath[pathindex++] = iheader[i++];
  
                  }
  
                  gfs-&gt;fpath[pathindex] = '\0';
              }
          }
  
  
      printf("Scheme: %s Method:%s Path:%s\n", scheme, method, gfs-&gt;fpath);
  
      return 0;
  }
  
  
  void *get_in_addr(struct sockaddr *sa)
  {
  
      if (sa-&gt;sa_family == AF_INET)
      {
          return &amp;(((struct sockaddr_in*)sa)-&gt;sin_addr);
      }
  
      return &amp;(((struct sockaddr_in6*)sa)-&gt;sin6_addr);
  }
  
  
  
  ssize_t gfs_sendheader(gfcontext_t *ctx, gfstatus_t stat, size_t file_len)
  {
  
      char resp_header[MAX_REQUEST_LEN];
      char end_marker[12] = "\\r\\n\\r\\n";
  
      sprintf(resp_header, "GETFILE%d%zd%s",stat, file_len, end_marker);
  
      printf("Response: %s\n", resp_header);
  
      if (send(ctx-&gt;clntSock, resp_header, MAX_REQUEST_LEN, 0) != MAX_REQUEST_LEN)
          perror("send() failed");
  
      return 0;
  }
  
  ssize_t gfs_send(gfcontext_t *ctx, void *data, size_t len)
  {
      size_t total = 0;
      size_t bytesLeft = len;
      size_t n;
  
      int debug_req=1;
  
      while (total &lt; len)
      {
           n = send(ctx-&gt;clntSock, data+total, bytesLeft, 0);
           if (n == -1) { printf("Nothing to send...\n"); break; }
  
           fprintf(stderr, "Tries: %d Bytes Sent: %zu\n", debug_req++, n);
  
           total += n;
           bytesLeft -= n;
      }
  
     // if ( shutdown( ctx-&gt;clntSock, SHUT_WR ) == -1 ) err( "socket shutdown failed" );
  
      return total;
  }
  
  void gfs_abort(gfcontext_t *ctx){
  
      close(ctx-&gt;clntSock);
      close(ctx-&gt;sockfd);
      free(ctx);
      free(gfserv);
  
      perror("aborting...");
  
      exit(1);
  }
  
  gfserver_t* gfserver_create()
  {
  
      gfserv = (gfserver_t*) malloc(1 * sizeof(gfserver_t));
      gfcontext = (gfcontext_t*) malloc(1*sizeof(gfcontext_t));
  
      return gfserv;
  }
  
  void gfserver_set_port(gfserver_t *gfs, unsigned short port)
  {
      //set port number in gfs structure
      snprintf(gfs-&gt;port,12, "%u",port);
  }
  
  void gfserver_set_maxpending(gfserver_t *gfs, int max_npending)
  {
      //set maxpending connections
      gfs-&gt;max_npending = max_npending;
  }
  
  void gfserver_set_handler(gfserver_t *gfs, ssize_t (*handler)(gfcontext_t *, char *, void*))
  {
      gfs-&gt;fp_handler = handler;
  }
  
  void gfserver_set_handlerarg(gfserver_t *gfs, void* arg)
  {
      gfs-&gt;handler_arg = (int *)arg;
  }
  
  void gfserver_serve(gfserver_t *gfs)
  {
  
      struct addrinfo hints, *servinfo, *p;
      struct sockaddr_storage clntAddr; //connectors address information
      socklen_t clntSize;
      int yes = 1;
      char s[INET6_ADDRSTRLEN];
      int rv;
  
      int rcvMsg = 0;
      char recvBuff[MAX_REQUEST_LEN];
      char *req_path;
  
      memset(&amp;hints, 0, sizeof(hints));
  
      hints.ai_family = AF_UNSPEC;
      hints.ai_socktype = SOCK_STREAM; //using stream socket instead of datagrams
      hints.ai_flags = AI_PASSIVE; //use my IP
  
      if ((rv = getaddrinfo(NULL, gfs-&gt;port, &amp;hints, &amp;servinfo)) != 0)
      {
          fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
          return 1;
      }
  
      // loop through all the results and bind to the first we can
      for(p = servinfo; p != NULL; p = p-&gt;ai_next)
      {
  
          if ((gfcontext-&gt;sockfd = socket(p-&gt;ai_family, p-&gt;ai_socktype, p-&gt;ai_protocol)) == -1)
          {
              perror("server: socket");
              continue;
          }
  
          //get rid of 'address already in use' error.
          if (setsockopt(gfcontext-&gt;sockfd, SOL_SOCKET, SO_REUSEADDR, &amp;yes, sizeof(int)) == -1)
          {
              perror("setsockopt");
              exit(1);
          }
  
          if (bind(gfcontext-&gt;sockfd, p-&gt;ai_addr, p-&gt;ai_addrlen) == -1)
          {
              close(gfcontext-&gt;sockfd);
              perror("server: bind");
              continue;
          }
  
          break;
      }
  
      if (p == NULL)
      {
          fprintf(stderr, "server: failed to bind.\n");
          return 2;
      }
  
      freeaddrinfo(servinfo); // no need of servinfo structure anymore
  
      if (listen(gfcontext-&gt;sockfd, gfs-&gt;max_npending) == -1)
      {
          perror("listen");
          exit(1);
      }
  
  
      //printf("server: waiting for connetions...\n");
  
      while(1)
      {
          clntSize = sizeof(clntAddr);
          gfcontext-&gt;clntSock = accept(gfcontext-&gt;sockfd, (struct sockaddr *)&amp;clntAddr, &amp;clntSize);
  
          if (gfcontext-&gt;clntSock == -1)
          {
              perror("accept");
              continue;
          }
  
          inet_ntop(clntAddr.ss_family, get_in_addr((struct sockaddr *)&amp;clntAddr), s, sizeof(s));
  
          //printf("server: got connection from %s\n", s);
  
          if (!fork())
          { // this is the child process
              if ((rcvMsg = recv(gfcontext-&gt;clntSock, recvBuff, MAX_REQUEST_LEN, 0)) &lt; 0)
              {
                  perror("recv() failed");
                  exit(1);
              }
  
              /*Still to parse received request...*/
              //printf("Recd Header: %s, Recd %d bytes\n",recvBuff, rcvMsg);
  
              /*Parse the received header...*/
  
              int len = parseHeader(recvBuff, gfs, rcvMsg);
  
              //printf("Requested Path: %s\n", gfs-&gt;fpath);
  
              if (gfs-&gt;fp_handler(gfcontext, gfs-&gt;fpath, NULL) &lt; 0)
              {
                  printf("some problem...\n");
  
              }
  
              if ( shutdown( gfcontext-&gt;clntSock, SHUT_WR ) == -1 ) err( "socket shutdown failed" );
              //close(gfcontext-&gt;clntSock);
          }
  
      }
  
  }
  
  //Server gf_send is being called from following function handler function:
  
  Handler:
  ssize_t handler_get(gfcontext_t *ctx, char *path, void* arg){
      int fildes;
      size_t file_len, bytes_transferred;
      ssize_t read_len, write_len;
      char buffer[BUFFER_SIZE];
  
      printf("Path: %s\n", path);
  
      if( 0 &gt; (fildes = content_get(path)))
          return gfs_sendheader(ctx, GF_FILE_NOT_FOUND, 0);
  
      /* Calculating the file size */
      file_len = lseek(fildes, 0, SEEK_END);
  
      gfs_sendheader(ctx, GF_OK, file_len);
  
      /* Sending the file contents chunk by chunk. */
      int req=1;
      bytes_transferred = 0;
      while(bytes_transferred &lt; file_len){
          read_len = pread(fildes, buffer, BUFFER_SIZE, bytes_transferred);
          if (read_len &lt;= 0){
              fprintf(stderr, "handle_with_file read error, %zd, %zu, %zu", read_len, bytes_transferred, file_len );
              gfs_abort(ctx);
              return -1;
          }
  
          printf("Request No: %d ", req++);
          write_len = gfs_send(ctx, buffer, read_len);
          if (write_len != read_len){
              fprintf(stderr, "handle_with_file write error");
              gfs_abort(ctx);
              return -1;
          }
  
          bytes_transferred += write_len;
      }
  
      printf("Total Bytes sent to client: %zu\n", bytes_transferred);
  
      return bytes_transferred;
  }
  </code></pre>