title: Sending multiple messages through a socket in c
body: <p>So in this program I am trying to send the file content and file name from client to server in two steps. I tried using <code>memset()</code> to empty the buffer so as to use it for storing the filename, however then I realised that the filename is being passed into the buffer along with the content, so there is no point of <code>memset()</code>. So, I need to separate the two transmissions I was thinking of closing the buffer for first process and then opening it for the second transmission again, I am not sure how to proceed.</p>
  
  <p>Here's my code:</p>
  
  <p><strong>Server.c</strong></p>
  
  <pre><code>#include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  
  int main(int argc, char *argv[]){
  
      int fd =0, confd = 0;
      struct sockaddr_in serv_addr;
  
     char buff[1025];
     int num;
  
     fd = socket(AF_INET, SOCK_STREAM, 0);
     printf("Socket created\n");
  
     memset(&amp;serv_addr, '0', sizeof(serv_addr));
     memset(buff, '0', sizeof(buff));
  
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
     serv_addr.sin_port = htons(5000);
  
     bind(fd, (struct sockaddr*)&amp;serv_addr, sizeof(serv_addr));
  
     listen(fd, 10);
  
     while(1){
  
         confd = accept(fd, (struct sockaddr*)NULL, NULL);
  
         FILE* fp = fopen( "create.txt", "wb");
  
         int b = recv(confd, buff, 1024, 0);
  
         if(fp!=NULL){
  
             while(b&gt;0){
  
             fwrite(buff, 1, b, fp);
             b = recv(confd, buff, 1024, 0);
             }
         }
  
         else{
             printf("Error!");
         }
  
         printf("Got it :)\n");
         //printf("Buff before: %s\n", buff);
  
         memset(buff, 0, sizeof(buff));
  
         num = read(confd, buff, 1024);
         //printf("Buff after: %s\n", buff);
  
         if(num&lt;0){
             printf("error reading the socket");}
         else{
             printf("File uploaded: %s\n", buff);
         }
  
         //close(confd);
         fclose(fp);
   }
   return 0;
   }
  </code></pre>
  
  <p><strong>Client.c</strong></p>
  
  <pre><code>#include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;arpa/inet.h&gt;
  
  
  int main(int argc, char *argv[]){
  
      int i=0, k=0,num;
      char ip[50];
      char upfile[50];
      char dwfile[50];
  
  
  
      for(i=0; i&lt;argc; i++){
          if(strcmp(argv[i], "-i")==0){
              strcpy(ip, argv[i+1]);
          }
  
          else if(strcmp(argv[i],"-u")==0){
              strcpy(upfile, argv[i+1]);
              k =2;
          }
  
          else if(strcmp(argv[i], "-d")==0){
              strcpy( dwfile, argv[i+1]);
              k=3;    }
  
         /*  else(argv[i] = "-l"){
         }*/
  
      }
      printf("%s %s %s", ip, upfile, dwfile);
  
      int sfd =0, n=0, c;
      char rbuff[1024];
  
      struct sockaddr_in serv_addr;
  
      memset(rbuff, '0', sizeof(rbuff));
      sfd = socket(AF_INET, SOCK_STREAM, 0);
  
      serv_addr.sin_family = AF_INET;
      serv_addr.sin_port = htons(5000);
      serv_addr.sin_addr.s_addr = inet_addr(ip);
  
      c = connect(sfd, (struct sockaddr *)&amp;serv_addr, sizeof(serv_addr));
      if(c==-1){
          perror("Connect");
          return 1;
      }
  
     if (k==2){
         printf("==========This is the upload function");
         FILE *fp = fopen(upfile, "rb");
  
         if(fp == NULL){
             fprintf(stderr, "oh no!");
             return 1;
         }
  
         char sendbuffer[100];
  
         int b = fread(sendbuffer, 1, sizeof(sendbuffer),fp);
         while(b&gt;0){
             send(sfd, sendbuffer, b, 0);
             b = fread(sendbuffer, 1, sizeof(sendbuffer),fp);
         }
  
         num = write(sfd, upfile, strlen(upfile));
         fclose(fp);
  
         printf("Send filename: %s", upfile);
         printf("============End upload!");
     }
  
     return 0;
  
  }
  </code></pre>
  
  <p>Can anyone help ?</p>
  
  <p><strong>EDIT1:</strong></p>
  
  <p>Sending the filename before sending the file contents seems to work, but however doing that is printing a long trail of zeroes. For example: <code>File uploaded: tt.txt000000000000000000000....</code></p>