title: bind() get "Invalid argument" error in Linux network programming
body: <p>This is my code</p>
  
  <pre><code>#include "basic.h"
  #include "bp_socket.h"
  
  int setup_tcp(char *port)
  {
      struct sockaddr_in server_addr;
      in_port_t server_port;
      int sock;
      int retval;                     //return value
  
      if ((server_port = atoi(port)) &lt; 0)
      {
          server_port = (in_port_t)DEFAULT_PORT;
          fprintf(stderr, "Error port: %d in line %d\n", (int)server_port, __LINE__);
          //fprintf(stderr, "Use default port: %d\n", DEFAULT_PORT);
      }
  
      sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
      if (sock &lt; 0)
      {
          return sock;
      }
  
      retval = listen(sock, 40);
      if (retval &lt; 0)
      {
          return retval;
      }
  
      memset(&amp;server_addr, 0, sizeof(struct sockaddr_in));
      server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
      server_addr.sin_port        = htons(server_port);
      server_addr.sin_family      = AF_INET;
      retval = bind(sock, (struct sockaddr*)&amp;server_addr, sizeof(server_addr));
      if (retval &lt; 0)
      {
          perror("bind");
          return retval;
      }
  
      return sock;
  }
  
  int main()
  {
      printf("%d\n", setup_tcp("4558"));
      perror("");
      return 0;
  }
  </code></pre>
  
  <p>I debug my code, then I found <code>bind()</code> return <code>-1</code></p>
  
  <p>I google it and read man page, but I couldn't found my answer.</p>
  
  <p>function <code>setup_tcp</code> is create socket, and bind with address. </p>
  
  <p>then return <code>a socket</code> or <code>errno value</code>.</p>
  
  <p>How to solve it. I had read my code for many times.</p>
  
  <p>Thank you!</p>