Repository navigation
Expand file tree
/
Copy pathServer.cpp
More file actions
91 lines (79 loc) · 1.97 KB
/
Copy pathServer.cpp
File metadata and controls
91 lines (79 loc) · 1.97 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* Server.cpp
*
* Created on: May 5, 2012
* Author: randy
*/
#include "Server.h"
#include "ServerPI.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <cstring>
#include <errno.h>
#include <stdio.h>
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
using namespace std;
#define IPV6_LISTEN_PORT 21
#define PENDING_CONNECTION_QUEUE_LEN 10
#define THREAD_NUM 20
void Server::initThread()
{
this->listenMutex = new boost::mutex;
for (int i = 0; i < THREAD_NUM; i++){
ServerPI* pi = new ServerPI(this->listenSocked, this->listenMutex);
boost::thread thrd(boost::bind(&ServerPI::begin, pi));
}
}
void Server::run() {
struct sockaddr_in6 sin6;
int s0;
memset(&sin6, 0, sizeof(sin6));
sin6.sin6_family = AF_INET6;
sin6.sin6_port = htons(IPV6_LISTEN_PORT);
int on = 1;
if ((s0 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP)) == -1){
printf("init socket error : %s\n", strerror(errno));
return;
}
if (setsockopt(s0, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))){
printf("setsockopt error : %s\n", strerror(errno));
}
if (bind(s0, (const struct sockaddr*) &sin6, sizeof(sin6))) {
printf("bind error : %s\n", strerror(errno));
return;
}
if (listen(s0, PENDING_CONNECTION_QUEUE_LEN)) {
printf("listen error\n");
return;
}
this->listenSocked = s0;
printf("init server successfully\n");
//init thread
this->initThread();
for(;;){
}
// for (;;) {
// memset(&hbuf, 0, sizeof(hbuf));
// sin6_len = sizeof(sin6_accept);
// printf("waiting for connection\n");
// s = accept(s0, (struct sockaddr*) &sin6_accept, &sin6_len);
// printf("accept a user\n");
// if (s == -1) {
// printf("accept failed from %s\n", hbuf);
// } else {
// getnameinfo((struct sockaddr*) &sin6_accept, sin6_len, hbuf,
// sizeof(hbuf), NULL, 0, NI_NUMERICHOST);
// printf("accepPt a connection from %s\n", hbuf);
// }
//
// }
}
Server::~Server()
{
for (unsigned int i = 0; i < pis.size(); i++){
delete pis[i];
}
}