6 DWORD WINAPI
ChargenHandler(VOID
* Sock_
)
9 SOCKET Sock
= (SOCKET
)Sock_
;
11 if (!GenerateChars(Sock
)) {
12 _tprintf(_T("Char generation failed\n"));
16 _tprintf(_T("Shutting connection down...\n"));
17 if (ShutdownConnection(Sock
, FALSE
)) {
18 _tprintf(_T("Connection is down.\n"));
22 _tprintf(_T("Connection shutdown failed\n"));
25 _tprintf(_T("Terminating thread\n"));
32 BOOL
GenerateChars(SOCKET Sock
)
35 charIndex
, /* internal loop */
36 loopIndex
; /* line loop */
41 /* fill ring with printable characters */
42 for (charIndex
=0, i
=START
; i
<=END
; charIndex
++, i
++)
44 /* establish the end character in the ring */
45 endring
= &ring
[charIndex
];
47 /* where we will start output from */
52 /* if the loop index is equal to number of chars previously
53 * printed, start the loop from the beginning */
54 if (loopIndex
== END
-START
)
57 /* start printing from char controled by loopIndex */
58 charIndex
= loopIndex
;
59 for (i
=0; i
<LINESIZ
; i
++)
61 /* FIXME: Should send lines instead of chars to improve efficiency
62 * TCP will wait until it fills a packet anway before putting on
63 * the wire, so it's pointless to keep polling send */
64 if (!SendChar(Sock
, ring
[charIndex
]))
68 /* if current char equal last char, reset */
69 if (ring
[charIndex
] == *endring
)
76 if ((!SendChar(Sock
, L
'\r')) || (!SendChar(Sock
, L
'\n')))
79 /* increment loop index to start printing from next char in ring */
86 BOOL
SendChar(SOCKET Sock
, TCHAR c
)
92 RetVal
= send(Sock
, &c
, sizeof(TCHAR
), 0);
93 /*FIXME: need to establish if peer closes connection,
94 not just report a socket error */
100 else if (RetVal
== SOCKET_ERROR
)
102 _tprintf(("Socket error\n"));
106 _tprintf(("unknown error\n"));
109 _tprintf(("Connection closed by peer.\n"));