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)
17 DWORD WINAPI
EchoHandler(VOID
* Sock_
)
20 SOCKET Sock
= (SOCKET
)Sock_
;
22 if (!EchoIncomingPackets(Sock
)) {
23 LogEvent(_T("Echo: EchoIncomingPackets failed\n"), 0, FALSE
);
27 LogEvent(_T("Echo: Shutting connection down...\n"), 0, FALSE
);
29 if (ShutdownConnection(Sock
, TRUE
))
30 LogEvent(_T("Echo: Connection is down\n"), 0, FALSE
);
33 LogEvent(_T("Echo: Connection shutdown failed\n"), 0, FALSE
);
37 LogEvent(_T("Echo: Terminating thread\n"), 0, FALSE
);
43 BOOL
EchoIncomingPackets(SOCKET Sock
)
45 TCHAR ReadBuffer
[BUF
];
46 TCHAR temp
[512]; // temp for holding LogEvent text
52 ReadBytes
= recv(Sock
, ReadBuffer
, BUF
, 0);
55 _stprintf(temp
, _T("Received %d bytes from client\n"), ReadBytes
);
56 LogEvent(temp
, 0, FALSE
);
59 while (SentBytes
< ReadBytes
)
61 Temp
= send(Sock
, ReadBuffer
+ SentBytes
,
62 ReadBytes
- SentBytes
, 0);
65 _stprintf(temp
, _T("Sent %d bytes back to client\n"), Temp
);
66 LogEvent(temp
, 0, FALSE
);
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 _stprintf(temp
, _T("Peer unexpectedly dropped connection!\n"));
76 LogEvent(temp
, 0, FALSE
);
81 else if (ReadBytes
== SOCKET_ERROR
)
84 } while (ReadBytes
!= 0);
86 LogEvent(_T("Echo: Connection closed by peer.\n"), 0, FALSE
);