title: segmentation fault in linux (socket programming (TCP) in C)
body: <p>I am just learning socket programming on Linux by some websites and here are some parts of my code on server side by using TCP:</p>
  
  <pre><code>#define BufferLength 100
  #define SERVPORT 3111
  int main()
  {
    /* Variable and structure definitions. */
    int sd, sd2, rc, length = sizeof(int);
    int totalcnt = 0, on = 1;
    char temp;
    char buffer[BufferLength];
    struct sockaddr_in serveraddr;
    struct sockaddr_in their_addr;
    fd_set read_fd;
  
    /* Get a socket descriptor */
    if((sd = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0)
    {
      perror("Server-socket() error");
      exit (-1);
    }
    else
      printf("Server-socket() is OK\n");
  
    /* Allow socket descriptor to be reusable */
    if((rc = setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&amp;on, sizeof(on))) &lt; 0)
    {
      perror("Server-setsockopt() error");
      close(sd);
      exit (-1);
    }
    else
      printf("Server-setsockopt() is OK\n");
  
    /* bind to an address */
    memset(&amp;serveraddr, 0x00, sizeof(struct sockaddr_in));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(SERVPORT);
    serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
    printf("Using %s, listening at %d\n", inet_ntoa(serveraddr.sin_addr), SERVPORT);
  
    /* continue */
  }
  </code></pre>
  
  <p>When I did the last line (printf("using......")), I got a segmentation fault, why? Thanks.</p>