271afdfc46b1a846ef7a2cb8523fbe384ca25364
[reactos.git] / reactos / apps / utils / net / tcpsvcs / daytime.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 DaytimeHandler(VOID* Sock_)
8 {
9 struct tm *newtime;
10 time_t aclock;
11 TCHAR *pszTime;
12 DWORD Retval = 0;
13 SOCKET Sock = (SOCKET)Sock_;
14
15 time(&aclock);
16 newtime = localtime(&aclock);
17 pszTime = _tasctime(newtime);
18
19 SendTime(Sock, pszTime);
20
21 _tprintf(_T("Shutting connection down...\n"));
22 if (ShutdownConnection(Sock, FALSE))
23 _tprintf(_T("Connection is down.\n"));
24 else
25 {
26 _tprintf(_T("Connection shutdown failed\n"));
27 Retval = 3;
28 }
29 _tprintf(_T("Terminating thread\n"));
30 ExitThread(0);
31
32 return Retval;
33 }
34
35
36 BOOL SendTime(SOCKET Sock, TCHAR *time)
37 {
38 INT StringSize = strlen(time);
39 INT RetVal = send(Sock, time, sizeof(TCHAR) * StringSize, 0);
40
41 if (RetVal == SOCKET_ERROR)
42 return FALSE;
43
44 _tprintf(("Connection closed by peer.\n"));
45 return TRUE;
46 }