1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
/* This will develop into the inted-like program which
we may want to use for a server on Win95/NT. Right now
it is just a test program ("telnet foo 2401" and you'll
get a message). */
#include <winsock.h>
#include <stdio.h>
#include <io.h>
#include <process.h>
int
main ()
{
struct sockaddr_in sa;
SOCKET t;
SOCKET s;
WSADATA data;
if (WSAStartup (MAKEWORD (1, 1), &data))
{
fprintf (stderr, "cvs: unable to initialize winsock\n");
exit (1);
}
t = socket (PF_INET, SOCK_STREAM, 0);
if (t == INVALID_SOCKET)
{
printf ("Error in socket(): %d\n", WSAGetLastError ());
exit (1);
}
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = INADDR_ANY;
sa.sin_port = htons (2401);
if (bind (t, (struct sockaddr *) &sa, sizeof (sa)) != 0)
{
printf ("Cannot bind(): %d\n", WSAGetLastError ());
exit (1);
}
if (listen (t, 1) != 0)
{
printf ("Cannot listen(): %d\n", WSAGetLastError ());
exit (1);
}
while (1)
{
int sasize = sizeof (sa);
#if 0
int save_stdin, save_stdout;
#endif
s = accept (t, (struct sockaddr *) &sa, &sasize);
if (s == INVALID_SOCKET)
{
printf ("Cannot accept(): %d\n", WSAGetLastError ());
exit (1);
}
#if 0
/* This, of course, does not work because sockets are
not file descriptors and file descriptors are not
sockets. Duh! */
save_stdin = _dup (0);
if (save_stdin < 0)
{
printf ("Cannot save stdin: %s\n", strerror (errno));
exit (1);
}
save_stdout = _dup (1);
if (save_stdout < 0)
{
printf ("Cannot save stdout: %s\n", strerror (errno));
exit (1);
}
if (_dup2 (s, 0) < 0)
{
printf ("Cannot dup stdin: %s\n", strerror (errno));
exit (1);
}
if (_dup2 (s, 1) < 0)
{
printf ("Cannot dup stdout: %s\n", strerror (errno));
exit (1);
}
/* Of course this will be "cvs" eventually, but "netstat"
is for testing. */
if (_spawnl (_P_DETACH, "netstat", "netstat", NULL) < 0)
{
printf ("Cannot spawn subprocess: %s\n", strerror (errno));
exit (1);
}
#else
if (send (s, "hello, world\n", 13, 0) == SOCKET_ERROR)
{
/* Note that we do not detect the case in which we sent
less than the requested number of bytes. */
printf ("Cannot send(): %d\n", WSAGetLastError ());
exit (1);
}
#endif
if (closesocket (s) != 0)
{
printf ("Cannot closesocket(): %d\n", WSAGetLastError ());
exit (1);
}
}
return 0;
}
|