-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpServer.cpp
More file actions
66 lines (53 loc) · 1.45 KB
/
httpServer.cpp
File metadata and controls
66 lines (53 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// 2002 by Ted T. Yuan.
//
#include <stdio.h>
#include <string.h>
#include <iostream>
#ifdef _DEBUG
#pragma warning (disable: 4786 4788)
#endif
#include <string>
#include <vector>
#include <NetworkService.h>
struct HttpHello : SPACE_YIN::NetworkService
{
const static char * hello;
HttpHello(std::string serviceId,
int lengthWaitList, int numThreads, unsigned short sourcePort)
: SPACE_YIN::NetworkService(
serviceId, lengthWaitList, numThreads, sourcePort) {}
void ServiceHandler(int socket)
{
char buffer[1024];
int count;
// we are simple minded...
if ( (count = recv(socket, buffer, 1024, 0)) > 0)
{
if(count < 1024) buffer[count] = 0;
std::cout << buffer << std::endl;
}
static volatile int nVisitors = 0;
sprintf(buffer, "%s, you are visitor#%d", hello, ++nVisitors);
send(socket, buffer, strlen(buffer), 0);
shutdown(socket, 1); //1 = SHUT_WR
CloseConnection(socket);
}
};
const char *HttpHello::hello =
"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\nHello World";
////////////////////////////////////////////////////
// launch a web server
void main()
{
initializeWinsock();
// use a browser to test the server, i.e., http://localhost:1234
HttpHello hh("Hello Web",
10, // server backlog
10, // number of worker threads
1234 // service port number
);
boost::thread thrd(hh);
thrd.join();
closeWinsock();
}