-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
145 lines (115 loc) · 3.76 KB
/
server.cpp
File metadata and controls
145 lines (115 loc) · 3.76 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "read_config.h"
#include "common.h"
#include "server_config.h"
#include "client_config.h"
#include "logger.h"
#include <signal.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int count = 0;
static int num_pid;
static int* port;
static int* ServerID;
static Statistics *sta;
int *SIGNAL;
char tmp_str[2000];
void output_log(char *content){
if(*SIGNAL == 1){
cout<<"-------------------------------------------------------------------------------"<<endl;
cout<<content<<endl;
cout<<"-------------------------------------------------------------------------------"<<endl;
}
}
void dg_echo(int sockfd, SA *pcliaddr, socklen_t clilen,int servernumber)
{
socklen_t len;
char mesg[MAXLINE];
int size = 7000;
struct sockaddr_in cliaddr;
bzero(&cliaddr, sizeof(cliaddr));
time_t timer =time(NULL);
setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
for ( ; ; ) {
len = clilen;
t_msg clientmsg;
recvfrom(sockfd, &clientmsg, MAXLINE, 0, pcliaddr, &len);
sta->msg_count++;
sprintf(tmp_str,"server id = %d is dealing the request\n",ServerID[servernumber]);
output_log(tmp_str);
srand((unsigned)time(0));
int ran_num = (rand() % (30))+ 1;
sprintf(tmp_str,"server will sleep %ds to test choose another server\nServer id %d is accepted.",ran_num,ServerID[servernumber]);
output_log(tmp_str);
sprintf(tmp_str,"client id:%d message type:%d client destination id:%d ",clientmsg.dst_id,clientmsg.msg_type,clientmsg.src_id,ServerID[servernumber]);
output_log(tmp_str);
if(clientmsg.dst_id != ServerID[servernumber]){
sta->msg_wrong++;
cout<<"BL msg abondon "<<endl;
}
else {
sta->msg_true++;
//sleep(ran_num);
strcpy(clientmsg.data,ctime(&timer));
int n = sendto(sockfd, &clientmsg, MAXLINE, 0, pcliaddr, len);
sta->msg_answer++;
if (n < 0){
sprintf(tmp_str,"sendto %d Error",n);
output_log(tmp_str);
}
count++;
}
}
}
void when_alarm(int signo)
{
sprintf(tmp_str,"服务器接收到 %d 条消息,正确的 %d 条,错误的 %d 条,应答的 %d 条信息!\n",sta->msg_count,sta->msg_true,sta->msg_wrong,sta->msg_answer);
output_log(tmp_str);
}
int main(int argc,char **argv){
int sockfd,forid;
int x, shmid,shmid_log;
listener li;
//Listen the logger
//---------------
if((shmid_log = shmget(IPC_PRIVATE,sizeof(int),IPC_CREAT|0666))<0)
printf("shmget error\n"),exit(1);
if((SIGNAL = (int*)shmat(shmid_log,0,0)) == (int *)-1)
printf("shmat error\n"),exit(1);
*SIGNAL = 1;
li.create_listen_thread(SIGNAL);
//---------------
struct sockaddr_in servaddr, cliaddr;
//Statistics message
//--------------------
if((shmid = shmget(IPC_PRIVATE,sizeof(int),IPC_CREAT|0666))<0)
printf("shmget error\n"),exit(1);
if((sta = (Statistics*)shmat(shmid,0,0)) == (Statistics *)-1)
printf("shmat error\n"),exit(1);
//-------------------
sta->msg_answer=sta->msg_count=sta->msg_true=sta->msg_wrong=0;
server_config *se = new server_config();
se->deal_data();
num_pid = se->get_num_pid();
port = se->get_port();
ServerID = se->get_id();
signal(SIGALRM,when_alarm);
for(int i=0; i<num_pid; i++){
if(forid=fork()>0){
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(port[i]);
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
sprintf(tmp_str,"the %d server.it's port:%d\n",i,port[i]);
output_log(tmp_str);
bind(sockfd, (SA *) &servaddr, sizeof(servaddr));
dg_echo(sockfd, (SA *) &cliaddr, sizeof(cliaddr),i);
}
}
while(1){
alarm(5);
pause();
}
return 0;
}