title: Why read and write can not work normally
body: <p>I write a simple server to transfer a file to the client. Here are the source codes.</p>
  
  <pre><code>//server.c
  #include "general.h"
  #define LISTENQ 10
  #define BUFSIZE 1024
  #define FILENAME "List"
  
  void sendlist(int sockfd) {
      int fd = open(FILENAME, O_RDONLY);
      char readbuf[BUFSIZE];
      ssize_t n;
  
      if(fd == -1)
              exit(1);
  
      for(;;) {
              if((n = read(fd, readbuf, BUFSIZE)) &gt; 0)
                      write(sockfd, readbuf, BUFSIZE);
              else
                      break;
      }
  
      close(fd);
      close(sockfd);
  }
  
  int main(int argc, char ** argv) {
      int listenfd, connfd;
      pid_t childpid;
      socklen_t len;
      struct sockaddr_in cliaddr, servaddr;
  
      listenfd = socket(AF_INET, SOCK_STREAM, 0);
  
      bzero(&amp;servaddr, sizeof(servaddr));
      servaddr.sin_family = AF_INET;
      servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
      servaddr.sin_port = htons(SERV_PORT);
  
      bind(listenfd, (struct sockaddr_in *)&amp;servaddr, sizeof(servaddr));
      listen(listenfd,LISTENQ);
  
      for(;;) {
              len = sizeof(cliaddr);
              connfd = accept(listenfd, (struct sockaddr_in *)&amp;cliaddr, &amp;len);
              if((childpid = fork()) == 0) {
                      close(listenfd);
                      sendlist(connfd);
                      exit(0);
              }
              close(connfd);
      }
  }
  
  
  
  //client.c
  #include "general.h"
  
  #define MAXLINE 1024
  #define FILENAME "List2"
  
  void getlist(int sockfd) {
      int fd = creat(FILENAME, S_IRUSR | S_IWUSR);
      char buf[MAXLINE];
      ssize_t n;
  
      if(fd == -1)
              exit(2);
  
      for(;;) {
              if((n = read(sockfd, buf, MAXLINE)) &gt; 0)
                      write(fd, buf, MAXLINE);
              else
                      break;
      }
  
      close(fd);
  }
  
  int main(int argc, char ** argv) {
      int sockfd;
      struct sockaddr_in servaddr;
  
      if(argc != 2)
              exit(1);
  
      sockfd = socket(AF_INET, SOCK_STREAM, 0);
      bzero(&amp;servaddr, sizeof(servaddr));
      servaddr.sin_family = AF_INET;
      servaddr.sin_port = htons(SERV_PORT);
      inet_pton(AF_INET, argv[1], &amp;servaddr.sin_addr);
  
      connect(sockfd, (struct sockaddr_in *)&amp;servaddr, sizeof(servaddr));
  
      getlist(sockfd);
  
      exit(0);
  }
  </code></pre>
  
  <p>My problem is: the file List and List2 are not the same.
  For example, the content of file List is:<code>test</code>
  Content of file List2 contains not only <code>test</code>, but also many unreadable codes like this:</p>
  
  <pre><code>^@^@^@^@^@^@^@^@^@&lt;8c&gt;]úUü^?^@^@ ^G^\Vü^?^@^@^A^@^@^
  </code></pre>
  
  <p>I think the reason are read and write function. Shouldn't I use them?
  Thanks</p>