6 DWORD WINAPI
ChargenHandler(VOID
* Sock_
)
9 SOCKET Sock
= (SOCKET
)Sock_
;
11 if (!GenerateChars(Sock
))
13 _tprintf(_T("Char generation failed\n"));
17 _tprintf(_T("Shutting connection down...\n"));
18 if (ShutdownConnection(Sock
, FALSE
))
19 _tprintf(_T("Connection is down.\n"));
22 _tprintf(_T("Connection shutdown failed\n"));
25 _tprintf(_T("Terminating chargen thread\n"));
32 BOOL
GenerateChars(SOCKET Sock
)
35 int charIndex
; /* internal loop */
36 int 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 */
51 /* if the loop index is equal to the last char,
52 * start the loop again from the beginning */
53 if (loopIndex
== END
-START
)
56 /* start printing from char controled by loopIndex */
57 charIndex
= loopIndex
;
58 for (i
=0; i
< LINESIZ
- 2; i
++)
60 Line
[i
] = ring
[charIndex
];
62 if (ring
[charIndex
] == *endring
)
68 Line
[LINESIZ
- 2] = L
'\r';
69 Line
[LINESIZ
- 1] = L
'\n';
71 if (!SendLine(Sock
, Line
))
74 /* increment loop index to start printing from next char in ring */
81 BOOL
SendLine(SOCKET Sock
, TCHAR
* Line
)
87 LineSize
= sizeof(TCHAR
) * LINESIZ
;
90 RetVal
= send(Sock
, Line
, LineSize
, 0);
91 /*FIXME: need to establish if peer closes connection,
92 not just report a socket error */
95 if (RetVal
!= LineSize
)
97 _tprintf(("Not sent enough\n"));
103 else if (RetVal
== SOCKET_ERROR
)
105 _tprintf(("Socket error\n"));
109 _tprintf(("unknown error\n"));
112 _tprintf(("Connection closed by peer.\n"));