76b9714b7d17bd67a05383fea69f05d709dd2b2e
[reactos.git] / reactos / apps / utils / net / tcpsvcs / chargen / chargen.c
1 #include <stdio.h>
2 #include <winsock2.h>
3 #include <tchar.h>
4 #include "chargen.h"
5 #include "../skelserver/skelserver.h"
6
7 DWORD WINAPI ChargenHandler(VOID* Sock_)
8 {
9 DWORD Retval = 0;
10 SOCKET Sock = (SOCKET)Sock_;
11
12 if (!GenerateChars(Sock)) {
13 _tprintf(_T("Echo incoming packets failed\n"));
14 Retval = 3;
15 }
16
17 _tprintf(_T("Shutting connection down...\n"));
18 if (ShutdownConnection(Sock)) {
19 _tprintf(_T("Connection is down.\n"));
20 }
21 else
22 {
23 _tprintf(_T("Connection shutdown failed\n"));
24 Retval = 3;
25 }
26
27 return Retval;
28 }
29
30
31 BOOL GenerateChars(SOCKET Sock)
32 {
33 int i,
34 charIndex, /* internal loop */
35 loopIndex; /* line loop */
36 char ring[END-START];
37 char *endring;
38
39 /* fill ring with printable characters */
40 for (charIndex=0, i=START; i<=END; charIndex++, i++)
41 ring[charIndex] = i;
42 /* establish the end character in the ring */
43 endring = &ring[charIndex];
44
45 /* where we will start output from */
46 loopIndex = 0;
47
48 while (1)
49 {
50 /* if the loop index is equal to number of chars previously
51 * printed, start the loop from the beginning */
52 if (loopIndex == END-START)
53 loopIndex = 0;
54
55 /* start printing from char controled by loopIndex */
56 charIndex = loopIndex;
57 for (i=0; i<LINESIZ; i++)
58 {
59 SendChar(Sock, ring[charIndex]);
60 /* if current char equal last char, reset */
61 if (ring[charIndex] == *endring)
62 charIndex = 0;
63 else
64 charIndex++;
65 }
66 SendChar(Sock, L'\r');
67 SendChar(Sock, L'\n');
68
69 /* increment loop index to start printing from next char in ring */
70 loopIndex++;
71 }
72
73 return 0;
74 }
75
76 BOOL SendChar(SOCKET Sock, TCHAR c)
77 {
78 INT RetVal;
79 INT SentBytes;
80
81 SentBytes = 0;
82 RetVal = send(Sock, &c, sizeof(TCHAR), 0);
83 if (RetVal > 0) {
84 SentBytes += RetVal;
85 }
86 else if (RetVal == SOCKET_ERROR) {
87 return FALSE;
88 }
89 else
90 {
91 /* Client closed connection before we could reply to
92 all the data it sent, so quit early. */
93 _tprintf(_T("Peer unexpectedly dropped connection!\n"));
94 return FALSE;
95 }
96
97 _tprintf(("Connection closed by peer.\n"));
98 return TRUE;
99 }
100
101
102
103
104