1 |
// echo.C -*- C++ -*- socket library |
2 |
// Copyright (C) 1992,1993,1994 Gnanasekaran Swaminathan <gs4t@virginia.edu> |
3 |
// |
4 |
// Permission is granted to use at your own risk and distribute this software |
5 |
// in source and binary forms provided the above copyright |
6 |
// notice and this paragraph are preserved on all copies. |
7 |
// This software is provided "as is" with no express or implied warranty. |
8 |
// |
9 |
// Version: 17Oct95 1.10 |
10 |
|
11 |
#include <config.h> |
12 |
|
13 |
#include <echo.h> |
14 |
#include <Fork.h> |
15 |
#include <stdlib.h> |
16 |
|
17 |
void echo::echobuf::serve_clients (int portno) |
18 |
{ |
19 |
if (protocol_name ()) { |
20 |
if (portno < 0) |
21 |
sockinetbuf::bind ((unsigned long) INADDR_ANY, "echo", protocol_name ()); |
22 |
else if (portno <= 1024) { |
23 |
sockinetbuf::bind (); |
24 |
cout << "Host: " << localhost () << endl |
25 |
<< "Port: " << localport () << endl; |
26 |
} else |
27 |
sockinetbuf::bind ((unsigned long) INADDR_ANY, portno); |
28 |
|
29 |
// act as a server now |
30 |
listen (sockbuf::somaxconn); |
31 |
|
32 |
// commit suicide when we receive SIGTERM |
33 |
Fork::suicide_signal (SIGTERM); |
34 |
|
35 |
for (;;) { |
36 |
sockbuf s = accept (); |
37 |
|
38 |
Fork f (1, 1); // kill my children when I get terminated. |
39 |
|
40 |
if (f.is_child ()) { |
41 |
char buf [1024]; |
42 |
int rcnt; |
43 |
|
44 |
while ((rcnt = s.read (buf, 1024)) > 0) |
45 |
while (rcnt != 0) { |
46 |
int wcnt = s.write (buf, rcnt); |
47 |
if (wcnt == -1) { |
48 |
error ("echo::echobuf::bind"); |
49 |
return; |
50 |
} |
51 |
rcnt -= wcnt; |
52 |
} |
53 |
sleep (300); |
54 |
exit (0); |
55 |
} |
56 |
} |
57 |
} |
58 |
} |
59 |
|