a5215218cd14408c02d3cd4c2f9e11e512a4f311
[reactos.git] / reactos / apps / utils / net / tcpsvcs / discard.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/discard.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 "tcpsvcs.h"
19
20 DWORD WINAPI DiscardHandler(VOID* Sock_)
21 {
22 DWORD RetVal = 0;
23 SOCKET Sock = (SOCKET)Sock_;
24
25 if (!RecieveIncomingPackets(Sock))
26 {
27 _tprintf(_T("RecieveIncomingPackets failed\n"));
28 RetVal = -1;
29 }
30
31 _tprintf(_T("Shutting connection down...\n"));
32 if (ShutdownConnection(Sock, TRUE))
33 {
34 _tprintf(_T("Connection is down.\n"));
35 }
36 else
37 {
38 _tprintf(_T("Connection shutdown failed\n"));
39 RetVal = -1;
40 }
41
42 _tprintf(_T("Terminating discard thread\n"));
43 ExitThread(RetVal);
44 }
45
46
47
48 BOOL RecieveIncomingPackets(SOCKET Sock)
49 {
50 TCHAR ReadBuffer[BUF];
51 INT ReadBytes;
52
53 do
54 {
55 ReadBytes = recv(Sock, ReadBuffer, BUF, 0);
56 if (ReadBytes > 0)
57 _tprintf(_T("Received %d bytes from client\n"), ReadBytes);
58 else if (ReadBytes == SOCKET_ERROR)
59 {
60 _tprintf(("Socket Error: %d\n"), WSAGetLastError());
61 return FALSE;
62 }
63 } while (ReadBytes > 0);
64
65 _tprintf(("Connection closed by peer.\n"));
66 return TRUE;
67 }