reactos/base/services/tcpsvcs/echo.c

95 lines
2.6 KiB
C
Raw Normal View History

/*
* PROJECT: ReactOS simple TCP/IP services
* LICENSE: GPL - See COPYING in the top level directory
* FILE: /base/services/tcpsvcs/echo.c
* PURPOSE: Returns whatever input the client sends
* COPYRIGHT: Copyright 2005 - 2008 Ged Murphy <gedmurphy@reactos.org>
*
*/
#include "tcpsvcs.h"
#define RECV_BUF 1024
static BOOL
EchoIncomingPackets(SOCKET sock)
{
CHAR readBuffer[RECV_BUF];
TCHAR logBuf[256];
INT totalSentBytes;
INT readBytes;
INT retVal;
do
{
readBytes = recv(sock, readBuffer, RECV_BUF, 0);
if (readBytes > 0)
{
_stprintf(logBuf, _T("Received %d bytes from client"), readBytes);
LogEvent(logBuf, 0, 0, LOG_FILE);
totalSentBytes = 0;
while (!bShutdown && totalSentBytes < readBytes)
{
retVal = send(sock, readBuffer + totalSentBytes, readBytes - totalSentBytes, 0);
if (retVal > 0)
{
_stprintf(logBuf, _T("Sent %d bytes back to client"), retVal);
LogEvent(logBuf, 0, 0, LOG_FILE);
totalSentBytes += retVal;
}
else if (retVal == SOCKET_ERROR)
{
LogEvent(_T("Echo: socket error"), WSAGetLastError(), 0, LOG_ERROR);
return FALSE;
}
else
{
/* Client closed connection before we could reply to
all the data it sent, so quit early. */
LogEvent(_T("Peer unexpectedly dropped connection!"), 0, 0, LOG_FILE);
return FALSE;
}
}
}
else if (readBytes == SOCKET_ERROR)
{
LogEvent(_T("Echo: socket error"), WSAGetLastError(), 0, LOG_ERROR);
return FALSE;
}
} while ((readBytes != 0) && (!bShutdown));
if (!bShutdown)
LogEvent(_T("Echo: Connection closed by peer"), 0, 0, LOG_FILE);
return TRUE;
}
DWORD WINAPI
EchoHandler(VOID* sock_)
{
DWORD retVal = 0;
SOCKET sock = (SOCKET)sock_;
if (!EchoIncomingPackets(sock))
{
LogEvent(_T("Echo: EchoIncomingPackets failed"), 0, 0, LOG_FILE);
retVal = 1;
}
LogEvent(_T("Echo: Shutting connection down"), 0, 0, LOG_FILE);
if (ShutdownConnection(sock, TRUE))
{
LogEvent(_T("Echo: Connection is down"), 0, 0, LOG_FILE);
}
else
{
LogEvent(_T("Echo: Connection shutdown failed"), 0, 0, LOG_FILE);
retVal = 1;
}
LogEvent(_T("Echo: Terminating thread"), 0, 0, LOG_FILE);
ExitThread(retVal);
}