Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenecho.c
Go to the documentation of this file.
00001 /* 00002 * PROJECT: ReactOS simple TCP/IP services 00003 * LICENSE: GPL - See COPYING in the top level directory 00004 * FILE: /base/services/tcpsvcs/echo.c 00005 * PURPOSE: Returns whatever input the client sends 00006 * COPYRIGHT: Copyright 2005 - 2008 Ged Murphy <gedmurphy@reactos.org> 00007 * 00008 */ 00009 00010 #include "tcpsvcs.h" 00011 00012 #define RECV_BUF 1024 00013 00014 static BOOL 00015 EchoIncomingPackets(SOCKET sock) 00016 { 00017 CHAR readBuffer[RECV_BUF]; 00018 WCHAR logBuf[256]; 00019 INT totalSentBytes; 00020 INT readBytes; 00021 INT retVal; 00022 00023 do 00024 { 00025 readBytes = recv(sock, readBuffer, RECV_BUF, 0); 00026 if (readBytes > 0) 00027 { 00028 _swprintf(logBuf, L"Received %d bytes from client", readBytes); 00029 LogEvent(logBuf, 0, 0, LOG_FILE); 00030 00031 totalSentBytes = 0; 00032 while (!bShutdown && totalSentBytes < readBytes) 00033 { 00034 retVal = send(sock, readBuffer + totalSentBytes, readBytes - totalSentBytes, 0); 00035 if (retVal > 0) 00036 { 00037 _swprintf(logBuf, L"Sent %d bytes back to client", retVal); 00038 LogEvent(logBuf, 0, 0, LOG_FILE); 00039 totalSentBytes += retVal; 00040 } 00041 else if (retVal == SOCKET_ERROR) 00042 { 00043 LogEvent(L"Echo: socket error", WSAGetLastError(), 0, LOG_ERROR); 00044 return FALSE; 00045 } 00046 else 00047 { 00048 /* Client closed connection before we could reply to 00049 all the data it sent, so quit early. */ 00050 LogEvent(L"Peer unexpectedly dropped connection!", 0, 0, LOG_FILE); 00051 return FALSE; 00052 } 00053 } 00054 } 00055 else if (readBytes == SOCKET_ERROR) 00056 { 00057 LogEvent(L"Echo: socket error", WSAGetLastError(), 0, LOG_ERROR); 00058 return FALSE; 00059 } 00060 } while ((readBytes != 0) && (!bShutdown)); 00061 00062 if (!bShutdown) 00063 LogEvent(L"Echo: Connection closed by peer", 0, 0, LOG_FILE); 00064 00065 return TRUE; 00066 } 00067 00068 DWORD WINAPI 00069 EchoHandler(VOID* sock_) 00070 { 00071 DWORD retVal = 0; 00072 SOCKET sock = (SOCKET)sock_; 00073 00074 if (!EchoIncomingPackets(sock)) 00075 { 00076 LogEvent(L"Echo: EchoIncomingPackets failed", 0, 0, LOG_FILE); 00077 retVal = 1; 00078 } 00079 00080 LogEvent(L"Echo: Shutting connection down", 0, 0, LOG_FILE); 00081 00082 if (ShutdownConnection(sock, TRUE)) 00083 { 00084 LogEvent(L"Echo: Connection is down", 0, 0, LOG_FILE); 00085 } 00086 else 00087 { 00088 LogEvent(L"Echo: Connection shutdown failed", 0, 0, LOG_FILE); 00089 retVal = 1; 00090 } 00091 00092 LogEvent(L"Echo: Terminating thread", 0, 0, LOG_FILE); 00093 ExitThread(retVal); 00094 } Generated on Sat May 26 2012 04:16:38 for ReactOS by
1.7.6.1
|