bd29c43900f2630da3f02a474c686638018049f3
[reactos.git] / reactos / apps / utils / net / tcpsvcs / daytime.c
1 /*
2 * ReactOS Services
3 * Copyright (C) 2005 ReactOS Team
4 *
5 * LICENCE: GPL - See COPYING in the top level directory
6 * PROJECT: ReactOS simple TCP/IP services
7 * FILE: apps/utils/net/tcpsvcs/daytime.c
8 * PURPOSE: Provide CharGen, Daytime, Discard, Echo, and Qotd services
9 * PROGRAMMERS: Ged Murphy (gedmurphy@gmail.com)
10 * REVISIONS:
11 * GM 04/10/05 Created
12 *
13 */
14
15 #include <stdio.h>
16 #include <winsock2.h>
17 #include <tchar.h>
18 #include <time.h>
19 #include "tcpsvcs.h"
20
21 DWORD WINAPI DaytimeHandler(VOID* Sock_)
22 {
23 struct tm *newtime;
24 time_t aclock;
25 TCHAR *pszTime;
26 DWORD RetVal = 0;
27 SOCKET Sock = (SOCKET)Sock_;
28
29 time(&aclock);
30 newtime = localtime(&aclock);
31 pszTime = _tasctime(newtime);
32
33 SendTime(Sock, pszTime);
34
35 _tprintf(_T("Shutting connection down...\n"));
36 if (ShutdownConnection(Sock, FALSE))
37 _tprintf(_T("Connection is down.\n"));
38 else
39 {
40 _tprintf(_T("Connection shutdown failed\n"));
41 RetVal = -1;
42 }
43
44 _tprintf(_T("Terminating daytime thread\n"));
45 ExitThread(RetVal);
46 }
47
48
49 BOOL SendTime(SOCKET Sock, TCHAR *time)
50 {
51 INT StringSize = strlen(time);
52 INT RetVal = send(Sock, time, sizeof(TCHAR) * StringSize, 0);
53
54 if (RetVal == SOCKET_ERROR)
55 return FALSE;
56
57 _tprintf(("Connection closed by peer.\n"));
58 return TRUE;
59 }