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 extern BOOL bShutDown
;
19 DWORD WINAPI
EchoHandler(VOID
* Sock_
)
22 SOCKET Sock
= (SOCKET
)Sock_
;
24 if (!EchoIncomingPackets(Sock
)) {
25 LogEvent(_T("Echo: EchoIncomingPackets failed\n"), 0, FALSE
);
29 LogEvent(_T("Echo: Shutting connection down...\n"), 0, FALSE
);
31 if (ShutdownConnection(Sock
, TRUE
))
32 LogEvent(_T("Echo: Connection is down\n"), 0, FALSE
);
35 LogEvent(_T("Echo: Connection shutdown failed\n"), 0, FALSE
);
39 LogEvent(_T("Echo: Terminating thread\n"), 0, FALSE
);
45 BOOL
EchoIncomingPackets(SOCKET Sock
)
47 TCHAR ReadBuffer
[BUF
];
48 TCHAR buf
[256]; // temp for holding LogEvent text
54 ReadBytes
= recv(Sock
, ReadBuffer
, BUF
, 0);
57 _stprintf(buf
, _T("Received %d bytes from client\n"), ReadBytes
);
58 LogEvent(buf
, 0, FALSE
);
61 while (SentBytes
< ReadBytes
)
63 Temp
= send(Sock
, ReadBuffer
+ SentBytes
,
64 ReadBytes
- SentBytes
, 0);
67 _stprintf(buf
, _T("Sent %d bytes back to client\n"), Temp
);
68 LogEvent(buf
, 0, FALSE
);
71 else if (Temp
== SOCKET_ERROR
)
75 /* Client closed connection before we could reply to
76 all the data it sent, so quit early. */
77 _stprintf(buf
, _T("Peer unexpectedly dropped connection!\n"));
78 LogEvent(buf
, 0, FALSE
);
83 else if (ReadBytes
== SOCKET_ERROR
)
86 } while ((ReadBytes
!= 0) && (! bShutDown
));
89 LogEvent(_T("Echo: Connection closed by peer.\n"), 0, FALSE
);
92 LogEvent(_T("Echo: thread recieved shutdown signal\n"), 0, FALSE
);