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/echo.c
8 * PURPOSE: Provide CharGen, Daytime, Discard, Echo, and Qotd services
9 * PROGRAMMERS: Ged Murphy (gedmurphy@gmail.com)
20 DWORD WINAPI
EchoHandler(VOID
* Sock_
)
23 SOCKET Sock
= (SOCKET
)Sock_
;
25 if (!EchoIncomingPackets(Sock
)) {
26 _tprintf(_T("Echo incoming packets failed\n"));
30 _tprintf(_T("Shutting connection down...\n"));
31 if (ShutdownConnection(Sock
, TRUE
)) {
32 _tprintf(_T("Connection is down.\n"));
36 _tprintf(_T("Connection shutdown failed\n"));
40 _tprintf(_T("Terminating echo thread\n"));
46 BOOL
EchoIncomingPackets(SOCKET Sock
)
48 TCHAR ReadBuffer
[BUF
];
54 ReadBytes
= recv(Sock
, ReadBuffer
, BUF
, 0);
57 _tprintf(_T("Received %d bytes from client\n"), ReadBytes
);
60 while (SentBytes
< ReadBytes
)
62 Temp
= send(Sock
, ReadBuffer
+ SentBytes
,
63 ReadBytes
- SentBytes
, 0);
66 _tprintf(_T("Sent %d bytes back to client\n"), Temp
);
69 else if (Temp
== SOCKET_ERROR
)
73 /* Client closed connection before we could reply to
74 all the data it sent, so quit early. */
75 _tprintf(_T("Peer unexpectedly dropped connection!\n"));
80 else if (ReadBytes
== SOCKET_ERROR
)
83 } while (ReadBytes
!= 0);
85 _tprintf(("Connection closed by peer.\n"));