title: Winsock echo server port
body: <p>I'm trying to port this simple TCP echo program (<a href="https://github.com/mafintosh/echo-servers.c/blob/master/tcp-echo-server.c" rel="nofollow">https://github.com/mafintosh/echo-servers.c/blob/master/tcp-echo-server.c</a>) under Windows for teaching purposes. 
  My adaptation compiles and run, but it doesn't work:</p>
  
  <p>**** EDIT: The listen call was somehow cut out. Thank to Remy ****</p>
  
  <p>The client connects, but doen't get any echo.</p>
  
  <p>Ported code is as follows (error messages are in Italian, but they should be clear nevertheless):</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;winsock.h&gt;
  
  #define BUFFER_SIZE 1024
  void on_error(char *s) { fprintf(stderr,"%s\n",s); fflush(stderr); exit(1); }
  
  int main(int argc, char *argv[]) {
      WSADATA wsadata; 
      int server_fd, client_fd, err;
      struct sockaddr_in server, client;
      char buf[BUFFER_SIZE];
  
      int port = 6666; 
      int risultato = WSAStartup(MAKEWORD(2,2),&amp;wsadata);
      if (risultato != NO_ERROR)
          {fprintf(stderr,"Errore in WSAStartup");fflush(stderr); exit(1);}
  
      server_fd = socket(AF_INET, SOCK_STREAM, 0);
      if (server_fd &lt; 0) on_error("Non ho potuto creare il socket\n");    
      server.sin_family = AF_INET; 
      server.sin_port = htons(port); 
      server.sin_addr.s_addr = htonl(INADDR_ANY); 
  
      const char opt_val = 1;
      setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &amp;opt_val, sizeof opt_val);
      /** bind &amp; listen **/
      err = bind(server_fd, (struct sockaddr *) &amp;server, sizeof(server));
      if (err &lt; 0) on_error("Non ho potuto fare il bind del socket\n");   
      err = listen(server_fd, 128);
      if (err &lt; 0) on_error("Non ho potuto mettermi in ascolto sul socket\n");
      printf("SERVER LISTENING ON PORT %d\n", port);
  
      while (1) {
        int client_len = sizeof(client);
        do {
        client_fd = accept(server_fd, (struct sockaddr *) &amp;client, &amp;client_len);
        } while ( client_fd = SOCKET_ERROR);
  
        if (client_fd &lt; 0) on_error("Non riesco a stabilire una nuova connessione\n");
  
        while (1) {
          int read = recv(client_fd, buf, BUFFER_SIZE, 0);
  
          if (!read) break;
          if (read &lt; 0) on_error("Errore nella lettura dal client\n");
  
          err = send(client_fd, buf, read, 0);
          if (err &lt; 0) on_error("Errore nella scrittura verso il client\n");
      }
    }
  
      WSACleanup();
      return 0;
  }
  </code></pre>