1 /* ttt.c - client-server tic-tac-toe game
3 * Copyright © 2005 Carl Worth
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * Author: Carl Worth <cworth@cworth.org>
23 #include "ttt-socket.h"
26 ttt_dispatch (int connected_socket)
33 cnt = read (connected_socket, buf, BUF_SIZE);
37 write (connected_socket, buf, cnt);
41 static const char *WELCOME_MESSAGE =
42 "Welcome to ttt-server. So far, this program is simply a demonstration\n"
43 "of a TCP/IP server that handles one client at a time. The server is\n"
44 "currently listening on:\n"
46 "\nTo test this program, simply connect a client to that host and port.\n"
49 "\nOnce you have connected a client, the server will echo any characters\n"
50 "it receives back to the client as well as to stdout.\n"
51 "\nNote that to terminate the telnet client you type Control-], then\n"
52 "<Enter>, then \"close\" (and <Enter>) at the \"telnet> \" prompt.\n"
55 "\nPS. The server handles only one client at a time, but multiple clients\n"
56 "may be able to connect simultaneously. Subsequent clients will see no\n"
57 "output from the server until all previous clients exit.\n"
58 "\nExtending the server to fork a new process for each client would be a fun\n"
59 "project for a motivated student (as would writing a custom client program).\n\n";
62 main (int argc, char **argv)
67 ttt_args_parse (&args, argc, argv);
70 stderr = xfreopen (args.log_file, "a", stderr);
72 socket = ttt_socket_create_server (args.host, args.port);
74 printf (WELCOME_MESSAGE, args.host, args.port, args.host, args.port);
77 ttt_socket_accept (socket, ttt_dispatch);
79 /* We only reach here if something bad happened. */