]> git.cworth.org Git - ttt/blob - src/ttt-server.c
2cac1177209caf16d7cafb6b2394e384490352e0
[ttt] / src / ttt-server.c
1 /* ttt.c - client-server tic-tac-toe game
2  *
3  * Copyright © 2005 Carl Worth
4  *
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)
8  * any later version.
9  *
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.
14  *
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.
18  *
19  * Author: Carl Worth <cworth@cworth.org>
20  */
21
22 #include "ttt.h"
23 #include "ttt-socket.h"
24
25 static void
26 ttt_dispatch (int connected_socket)
27 {
28 #define BUF_SIZE 1024
29
30     while (1) {
31         char buf[BUF_SIZE];
32         int cnt;
33         cnt = read (connected_socket, buf, BUF_SIZE);
34         if (cnt == 0)
35             break;
36         write (0, buf, cnt);
37         write (connected_socket, buf, cnt);
38     }
39 }
40
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"
45 "\n     %s:%s\n"
46 "\nTo test this program, simply connect a client to that host and port.\n"
47 "For example:\n"
48 "\n     telnet %s %s\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"
53 "\nHave fun!\n"
54 "-Carl\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";
60
61 int 
62 main (int argc, char **argv)
63 {
64     ttt_args_t args;
65     int socket;
66
67     ttt_args_parse (&args, argc, argv);
68
69     if (args.log_file)
70         stderr = xfreopen (args.log_file, "a", stderr);
71
72     socket = ttt_socket_create_server (args.host, args.port);
73
74     printf (WELCOME_MESSAGE, args.host, args.port, args.host, args.port);
75
76     while (1)
77         ttt_socket_accept (socket, ttt_dispatch);
78
79     /* We only reach here if something bad happened. */
80     return 1;
81 }