Build a backend server: service-side code (asynchronous, large concurrency)

Source: Internet
Author: User
Tags bind continue include printf socket win32
#include <stdio.h>
#include <time.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
07.
#ifndef WIN32
#include <unistd.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#else
#include <WinSock2.h>
#define CLOSE (f) closesocket (f)
#endif
19.
20.struct my_event_s
21.{
int FD;
-Char recv[64];
Char send[64];
25.
-int rc_pos;
an. int sd_pos;
28.};
29.
30.int Main (int argc, char** argv)
31.{
int port;
-Flag int;
size int;
Sock_server int;
int sock_client;
time_t current;
time_t last;
int num;
E_num int;
My_empty_index int;
I,j int;
Event_flag int;
#define EPOLL_MAX 51200
struct epoll_event wait_events[Epoll_max];
struct my_event_s my_event[Epoll_max];
struct my_event_s* tobe_myevent;
struct Epoll_event tobe_event;
EPFD int;
50.
#define RECV_BUF_LEN 256
buffer[Char Recv_buf_len];
53.
struct sockaddr_in addr_server;
struct sockaddr_in addr_client;
56.
if (argc <= 1)
58. {
-printf ("Please set your port\n");
return 0;
61.}
32K
-printf ("Your port:%s\n", argv[1]);
64.
#ifdef WIN32
Wsadata Wsadata;
Flag = WSAStartup (0x101, &wsadata);
if (flag)
69. {
printf ("Your Windows Socket Setup wrong\n");
return 0;
72.}
73.
#endif.
75.
Port = atoi (argv[1]);
77.
addr_server.sin_family = af_inet;
Addr_server.sin_port = htons (port);
ADDR_SERVER.SIN_ADDR.S_ADDR = htonl (Inaddr_any);
81.
82.
83.
Sock_server = socket (af_inet, sock_stream, 0);
Flag = Fcntl (sock_server, F_GETFL, 0);
Fcntl (Sock_server, F_SETFL, flag | O_nonblock);
87.
Flag = bind (sock_server, struct sockaddr*) &addr_server, sizeof (struct sockaddr));
if (Flag < 0)
90. {
printf ("Your bind is not ok\n");
A. Close (Sock_server);
A. return 0;
94.}
95.
Flag = Listen (sock_server, 1024);
if (Flag < 0)
98. {
-printf ("Your listen is not ok\n");
-Close (Sock_server);
A. return 0;
102.}
103.
104.
EPFD = Epoll_create (Epoll_max);
if (EPFD <= 0)
107. {
108. printf ("Event module could not being setup\n");
109. Close (Sock_server);
0;
111.}
112.
113. tobe_event.events = Epollin;
114. TOBE_EVENT.DATA.FD = Sock_server;
115.
116. Epoll_ctl (EPFD, Epoll_ctl_add, Sock_server, &tobe_event);
117.
118.
119. Size = sizeof (addr_client);
num = 0;
121. Last = 0;
122. My_empty_index = 0;
123.
124. while (1)
125. {
126. #define WAIT_TIME_OUT 600
127. E_num = Epoll_wait (EPFD, wait_events, Epoll_max, wait_time_out);
128. If (e_num <= 0)
129. {
130. Continue;
131.}
132.
for (i = 0; i < e_num; ++i)
134. {
135. if (Sock_server = = wait_events[I].data.fd)
136. {
137. while (1)
138. {
139. Sock_client = Accept (sock_server, struct sockaddr*) &addr_client, (socklen_t*) &size);
140. If (Sock_client < 0)
141. {
if (errno = = Eagain)
143. {
144. Break;
145.}
146. if (errno = = eintr)
147. {
148. Continue;
149.}
The break;
151.}
152.
153. tobe_myevent = my_event + my_empty_index;
154. memset (tobe_myevent, 0, sizeof (struct my_event_s));
TOBE_MYEVENT-&GT;FD = sock_client;
156.
157. Flag = Fcntl (sock_client, F_GETFL, 0);
158 fcntl (Sock_client, F_SETFL, flag | O_nonblock);
159.
160. tobe_event.events = Epollin | Epollet;
161. tobe_event.data.u32 = My_empty_index;
162.
163. Epoll_ctl (EPFD, Epoll_ctl_add, Sock_client, &tobe_event);
164.
165.
166. for (j = My_empty_index + 1; j < Epoll_max; ++j)
167. {
if (!my_event[J].FD)
169. {
170. My_empty_index = J;
171. Break;
172.}
173.}
174.
175. if (MY_EVENT[J].FD)
176. {
177. for (j = 0; j < Epoll_max; ++j)
178. {
179. If (!my_event[J].FD)
180. {
181. My_empty_index = J;
A. break;
183.}
184.}
185.
186. If (my_event[J].FD)
187. {
188. printf ("Your events has been none else\n");
189. Close (sock_client);
190. Close (Sock_server);
191. return 0;
192.}
193.}
194.
195. ++num;
196. Current = time (0);
197. If (current > last)
198. {
199. printf ("Last SEC qps:%d\n", num);
num = 0;
201. Last = current;
202.}
203.
204. memcpy (tobe_myevent->send,¤t, sizeof (time_t));
205.
206. Flag = recv (Sock_client, TOBE_MYEVENT-&GT;RECV, 64, 0);
207. If (Flag < 64)
208. {
209. If (Flag > 0)
210. Tobe_myevent->rc_pos + = flag;
211. Continue;
212.}
213.
214. if (tobe_myevent->recv[31] | | | tobe_myevent->recv[63])
215. {
216. printf ("Your recv does follow the protocal\n");
217. TOBE_MYEVENT-&GT;FD = 0;
218. Close (sock_client);
219. Continue;
220.}
221.
222.
223. Flag = Send (sock_client, tobe_myevent->send, sizeof (time_t), 0);
224. if (Flag < sizeof (time_t))
225. {
tobe_event.events = Epollet | Epollout;
Epoll_ctl (EPFD, Epoll_ctl_mod, Sock_client, &tobe_event);
228. if (Flag > 0)
229. Tobe_myevent->sd_pos + = flag;
230. Continue;
231.}
232. TOBE_MYEVENT-&GT;FD = 0;
233. Close (sock_client);
234.
235.}
236.
237.}
238. Else
239. {
Tobe_myevent = my_event + wait_events[i].data.u32;
241. sock_client = tobe_myevent->fd;
242. Event_flag = wait_events[I].events;
243.
244. if (Event_flag & Epollhup)
245. {
246. TOBE_MYEVENT-&GT;FD = 0;
247. Close (sock_client);
248. Continue;
249.}
. else if (Event_flag & Epollerr)
251. {
252. tobe_myevent->fd = 0;
253. Close (sock_client);
254. Continue;
255.}
256. else if (Event_flag & Epollout)
257. {
258. if (Tobe_myevent->rc_pos!= 64)
259. {
Continue;
261.}
262.
263. If (tobe_myevent->sd_pos >= sizeof (time_t))
264. {
265. tobe_myevent->fd = 0;
266. Close (sock_client);
267. Continue;
268.}
269.
270. Flag = Send (sock_client, tobe_myevent->send + tobe_myevent->sd_pos, sizeof (time_t)-Tobe _myevent->sd_pos, 0);
271. If (Flag < 0)
272. {
273. if (errno = = Eagain)
274. {
275. Continue;
276.}
277. else if (errno = = eintr)
278. {
279. Continue;
280.}
281. tobe_myevent->fd = 0;
282. Close (sock_client);
283. Continue;
284.}
285.
286. if (flag >0)
287. {
Tobe_myevent->sd_pos + = flag;
289. if (tobe_myevent->sd_pos >= sizeof (time_t))
290. {
291. tobe_myevent->fd = 0;
292. Close (sock_client);
293. Continue;
294.}
295.}
296.}
297. if (Event_flag & Epollin)
298. {
299. if (Tobe_myevent->rc_pos < 64)
300. {
Flag = recv (sock_client, Tobe_myevent->recv + Tobe_myevent->rc_pos, 64-tobe_myevent-& Gt;rc_pos, 0);
302. If (flag <= 0)
303. {
304. Continue;
305.}
306.
307. Tobe_myevent->rc_pos + = flag;
308.
309. if (Tobe_myevent->rc_pos < 64)
310. {
311. Continue;
312.}
313.
if (tobe_myevent->recv[31] | | tobe_myevent->recv[63])
315. {
316. printf ("Your recv does follow the protocal\n");
317. TOBE_MYEVENT-&GT;FD = 0;
318. Close (sock_client);
319. Continue;
320.}
321.
322. Flag = Send (sock_client, tobe_myevent->send, sizeof (time_t), 0);
323. if (Flag < sizeof (time_t))
324. {
325. if (Flag > 0)
326. Tobe_myevent->sd_pos + = flag;
327. tobe_event.events = Epollet | Epollout;
328. tobe_event.data.u32 = wait_events[i].data.u32;
329. Epoll_ctl (EPFD, Epoll_ctl_mod, Sock_client, &tobe_event);
Continue;
331.}
332. tobe_myevent->fd = 0;
333. Close (sock_client);
334.}
335.
336.}
337.}
338.
339.
340.}
341.
342.}
343. printf ("Close Server connection\n");
344. Close (Sock_server);
345.
346. return 0;
347.
348.}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Servers/zs/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.