title: Transmission of audio file using c sockets
body: <p>I am trying to send audio file from one computer to other using socket programming in c. When I send simple string without any framing information such as header or tailer it gets sent perfectly. But when I try to send the same information with some header information like size of the socket_data or packet_no. it doesn't get sent properly. Even the terminal output is SAME on both the machines but the file which gets created is totally different and unplayable. I have used serializing concept to send packet. Am attaching codes. Please comment whats going wrong.</p>
  
  <p><strong>Server.c</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;fcntl.h&gt;
  
  void set_socket(struct sockaddr_in *socket, int type, int host_short, int addr_type)
  {
      socket -&gt; sin_family = type;
      socket -&gt; sin_port = htons(host_short);
      socket -&gt; sin_addr.s_addr = htonl(addr_type);
  }
  
  void serialize(char *buffer, int count, char *data)
  {
      int i=0, j=0;
      char temp1[20];
  
      sprintf(temp1, "%d", count);
      while(temp1[i] != '\0')
      {
          buffer[j++] = temp1[i++];
      }
      buffer[j++]=' ';
  
      for(i=0; data[i] != '\0'; i++)
      {
          buffer[j++] = data[i];
      }
      buffer[j] = '\0';
      printf("BUFFER =%ld\n", sizeof(buffer));
  }
  
  int main()
  {
      int sid = 0, bid = 0, fp;
      char *send_data = (char *)malloc(1024*sizeof(char));
      char temp[1024];
      char *receive_data = (char *)malloc(1024*sizeof(char));
      int fd, count, cnt=0;
      struct sockaddr_in server_socket, client_socket;
      int size = sizeof(client_socket);
  
      if((sid = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
      {
          printf("Connection error..\n");
          exit(1);
      }
  
      set_socket(&amp;server_socket, AF_INET, 6000, INADDR_ANY);
  
      if((bid = bind(sid, (struct sockaddr *)&amp;server_socket, sizeof(struct sockaddr))) == -1)
      {
          printf("Binding error..\n");
          exit(1);
      }
  
      printf("I am waiting for client..\n");
  
      recvfrom(sid, receive_data, 1024, 0,(struct sockaddr *)&amp;client_socket, &amp;size);
      printf("received data is : %s\n", receive_data);
      fd = open(receive_data, O_RDONLY);
      printf("size = %ld\n", sizeof(send_data));
  
      while((count=read(fd, temp, 500)) != 0)
      {
          printf("I am inside the loop : %d\n", cnt++);
          serialize(send_data, count, temp);
          printf("Serialized : %s\n", send_data);
          sendto(sid, send_data, 1024, 0, (struct sockaddr *)&amp;client_socket, size);
      }
      printf("I am outside the loop : %d\n", count);
      strcpy(temp, "ENDOFFILE");
      serialize(send_data, sizeof(temp), temp);
      sendto(sid, send_data, 1024, 0, (struct sockaddr *)&amp;client_socket, size);
  
      fcloseall();
      close(sid);
      close(fd);
      return 0;
  }
  </code></pre>
  
  <p><strong>Client.c</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;fcntl.h&gt;
  
  void set_socket(struct sockaddr_in *socket, int type, int host_short)
  {
      socket -&gt; sin_family = type;
      socket -&gt; sin_port = htons(host_short);
  }
  
  void deserialize(char *buffer, int *size, char *data)
  {
      int i=0, j=0;
      char temp1[20];
  
      while(buffer[i] != ' ')
      {
          temp1[j++] = buffer[i++];
      }
      temp1[j] = '\0';
      printf("\nINT : %s\n", temp1);
      *size = atoi(temp1);
  
      i++;
      j=0;
  
      while(buffer[i] != '\0')
      {
          data[j++] = buffer[i++];
      }
      data[j++] = '\0';
  }
  
  int main()
  {
      int sid = 0, bid = 0, con = 0;
      char *send_data = (char *)malloc(1024*sizeof(char));
      char *receive_data = (char *)malloc(1024*sizeof(char));
      char *temp = (char *)malloc(1024*sizeof(char));
      struct hostent *host;
      struct sockaddr_in server_socket;
      int size = sizeof(server_socket);
  
      if((sid = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
      {
          printf("Connection error at client side..\n");
          exit(1);
      }
  
      set_socket(&amp;server_socket, AF_INET, 6000);
  
      if (inet_aton("127.0.0.1", &amp;server_socket.sin_addr)==0) 
      {
          fprintf(stderr, "inet_aton() failed\n");
          exit(1);
      }
  
      printf("Enter the name of the file you want to see : ");
      scanf("%s", send_data);
      int fd = open("sanket.mp3", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IXUSR);
      sendto(sid, send_data, 1024, 0, (struct sockaddr *)&amp;server_socket, size);
      printf("================= Contents of the File =====================\n");
      while(1)
      {
          int size;
          recvfrom(sid, temp, 1024, 0, (struct sockaddr *)&amp;server_socket, &amp;size);
          printf("Deserialize it : %s\n",temp);
          deserialize(temp, &amp;size, receive_data);
          if(!strcmp(receive_data, "ENDOFFILE"))
          {
              printf("============================================================\n");
              break;
          }
          else
              write(fd, receive_data, size);
      }
      fcloseall();
      close(sid);
  
      return 0;
  }
  </code></pre>
  
  <p>When I checked the size of the sent and received file, sizes are same but the contents are different, thus I am unable to play received audio file.</p>