- Don't call winsock initialiser for each seperate server thread
[reactos.git] / reactos / apps / utils / net / tcpsvcs / qotd.c
1 #include <stdio.h>
2 #include <winsock2.h>
3 #include <tchar.h>
4 #include <time.h>
5 #include "tcpsvcs.h"
6
7 DWORD WINAPI QotdHandler(VOID* Sock_)
8 {
9 DWORD Retval = 0;
10 SOCKET Sock;
11 INT NumOfQuotes;
12 INT QuoteToPrint;
13 TCHAR Quote[160];
14
15 Sock = (SOCKET)Sock_;
16
17 NumOfQuotes = 70; // need to emurate the rc file to discover
18 // how many quotes are in there.
19
20 /* randomise the quote */
21 srand((unsigned int) time(0));
22 QuoteToPrint = rand() % NumOfQuotes;
23
24 LoadString(NULL, QuoteToPrint, Quote, sizeof(Quote)/sizeof(TCHAR));
25
26 SendQuote(Sock, Quote);
27
28 _tprintf(_T("Shutting connection down...\n"));
29 if (ShutdownConnection(Sock, FALSE))
30 _tprintf(_T("Connection is down.\n"));
31 else
32 {
33 _tprintf(_T("Connection shutdown failed\n"));
34 Retval = 3;
35 }
36 _tprintf(_T("Terminating qotd thread\n"));
37 ExitThread(0);
38
39 return Retval;
40 }
41
42
43 BOOL SendQuote(SOCKET Sock, TCHAR* Quote)
44 {
45 INT StringSize;
46 INT RetVal;
47
48 StringSize = strlen(Quote);
49 RetVal = send(Sock, Quote, sizeof(TCHAR) * StringSize, 0);
50
51 if (RetVal == SOCKET_ERROR)
52 return FALSE;
53
54 _tprintf(("Connection closed by peer.\n"));
55 return TRUE;
56 }