6 DWORD WINAPI
EchoHandler(VOID
* Sock_
)
9 SOCKET Sock
= (SOCKET
)Sock_
;
11 if (!EchoIncomingPackets(Sock
)) {
12 _tprintf(_T("Echo incoming packets failed\n"));
16 _tprintf(_T("Shutting connection down...\n"));
17 if (ShutdownConnection(Sock
, TRUE
)) {
18 _tprintf(_T("Connection is down.\n"));
22 _tprintf(_T("Connection shutdown failed\n"));
25 _tprintf(_T("Terminating thread\n"));
33 BOOL
EchoIncomingPackets(SOCKET Sock
)
35 TCHAR ReadBuffer
[BUF
];
41 ReadBytes
= recv(Sock
, ReadBuffer
, BUF
, 0);
44 _tprintf(_T("Received %d bytes from client\n"), ReadBytes
);
47 while (SentBytes
< ReadBytes
)
49 Temp
= send(Sock
, ReadBuffer
+ SentBytes
,
50 ReadBytes
- SentBytes
, 0);
53 _tprintf(_T("Sent %d bytes back to client\n"), Temp
);
56 else if (Temp
== SOCKET_ERROR
)
60 /* Client closed connection before we could reply to
61 all the data it sent, so quit early. */
62 _tprintf(_T("Peer unexpectedly dropped connection!\n"));
67 else if (ReadBytes
== SOCKET_ERROR
)
70 } while (ReadBytes
!= 0);
72 _tprintf(("Connection closed by peer.\n"));