A simple Echo Server

The following simple Server opens a port on 2000 and waits for incoming connections. Each connection is answered with the same line as was written (echoed).
If you want to test the server, use telnet localhost 2000
 
From: http://www.adp-gmbh.ch/win/misc/sockets.html#echo
// EchoServer.cpp : Defines the entry point for the console application. // #include "Socket.h" #include <process.h> #include <string> DWORD WINAPI Answer(void* a) { Socket* s = (Socket*) a; while (1) { std::string r = s->ReceiveLine(); if (r.empty()) break; s->SendLine(r); } delete s; return 0; } int main(int argc, char* argv[]) { SocketServer in(2000,5); while (1) { Socket* s=in.Accept(); unsigned long ret; CreateThread(0,0,Answer,(unsigned long (__stdcall *)(void *))s,0,&ret); } return 0; }  
 
Dowload: http://download.csdn.net/source/1638196

你可能感兴趣的:(A simple Echo Server)