-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
200 lines (167 loc) · 5.91 KB
/
server.c
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include<stdlib.h>
#include<pthread.h>
#include<sys/socket.h>
#include<sys/types.h> //pthread_t , pthread_attr_t and so on.
#include<stdio.h>
#include<netinet/in.h> //structure sockaddr_in
#include<arpa/inet.h> //Func : htonl; htons; ntohl; ntohs
#include<assert.h> //Func :assert
#include<string.h> //Func :memset
#include<unistd.h> //Func :close,write,read
#define SOCK_PORT 11222
#define BUFFER_LENGTH 1024
#define MAX_CONN_LIMIT 512 //MAX connection limit
#define ERROR -1
#define TRUE 1
#include "memcached.h"
static void Data_handle(void * sock_fd); //Only can be seen in the file
int
format_request(uint32_t ip_addr, uint16_t port, char * buf, int buf_len);
int main()
{
int sockfd_server;
int sockfd;
int fd_temp;
struct sockaddr_in s_addr_in;
struct sockaddr_in s_addr_client;
int client_length;
sockfd_server = socket(AF_INET,SOCK_STREAM,0); //ipv4,TCP
assert(sockfd_server != -1);
memset(&s_addr_in,0,sizeof(s_addr_in));
s_addr_in.sin_family = AF_INET;
s_addr_in.sin_addr.s_addr = htonl(INADDR_ANY); //trans addr from uint32_t host byte order to network byte order.
s_addr_in.sin_port = htons(SOCK_PORT); //trans port from uint16_t host byte order to network byte order.
fd_temp = bind(sockfd_server,(struct scokaddr *)(&s_addr_in),sizeof(s_addr_in));
if(fd_temp == -1)
{
fprintf(stderr,"bind error!\n");
exit(1);
}
fd_temp = listen(sockfd_server,MAX_CONN_LIMIT);
if(fd_temp == -1)
{
fprintf(stderr,"listen error!\n");
exit(1);
}
while(1)
{
printf("waiting for new connection...\n");
pthread_t thread_id;
client_length = sizeof(s_addr_client);
//Block here. Until server accpets a new connection.
sockfd = accept(sockfd_server,(struct sockaddr_*)(&s_addr_client),(socklen_t *)(&client_length));
if(sockfd == -1)
{
fprintf(stderr,"Accept error!\n");
continue; //ignore current socket ,continue while loop.
}
//char data_recv[BUFFER_LENGTH];
//int tmpi = recv(sockfd, data_recv, BUFFER_LENGTH, 0);
//if (tmpi < 0) printf("==========================\n");
//else printf("Recv data is %s\n",data_recv);
printf("A new connection occurs!\n");
if(pthread_create(&thread_id,NULL,(void *)(&Data_handle),(void *)(&sockfd)) == -1)
{
fprintf(stderr,"pthread_create error!\n");
break; //break while loop
}
}
//Clear
int ret = shutdown(sockfd_server,SHUT_WR); //shut down the all or part of a full-duplex connection.
assert(ret != -1);
printf("Server shuts down\n");
return 0;
}
static void Data_handle(void * sock_fd)
{
int fd = *((int *)sock_fd);
int i_recvBytes;
char data_recv[BUFFER_LENGTH];
const char * data_send = "Server has received your request!\n";
while(1)
{
printf("waiting for request...\n");
//Reset data.
memset(data_recv,0,BUFFER_LENGTH);
//i_recvBytes = read(fd,data_recv,BUFFER_LENGTH);
i_recvBytes = recv(fd, data_recv, BUFFER_LENGTH, 0);
if(i_recvBytes == 0)
{
printf("Maybe the client has closed\n");
break;
}
if(i_recvBytes == -1) {
fprintf(stderr,"read error!\n");
break;
}
if(strcmp(data_recv,"quit")==0) {
printf("Quit command!\n");
break; //Break the while loop.
}
data_recv[i_recvBytes] = '\0';
printf("read from client : |%s|%d|\n|", data_recv, i_recvBytes);
int tmpi = 0;
for (; tmpi < i_recvBytes; tmpi++)
printf("%c", data_recv[tmpi]);
printf("|-----\n");
format_request(12334443, 4, data_recv, i_recvBytes);
tmpi = 0;
for (; tmpi < i_recvBytes; tmpi++)
printf("%c", data_recv[tmpi]);
printf("|-----\n");
uint32_t ipaddr; uint16_t port;
format_response(&ipaddr, &port, data_recv, i_recvBytes);
tmpi = 0;
for (; tmpi < i_recvBytes; tmpi++)
printf("%c", data_recv[tmpi]);
printf("|-----\n");
if(write(fd,data_send,strlen(data_send)) == -1) break;
}
//Clear
printf("terminating current client_connection...\n");
close(fd); //close a file descriptor.
pthread_exit(NULL); //terminate calling thread!
}
int
format_request(uint32_t ip_addr, uint16_t port, char * buf, int buf_len)
{
protocol_binary_request_header * header = (protocol_binary_request_header *)buf;
client_id * client = &(header->request.opaque);
uint16_t ip_rank;
if (buf_len < sizeof(protocol_binary_request_header))
return ERROR;
client = &(header->request.opaque);
/* check if is a request */
if (header->request.magic == PROTOCOL_BINARY_REQ) {
header->request.opcode = PROTOCOL_BINARY_CMD_GETK;
header->request.magic = PROTOCOL_BINARY_RES; /* --------------- */
//ip_rank = get_rank_from_ip(ip_addr);
ip_rank = 3; /* --------------- */
if (ip_rank == 0) {
return ERROR;
}
client->id.src_ip_rank = ip_rank;
client->id.src_port = port;
printf("[%d|%d]\n", client->id.src_ip_rank, client->id.src_port); /* --------------- */
return TRUE;
}
return ERROR;
}
int format_response(uint32_t *ip_addr, uint16_t *port, char *buf, int buf_len) {
protocol_binary_request_header * header = (protocol_binary_request_header *)buf;
client_id * client = &(header->request.opaque);
if (buf_len < sizeof(protocol_binary_request_header))
return ERROR;
client = &(header->request.opaque);
if (header->request.magic == PROTOCOL_BINARY_RES) {
*ip_addr = get_ip_from_rank(client->id.src_ip_rank);
//ip_addr = 12345678; /* --------------- */
*port = client->id.src_port;
printf("port[%d ip|%d]\n", *port, *ip_addr); /* --------------- */
return TRUE;
}
return ERROR;
}
int noname(char *buf) {
protocol_binary_request_header * header = (protocol_binary_request_header *)buf;
}