title: Beej Unix Sockets Error with sending / receiving
body: <p>I have a client.c and server.c program that are meant to do the following: The client sends a message that says &quot;Get\n&quot; to the server, and the server transmits a file back to the client. Once the file transmission is done, the connections are closed.</p>
  <p>So far, everything works up until the point where the server should receive &quot;Get\n&quot;, and I can't seem to figure out why. I have a lot of printf() statements so that I can visually interpret everything that goes on, but that hasn't helped me fix this so I'm really hoping there's someone that can help me here!</p>
  <p>Here is the code for my client.c:</p>
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;ctype.h&gt;          
  #include &lt;arpa/inet.h&gt;
  #include &lt;netdb.h&gt;
  
  #define PORT 43001
  #define LENGTH 512 
  
  int main(int argc, char *argv[])
  {
      int sockFD, newSockFD;
      char buff[LENGTH];
      struct sockaddr_in remote;
  
      //Get Socket Descriptor
      if((sockFD = socket(AF_INET, SOCK_STREAM, 0)) == -1)
      {
          printf(&quot;Failed to get socket descriptor\n&quot;);
          exit(1);
      }
  
      //Fill sockaddr struct
      remote.sin_family = AF_INET;
      remote.sin_port = htons(PORT);
      inet_pton(AF_INET, &quot;127.0.0.1&quot;, &amp;remote.sin_addr);
      bzero(&amp;(remote.sin_zero), 8);
  
      //Connect to server
      if(connect(sockFD, (struct sockaddr *)&amp;remote, sizeof(struct sockaddr)) == -1)
      {
          printf(&quot;Failed to connect()\n&quot;);
          exit(1);
      }
      else
      {
          printf(&quot;Connect()ed!\n&quot;);
      }
  
      //Send message
      char *msg = &quot;Get\n&quot;;
      int len, sentBytes;
      len = strlen(msg);
      printf(&quot;Sending: %s\n&quot;, msg);
      if((sentBytes = send(sockFD, msg, len, 0)) == -1)
      {
          printf(&quot;Error in sending message\n&quot;);
          exit(1);
      }
      else
      {
          printf(&quot;Message sent successfully!\n&quot;);
      }
  
      //Recieve File
      printf(&quot;Getting file from Server\n&quot;);
      char* fr_name = &quot;/in.txt&quot;;
      FILE *fr = fopen(fr_name, &quot;w&quot;);
      if(fr == NULL)
      {
          printf(&quot;File could not be opened\n&quot;);
      }
      else
      {
          bzero(buff, LENGTH);
          int inBlockSize = 0;
          while((inBlockSize = recv(sockFD, buff, LENGTH, 0)) &gt; 0)
          {
              int writeSize = fwrite(buff, sizeof(char), inBlockSize, fr);
              if(writeSize &lt; inBlockSize)
              {
                  printf(&quot;File writing failed\n&quot;);
              }
              bzero(buff, LENGTH);
              if(inBlockSize == 0 || inBlockSize != 512)
              {
                  break;
              }
              if(inBlockSize &lt; 0)
              {
                  printf(&quot;recv() failed due to some error\n&quot;);
              }
          }
           printf(&quot;OK received from server\n&quot;);
          fclose(fr);
      }
      close(sockFD);
      printf(&quot;Connection closed\n&quot;);
      return 0;
  }
  </code></pre>
  <p>When I run it, this is the output I get:</p>
  <pre><code>Connect()ed!
  Sending: Get (Should be Get\n but I understand why this isn't happening)
  
  Message sent successfully!
  Getting file from server
  File could not be opened (This is referring to the .txt file I'm trying to write into what the server sends)
  Connection closed
  </code></pre>
  <p>Then, for the server.c, this is my code:</p>
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;ctype.h&gt;          
  #include &lt;arpa/inet.h&gt;
  #include &lt;netdb.h&gt;
  
  #define PORT 43001 
  #define BACKLOG 5
  #define LENGTH 512 
  
  int main()
  {
      int sockFD, newSockFD;
      int num;
      int sin_size;
      struct sockaddr_in local;
      struct sockaddr_in remote;
      char buff[LENGTH]; 
      char* fileName = &quot;1.txt&quot;;
      char sendBuff[LENGTH];
      FILE *fs;
  
      //Get socket descriptor
      if ((sockFD = socket(AF_INET, SOCK_STREAM, 0)) == -1)
      {
          printf(&quot;Failed to get socket descriptor.\n&quot;);
          exit(1);
      }
      else
      {
          printf(&quot;Got socket descriptor successfully\n&quot;);
      }
  
      //Get client socket struct
      local.sin_family = AF_INET;
      local.sin_port = htons(PORT);
      local.sin_addr.s_addr = INADDR_ANY;
      bzero(&amp;(local.sin_zero), 8); //Deals with the rest of the struct
  
      //Bind
      if(bind(sockFD, (struct sockaddr*)&amp;local, sizeof(struct sockaddr)) == -1)
      {
          printf(&quot;Failed to bind to port\n&quot;);
          exit(1);
      }
      else
      {
          printf(&quot;Binded to port successfully\n&quot;);
      }
  
      //Listen
      if(listen(sockFD, BACKLOG) == -1)
      {
          printf(&quot;listen() failed\n&quot;);
          exit(1);
      }
      else
      {
          printf(&quot;listen()ing to port successfully\n&quot;);
      }
  
      sin_size = sizeof(struct sockaddr_in);
  
      //Accept
      if(newSockFD = accept(sockFD, (struct sockaddr *)&amp;remote, &amp;sin_size) == -1)
      {
          printf(&quot;accept() failed\n&quot;);
          exit(1);
      }
      else
      {
          printf(&quot;accept()'d connection successfully\n&quot;);
      }
  
      //Get Message from Client
      int blkSize = 0;
      if((blkSize = recv(newSockFD, buff, LENGTH, 0)) == -1)
      {
          printf(&quot;recv()ing failed\n&quot;);
          exit(1);
      }
      
      buff[blkSize + 1] = &quot;\0&quot;;
      printf(&quot;Client sent: %s\n&quot;, buff);
  
      //Open File Contents
      printf(&quot;Sending file to client\n&quot;);
      fs = fopen(fileName, &quot;r&quot;);
      if(fs == NULL)
      {
          printf(&quot;File not found\n&quot;);
          exit(1);
      }
  
      bzero(sendBuff, LENGTH);
      int sendingBlkSize;
  
      //Send File
      while((sendingBlkSize = fread(sendBuff, sizeof(char), LENGTH, fs)) &gt; 0)
      {
          if(send(newSockFD, sendBuff, sendingBlkSize, 0) &lt; 0)
          {
              printf(&quot;Failed to send the file\n&quot;);
              exit(1);
          }
          bzero(sendBuff, LENGTH);
      }
      printf(&quot;Sent file to client, closing connection\n&quot;);
      close(newSockFD);
      return 0;
  }
  </code></pre>
  <p>The output for this when ran with the client is:</p>
  <pre><code>Got socket descriptor successfully
  Binded to port successfully
  listen()ing to port successfully
  accept()'ed connection successfully
  recv()ing failed
  </code></pre>
  <p>So the message is sent successfully, but it is not received on the other end. I've done my best to try and troubleshoot this, but everything seems to be right to me. I'm using Beej's Guide To Network Programming to do this with Unix.</p>