1 |
ab11 |
1.1 |
// tsinread.cc. Test for -*- 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 <sockinet.h> |
12 |
|
|
#include <unistd.h> |
13 |
|
|
|
14 |
|
|
static void process_input (iosockinet& s); |
15 |
|
|
|
16 |
|
|
int main() |
17 |
|
|
{ |
18 |
|
|
sockinetbuf sin (sockbuf::sock_stream); |
19 |
|
|
|
20 |
|
|
sin.bind(); |
21 |
|
|
|
22 |
|
|
cout << "localhost = " << sin.localhost() << endl |
23 |
|
|
<< "localport = " << sin.localport() << endl; |
24 |
|
|
|
25 |
|
|
sin.listen(); |
26 |
|
|
|
27 |
|
|
for(;;) { |
28 |
|
|
iosockinet s (sin.accept()); |
29 |
|
|
|
30 |
|
|
process_input(s); |
31 |
|
|
s->close(); |
32 |
|
|
|
33 |
|
|
// lets use select to find out whether a socket is ready |
34 |
|
|
if (!sin.is_readready(2)) { |
35 |
|
|
// the socket is not ready. Let us sleep for 1 sec. |
36 |
|
|
sleep(1); |
37 |
|
|
} |
38 |
|
|
|
39 |
|
|
iosockinet s2 (sin.accept()); |
40 |
|
|
process_input(s2); |
41 |
|
|
break; // let us get out of forever |
42 |
|
|
} |
43 |
|
|
return 0; |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
static void process_input(iosockinet& sio) |
47 |
|
|
{ |
48 |
|
|
char buf[256]; |
49 |
|
|
char* p = buf; |
50 |
|
|
|
51 |
|
|
sio >> p; |
52 |
|
|
|
53 |
|
|
while (*p) { |
54 |
|
|
if (*p != '%') |
55 |
|
|
cout << *p; |
56 |
|
|
else |
57 |
|
|
switch (*++p) { |
58 |
|
|
case 'd': { |
59 |
|
|
int i; |
60 |
|
|
sio >> i; |
61 |
|
|
cout << i << ' '; |
62 |
|
|
sio << "INT "; |
63 |
|
|
break; |
64 |
|
|
} |
65 |
|
|
case 'f': { |
66 |
|
|
double d; |
67 |
|
|
sio >> d; |
68 |
|
|
cout << d << ' '; |
69 |
|
|
sio << "DOUBLE "; |
70 |
|
|
break; |
71 |
|
|
} |
72 |
|
|
case 's': { |
73 |
|
|
char str[256]; |
74 |
|
|
sio >> str; |
75 |
|
|
cout << str << ' '; |
76 |
|
|
sio << "STRING "; |
77 |
|
|
break; |
78 |
|
|
} |
79 |
|
|
case 'c': { |
80 |
|
|
char c; |
81 |
|
|
sio >> c; |
82 |
|
|
cout << c << ' '; |
83 |
|
|
sio << "CHAR "; |
84 |
|
|
break; |
85 |
|
|
} |
86 |
|
|
case '%': |
87 |
|
|
cout << '%' << ' '; |
88 |
|
|
break; |
89 |
|
|
default: |
90 |
|
|
cout << '%' << *p; |
91 |
|
|
} |
92 |
|
|
p++; |
93 |
|
|
} |
94 |
|
|
sio << "received" << endl; |
95 |
|
|
cout << endl; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
|