add the vista application recovery functions to w32api and add their stubs to kernel32
[reactos.git] / reactos / apps / utils / net / tcpsvcs / echo / echo.c
1 #include <stdio.h>
2 #include <winsock2.h>
3 #include <tchar.h>
4 #include "echo.h"
5 #include "../skelserver/skelserver.h"
6
7 DWORD WINAPI EchoHandler(VOID* Sock_)
8 {
9 DWORD Retval = 0;
10 SOCKET Sock = (SOCKET)Sock_;
11
12 if (!EchoIncomingPackets(Sock)) {
13 _tprintf(_T("Echo incoming packets failed\n"));
14 Retval = 3;
15 }
16
17 _tprintf(_T("Shutting connection down...\n"));
18 if (ShutdownConnection(Sock)) {
19 _tprintf(_T("Connection is down.\n"));
20 }
21 else
22 {
23 _tprintf(_T("Connection shutdown failed\n"));
24 Retval = 3;
25 }
26
27 return Retval;
28 }
29
30
31
32 BOOL EchoIncomingPackets(SOCKET Sock)
33 {
34 TCHAR ReadBuffer[BUF];
35 INT Temp;
36 INT ReadBytes;
37 INT SentBytes;
38
39 do {
40 ReadBytes = recv(Sock, ReadBuffer, BUF, 0);
41 if (ReadBytes > 0) {
42 _tprintf(_T("Received %d bytes from client\n"), ReadBytes);
43
44 SentBytes = 0;
45 while (SentBytes < ReadBytes) {
46 Temp = send(Sock, ReadBuffer + SentBytes,
47 ReadBytes - SentBytes, 0);
48 if (Temp > 0) {
49 _tprintf(_T("Sent %d bytes back to client\n"), Temp);
50 SentBytes += Temp;
51 }
52 else if (Temp == SOCKET_ERROR) {
53 return FALSE;
54 }
55 else {
56 /* Client closed connection before we could reply to
57 all the data it sent, so quit early. */
58 _tprintf(_T("Peer unexpectedly dropped connection!\n"));
59 return FALSE;
60 }
61 }
62 }
63 else if (ReadBytes == SOCKET_ERROR) {
64 return FALSE;
65 }
66 } while (ReadBytes != 0);
67
68 _tprintf(("Connection closed by peer.\n"));
69 return TRUE;
70 }
71