+static ttt_status_t
+_sockaddr_init (struct sockaddr_in *addr,
+ const char *host,
+ const char *port)
+{
+ struct hostent *hostent;
+ struct servent *servent;
+ char *endptr;
+ long port_long;
+ uint16_t port_short;
+
+ addr->sin_family = AF_INET;
+
+ hostent = gethostbyname (host);
+ if (hostent == NULL)
+ {
+ fprintf (stderr, "Error: Lookup failed for host %s: %s\n",
+ host, hstrerror (h_errno));
+ return TTT_STATUS_FAILURE;
+ }
+ memcpy (&addr->sin_addr.s_addr, hostent->h_addr_list[0],
+ sizeof (addr->sin_addr.s_addr));
+
+ assert (*port != '\0');
+ port_long = strtol (port, &endptr, 10);
+ if (*endptr == '\0')
+ {
+ /* Numeric port*/
+ if (port_long <= 0 || port_long >= (1 << 16)) {
+ fprintf (stderr, "Error: Port %ld out of range.\n", port_long);
+ return TTT_STATUS_FAILURE;
+ }
+ port_short = port_long;
+ addr->sin_port = htons (port_short);
+ } else {
+ /* Named port */
+ servent = getservbyname (port, "tcp");
+ if (port == NULL) {
+ fprintf (stderr, "Error: Lookup failed for port %s: %s\n",
+ port, hstrerror (h_errno));
+ return TTT_STATUS_FAILURE;
+ }
+ addr->sin_port = servent->s_port;
+ }
+
+ return TTT_STATUS_SUCCESS;
+}
+
+static int
+_wait_for_connection (int listen_socket)
+{
+ fd_set read_set;
+ int num_selected;
+ int flags, err;
+ int connected_socket;
+
+ FD_ZERO (&read_set);
+ FD_SET (listen_socket, &read_set);
+
+ while (1) {
+ num_selected = select (listen_socket + 1,
+ &read_set, NULL, NULL,
+ NULL);
+
+ flags = fcntl (listen_socket, F_GETFL);
+ flags |= O_NONBLOCK;
+ xfcntl (listen_socket, F_SETFL, flags);
+
+ connected_socket = accept (listen_socket, 0, 0);
+ err = errno;
+
+ flags &= ~O_NONBLOCK;
+ xfcntl (listen_socket, F_SETFL, flags);
+
+ if (connected_socket != -1)
+ return connected_socket;
+
+ if (err == EWOULDBLOCK || err == EAGAIN)
+ continue;
+
+ fprintf (stderr, "Error: accept failed: %s. Aborting.\n",
+ strerror (err));
+ exit (1);
+ }
+}
+
+static const char *WELCOME_MESSAGE =
+"Welcome to ttt-server. So far, this program is simply a demonstration\n"
+"of a one-shot TCP/IP server. The server is currently listening on:\n"
+"\n %s:%s\n"
+"\nTo test this program, simply connect a client to that host and port.\n"
+"For example:\n"
+"\n telnet %s %s\n"
+"\nOnce you have connected a client, the server will echo any characters\n"
+"it receives back to the client as well as to stdout.\n"
+"\nNote that to terminate the telnet client you type Control-], then\n"
+"<Enter>, then \"close\" (and <Enter>) at the \"telnet> \" prompt.\n"
+"\nHave fun!\n"
+"-Carl\n"
+"\nPS. By one-shot, I mean that the server will only accept a connection\n"
+"from a single client, and the server will exit when that client disconnects\n"
+"To test the server again, you will need to manually restart it again.\n"
+"Extending the server to start listening again, or to handle multiple\n"
+"clients simultaneously would both be fun projects for a motivated\n"
+"student (as would writing a custom client program).\n\n";
+