3 * Copyright (C) 2005 ReactOS Team
5 * LICENCE: GPL - See COPYING in the top level directory
6 * PROJECT: ReactOS simple TCP/IP services
7 * FILE: apps/utils/net/tcpsvcs/chargen.c
8 * PURPOSE: Provide CharGen, Daytime, Discard, Echo, and Qotd services
9 * PROGRAMMERS: Ged Murphy (gedmurphy@gmail.com)
20 DWORD WINAPI
ChargenHandler(VOID
* Sock_
)
23 SOCKET Sock
= (SOCKET
)Sock_
;
25 if (!GenerateChars(Sock
))
27 _tprintf(_T("Char generation failed\n"));
31 _tprintf(_T("Shutting connection down...\n"));
32 if (ShutdownConnection(Sock
, FALSE
))
33 _tprintf(_T("Connection is down.\n"));
36 _tprintf(_T("Connection shutdown failed\n"));
40 _tprintf(_T("Terminating chargen thread\n"));
46 BOOL
GenerateChars(SOCKET Sock
)
49 int charIndex
; /* internal loop */
50 int loopIndex
; /* line loop */
55 /* fill ring with printable characters */
56 for (charIndex
=0, i
=START
; i
<=END
; charIndex
++, i
++)
58 /* save the address of the end character in the ring */
59 endring
= &ring
[charIndex
];
61 /* where we will start output from */
65 /* if the loop index is equal to the last char,
66 * start the loop again from the beginning */
67 if (loopIndex
== END
-START
)
70 /* start printing from char controled by loopIndex */
71 charIndex
= loopIndex
;
72 for (i
=0; i
< LINESIZ
- 2; i
++)
74 Line
[i
] = ring
[charIndex
];
76 if (ring
[charIndex
] == *endring
)
82 Line
[LINESIZ
- 2] = L
'\r';
83 Line
[LINESIZ
- 1] = L
'\n';
85 if (!SendLine(Sock
, Line
))
88 /* increment loop index to start printing from next char in ring */
95 BOOL
SendLine(SOCKET Sock
, TCHAR
* Line
)
101 LineSize
= sizeof(TCHAR
) * LINESIZ
;
104 RetVal
= send(Sock
, Line
, LineSize
, 0);
105 /*FIXME: need to establish if peer closes connection,
106 not just report a socket error */
109 if (RetVal
!= LineSize
)
111 _tprintf(("Not sent enough\n"));
117 else if (RetVal
== SOCKET_ERROR
)
119 _tprintf(("Socket error\n"));
123 _tprintf(("unknown error\n"));
126 _tprintf(("Connection closed by peer.\n"));