title: Unix Client and Server Program - Very Odd Client and Server Socket Bug
body: <p>I am currently making a client and server program in the Unix Environment. I have made it so the client is able to upload the contents of a file to the client. I am now in the process of adding options and error handlers to the server example the client must enter the file name. To do this i wanted to send a message saying OK if all the options checked out however if i do this it seems to cause my file reading and sending to go Crazy and I have no idea why.</p>
  
  <p>I have uploaded the functions for doing this</p>
  
  <p>Client Code</p>
  
  <pre><code>int putFile (char path[256], int fd)
  {
      char mystring[1000];
      char buffer[100];
      int i , n;
      FILE * pFile;
  
  
      n = read(fd,buffer,100);
      printf("%s", buffer);
  
      if (strcmp(buffer, "OK") == 0)
      {
          pFile = fopen(path, "r");
          if(pFile != NULL)
          {
              while(fgets(mystring, sizeof(mystring), pFile) != NULL)
              {
                  //fputs(mystring, fd);
                  write(fd,mystring,strlen(mystring));
              }
          }
          else
          {
              printf("Invalid File or Address \n");
          }
  
          fclose(pFile);
      }
      else
      {
          printf("%s \n", buffer);
      }
  }
  </code></pre>
  
  <p>Server Code for reading the socket </p>
  
  <pre><code>int putRequest(int fd, char buf[], char str[])
  {
      char data[256];
  
      int number;
      char * ptr;
      char results[100];
      int total = 0;
      char *arguments[1024]; 
      char temp[10];
      int i;
  
      ptr = strtok(buf," ");
      while (ptr != NULL)
      {
          char * temp; 
          temp = (char *)malloc(sizeof(ptr));
          temp = ptr;
          arguments[total] = temp;
          total++;
          ptr = strtok (NULL, " ");
      }
  
      if(total == 1)
      {
          strcat(str, "Invaild Arguments \n");
          return 1;
      }
  
      write(fd, "OK", 256);
      FILE * pFile;
      pFile = fopen ("myfile.txt","w");
      if (pFile!=NULL)
      {
  
          while(read(fd, data, 256) != NULL)
          {
  
              fputs(data, pFile);
          }
  
          fclose (pFile);
      }
      else
      {
          strcat(str, "Invaild File");
          return 0;
      }
      strcat(str, "Done");
      return 1;
  
  }
  </code></pre>
  
  <p>Thanks in advance and just post something if you need to see more code. I just placed the code which should be causing the problem.</p>