title: TCP-Socket Programming in C
body: <p>I have two files: tcp-demo-client.c and tcp-demo-server.c</p>
  
  <p>Functionality: If the connection succeeds, the client receives a simple timestamp from the server. I like to modify the code that the server only sends the timestamp if the client hits the space key. How can I do that? </p>
  
  <p>(It's my first socket project)</p>
  
  <p><strong>tcp-demo-client.c:</strong></p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;time.h&gt;
  
  int main(int argc, char **argv)
  {
      int ret;                // return value from functions
  
      // Check command line arguments
      if (argc != 3) {
              fprintf(stderr,
                      "Missing parameters. Usage: %s &lt;server-name-or-ip&gt; &lt;server-port&gt;\n",
                      argv[0]);
              return 1;
      }
      // Address information structure
      struct addrinfo aii;
      // Set whole structure to 0s
      memset(&amp;aii, 0, sizeof(aii));
      // A stream (TCP) connection
      aii.ai_socktype = SOCK_STREAM;
      // We do not care whether it is IPv4 or IPv6
      aii.ai_family = PF_UNSPEC;
  
      struct addrinfo *aio;
      // Get address information. 
      // First parameter is host string, either hostname or numerical IPv4/IPv6 address
      // Second parameter is port/service string, either as port number 
      // or well-known identifier, e.g. http
      // So, e.g. getaddrinfo( "www.compeng.uni-frankfurt.de", "http", ... getaddrinfo( "141.2.248.1", "80", ...
      // Third parameter is input address info structure (cf. above)
      // Fourth parameter is output address info structure, a linked list of potential addresses
      ret = getaddrinfo(argv[1], argv[2], &amp;aii, &amp;aio);
      if (ret) {
              fprintf(stderr, "Error getting address for %s:%s: %s\n",
                      argv[1], argv[2], gai_strerror(ret));
              return 1;
      }
  
      // File descriptor for the socket
      int sock = -1;
      struct addrinfo *iter;
      // Iterate over linked list of specified output addresses, 
      // use first address to which a connection can be established
      for (iter = aio; iter != NULL &amp;&amp; sock == -1; iter = iter-&gt;ai_next) {
              // Create socket given the parameters from the found address info.
              sock =
                  socket(iter-&gt;ai_family, iter-&gt;ai_socktype,
                         iter-&gt;ai_protocol);
              if (sock &lt; 0)
                      continue;       // Appropriate socket could not be created, try next address
  
              // Socket created successfully, now try to connect to remote target address
              // taken from address info
              ret = connect(sock, iter-&gt;ai_addr, iter-&gt;ai_addrlen);
              if (ret) {
                      // Socket could not be connected to remote target
                      close(sock);    // Close socket
                      sock = -1;
                      continue;       // try next address
              }
      }
      freeaddrinfo(aio);      // Release address information allocated in getaddrinfo
  
      if (sock == -1) {
              // No connection at all could be established to remote target
              fprintf(stderr, "Unable to establish any connection to %s:%s\n",
                      argv[1], argv[2]);
              return 1;
      }
  
  
      // Maximum size of incoming message
      int msglen = 100;
  
      // Buffer for message
      char buf[msglen + 1];   // One more to ensure that there is a trailing NULL char.
      memset(buf, 0, msglen + 1);
  
      ret = read(sock, buf, msglen);     // Return value is amount of bytes read, -1 in case of error
      printf("Data read: '%s'\n", buf);
  
      // Clean up after us and close the socket.
      close(sock);
      return 0;
  }
  </code></pre>
  
  <p><strong>tcp-demo-server.c:</strong></p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;time.h&gt;
  
  #define MAXPENDING 5
  
  int main(int argc, char **argv)
  {
      unsigned short listen_port;     // Server port */
      int listen_sock;        // Socket descriptor for server
      int client_sock;        // Socket descriptor for client
      struct sockaddr_in listen_addr; // Local address */
      struct sockaddr_in client_addr; // Client address */
  
      // Check command line arguments
      if (argc != 2) {
              fprintf(stderr, "Missing parameters. Usage: %s &lt;server-port&gt;\n",
                      argv[0]);
              return 1;
      }
      // Create socket for incoming connections
      if ((listen_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) &lt; 0) {
              perror("socket() failed");
              return 1;
      }
      // Construct local address structure 
      listen_port = atoi(argv[1]);    // First arg: listening port number
  
      memset(&amp;listen_addr, 0, sizeof(listen_addr));   // Zero out structure
      listen_addr.sin_family = AF_INET;       // Internet address family
      listen_addr.sin_addr.s_addr = htonl(INADDR_ANY);        // Any incoming interface
      listen_addr.sin_port = htons(listen_port);      // Local port
  
      // Bind to the local address
      if (bind
          (listen_sock, (struct sockaddr *)&amp;listen_addr,
           sizeof(listen_addr)) &lt; 0) {
              perror("bind() failed");
              return 1;
      }
      // Mark the socket so it will listen for incoming connections
      if (listen(listen_sock, MAXPENDING) &lt; 0) {
              perror("listen() failed");
              return 1;
      }
  
      for (;;) {              /* Run forever */
              socklen_t addr_len = sizeof(client_addr);
  
              // Wait for a client to connect */
              if ((client_sock =
                   accept(listen_sock, (struct sockaddr *)&amp;client_addr,
                          &amp;addr_len)) &lt; 0) {
                      perror("accept() failed");
                      return 1;
              }
              // client_sock is connected to a client
              printf("New connection from %s\n",
                     inet_ntoa(client_addr.sin_addr));
  
              // Create message to send
              time_t t = time(NULL);
              char *msg = ctime(&amp;t);
              int msglen = strlen(msg) + 1;
              int ret;
  
  
  
  
              // Write the whole message in one go, fail if this does not work
              ret = write(client_sock, msg, msglen);
              // Return value is amount of bytes written, -1 in case of error
              if (ret != msglen) {
                      perror("Error during write");
                      return 1;
              }
  
              close(client_sock);
      }
      /* NOT REACHED */
      return 1;
  }
  </code></pre>