2 * PROJECT: ReactOS simple TCP/IP services
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: /base/services/tcpsvcs/echo.c
5 * PURPOSE: Returns whatever input the client sends
6 * COPYRIGHT: Copyright 2005 - 2008 Ged Murphy <gedmurphy@reactos.org>
15 EchoIncomingPackets(SOCKET sock
)
17 CHAR readBuffer
[RECV_BUF
];
25 readBytes
= recv(sock
, readBuffer
, RECV_BUF
, 0);
28 _swprintf(logBuf
, L
"Received %d bytes from client", readBytes
);
29 LogEvent(logBuf
, 0, 0, LOG_FILE
);
32 while (!bShutdown
&& totalSentBytes
< readBytes
)
34 retVal
= send(sock
, readBuffer
+ totalSentBytes
, readBytes
- totalSentBytes
, 0);
37 _swprintf(logBuf
, L
"Sent %d bytes back to client", retVal
);
38 LogEvent(logBuf
, 0, 0, LOG_FILE
);
39 totalSentBytes
+= retVal
;
41 else if (retVal
== SOCKET_ERROR
)
43 LogEvent(L
"Echo: socket error", WSAGetLastError(), 0, LOG_ERROR
);
48 /* Client closed connection before we could reply to
49 all the data it sent, so quit early. */
50 LogEvent(L
"Peer unexpectedly dropped connection!", 0, 0, LOG_FILE
);
55 else if (readBytes
== SOCKET_ERROR
)
57 LogEvent(L
"Echo: socket error", WSAGetLastError(), 0, LOG_ERROR
);
60 } while ((readBytes
!= 0) && (!bShutdown
));
63 LogEvent(L
"Echo: Connection closed by peer", 0, 0, LOG_FILE
);
69 EchoHandler(VOID
* sock_
)
72 SOCKET sock
= (SOCKET
)sock_
;
74 if (!EchoIncomingPackets(sock
))
76 LogEvent(L
"Echo: EchoIncomingPackets failed", 0, 0, LOG_FILE
);
80 LogEvent(L
"Echo: Shutting connection down", 0, 0, LOG_FILE
);
82 if (ShutdownConnection(sock
, TRUE
))
84 LogEvent(L
"Echo: Connection is down", 0, 0, LOG_FILE
);
88 LogEvent(L
"Echo: Connection shutdown failed", 0, 0, LOG_FILE
);
92 LogEvent(L
"Echo: Terminating thread", 0, 0, LOG_FILE
);