title: Sending a file content to a server: Socket
body: <p>My code reads from a file line by line and sends it to a server.</p>
  
  <pre><code>client.c
  
  int main(int argc, char *argv[])
  {
      int sockfd, portno, n;
      struct sockaddr_in serv_addr;
      struct hostent *server;
      FILE* pFile;
      char* line = NULL;
      //char buffer[256];
      char* buffer;
      int len;
  
  
  
      if (argc &lt; 3) {
         fprintf(stderr,"usage %s hostname port\n", argv[0]);
         exit(0);
      }
  
  
  
     pFile = fopen ("myfile.txt","r");
     if (pFile==NULL)
     {
          printf("Error reading temp file\n");
          exit (1);
         }
  
      portno = atoi(argv[2]);
      sockfd = socket(AF_INET, SOCK_STREAM, 0);
  
      if (sockfd &lt; 0) 
          error("ERROR opening socket");
      server = gethostbyname(argv[1]);
      if (server == NULL) {
          fprintf(stderr,"ERROR, no such host\n");
          exit(0);
      }
  
      bzero((char *) &amp;serv_addr, sizeof(serv_addr));
      serv_addr.sin_family = AF_INET;
  
      bcopy((char *)server-&gt;h_addr, 
           (char *)&amp;serv_addr.sin_addr.s_addr,
           server-&gt;h_length);
  
      serv_addr.sin_port = htons(portno);
      if (connect(sockfd,(struct sockaddr *) &amp;serv_addr,sizeof(serv_addr)) &lt; 0) 
      error("ERROR connecting");
  
  
  //loop  
       while (!feof(pFile)) {
      //printf("Please enter the message: ");
      line = readLine(pFile, line);
      len=strlen(line);
      buffer= (char*) malloc((len+1)*sizeof(char));
      bzero(buffer,len);
      memcpy(buffer,line,len+1);
     // fgets(buffer,len,pFile);
      printf("%s\n", buffer);
      n = write(sockfd,buffer,strlen(buffer)+1);
      if (n &lt; 0) 
          error("ERROR writing to socket");
      free(buffer);    
      free(line); 
      }   
  
  
  
      buffer= (char*) malloc(2048*sizeof(char));   
      bzero(buffer,2048);
      n = read(sockfd,buffer,2048);
  
      if (n &lt; 0) 
           error("ERROR reading from socket");
      printf("%s\n",buffer);
  
      free(buffer);
      fclose (pFile);
      close(sockfd);
      return 0;
  }
  </code></pre>
  
  <p>And</p>
  
  <pre><code>    /* A simple server in the internet domain using TCP
     The port number is passed as an argument */
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt; 
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  
  
  
  void error(const char *msg)
  {
      perror(msg);
      exit(1);
  }
  
  
  
  int main(int argc, char *argv[])
  {
  
  
   int sockfd, newsockfd, portno;
   socklen_t clilen;
   char buffer[2048];
   struct sockaddr_in serv_addr, cli_addr;
   int n;
  
  
   if (argc &lt; 2) {
       fprintf(stderr,"ERROR, no port provided\n");
       exit(1);
   }
  
  
   sockfd = socket(AF_INET, SOCK_STREAM, 0);
   if (sockfd &lt; 0) 
      error("ERROR opening socket");
      else
      printf("Socket connected\n");
  
  
   bzero((char *) &amp;serv_addr, sizeof(serv_addr));
   portno = atoi(argv[1]);
   serv_addr.sin_family = AF_INET;
   serv_addr.sin_addr.s_addr = INADDR_ANY;
   serv_addr.sin_port = htons(portno);
   if (bind(sockfd, (struct sockaddr *) &amp;serv_addr,
            sizeof(serv_addr)) &lt; 0) 
            error("ERROR on binding");
   listen(sockfd,5);
   clilen = sizeof(cli_addr);
   newsockfd = accept(sockfd, 
               (struct sockaddr *) &amp;cli_addr, 
               &amp;clilen);
   if (newsockfd &lt; 0) 
        error("ERROR on accept");
  
   printf("Data Receieved by client: \n");
  
    bzero(buffer,2048);
    n=read(newsockfd,buffer,2048);
  
      if (n &lt; 0) error("ERROR reading from socket");
      printf("%s\n",buffer);
  
  
  
    n = write(newsockfd,"Server received the message",18); 
    if (n &lt; 0) 
        error("ERROR writing to socket");
  
  
  
   close(newsockfd);
   close(sockfd);
   return 0; 
  </code></pre>
  
  <p>}</p>
  
  <p>The problem is that the server reveives a few lines and misses a few. Not able to figure out what is wrong. Can some one help?</p>
  
  <pre><code>Client Side
  [gaurav1.k@DELL-BUILD03 Socket]$ gcc -Wall client.c -o client.exe
  [gaurav1.k@DELL-BUILD03 Socket]$ ./client.exe localhost 8000
  I am line number One,am I?
  Here comes line number Two.
  Will you welcome, Line number Three? And I am your Friend.
  I am here, It is me - Line number Four.
  Hello All, I am line number Five, am I? Yes I am.
  It is over, because I am line number six.
  Server received th
  [gaurav1.k@DELL-BUILD03 Socket]$
  
  Server Side:
  [gaurav1.k@DELL-BUILD03 Socket]$ gcc -Wall server.c -o server.exe
  [gaurav1.k@DELL-BUILD03 Socket]$ ./server.exe 8000
  Socket connected
  Data Receieved by client:
  I am line number One,am I?
  [gaurav1.k@DELL-BUILD03 Socket]$
  </code></pre>
  
  <p>The content of file to be read is:</p>
  
  <pre><code>I am line number One,am I?
  Here comes line number Two.
  Will you welcome, Line number Three? And I am your Friend.
  I am here, It is me - Line number Four.
  Hello All, I am line number Five, am I? Yes I am.
  It is over, because I am line number six.
  </code></pre>