01.#include <stdio.h>02.#include <time.h>03.#include <fcntl.h>04.#include <stdlib.h>05.#include <errno.h>06.#include <string.h>07.08.#ifndef WIN32 09.#include <unistd.h>10.#include <sys/epoll.h>11.#include <sys/types.h>12.#include <sys/socket.h>13.#include <arpa/inet.h>14.#include <netinet/in.h>15.#else 16.#include <WinSock2.h>17.#define close( f ) closesocket( f ) 18.#endif 19.20.struct my_event_s 21.{22. int fd;23. char recv[64];24. char send[64];25.26. int rc_pos;27. int sd_pos;28.};29.30.int main( int argc, char** argv )31.{32. int port;33. int flag;34. int size;35. int sock_server;36. int sock_client;37. time_t current;38. time_t last;39. int num;40. int e_num;41. int my_empty_index;42. int i,j;43. int event_flag;44.#define EPOLL_MAX 51200 45. struct epoll_event wait_events[ EPOLL_MAX ];46. struct my_event_s my_event[ EPOLL_MAX ];47. struct my_event_s* tobe_myevent;48. struct epoll_event tobe_event;49. int epfd;50.51.#define RECV_BUF_LEN 256 52. char buffer[ RECV_BUF_LEN ];53.54. struct sockaddr_in addr_server;55. struct sockaddr_in addr_client;56.57. if( argc <= 1 )58. {59. printf( "please set your port\n" );60. return 0;61. }62.63. printf( "your port:%s\n", argv[1] );64.65.#ifdef WIN32 66. WSADATA wsadata;67. flag = WSAStartup( 0x101, &wsadata );68. if( flag )69. {70. printf( "your windows socket setup wrong\n" );71. return 0;72. }73.74.#endif 75.76. port = atoi(argv[1]);77. 78. addr_server.sin_family = AF_INET;79. addr_server.sin_port = htons( port );80. addr_server.sin_addr.s_addr = htonl( INADDR_ANY );81.82.83.84. sock_server = socket( AF_INET, SOCK_STREAM, 0 );85. flag = fcntl( sock_server, F_GETFL, 0 );86. fcntl( sock_server, F_SETFL, flag | O_NONBLOCK );87.88. flag = bind( sock_server, ( struct sockaddr* )&addr_server, sizeof( struct sockaddr ) );89. if( flag < 0 )90. {91. printf( "your bind is not ok\n" );92. close( sock_server );93. return 0;94. }95.96. flag = listen( sock_server, 1024 );97. if( flag < 0 )98. {99. printf( "your listen is not ok\n");100. close( sock_server );101. return 0;102. }103.104. 105. epfd = epoll_create( EPOLL_MAX );106. if( epfd <= 0 )107. {108. printf( "event module could not be setup\n");109. close( sock_server );110. return 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 );120. 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.133. 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. {142. if( errno == EAGAIN )143. {144. break;145. }146. if( errno == EINTR )147. {148. continue;149. }150. break;151. }152.153. tobe_myevent = my_event + my_empty_index;154. memset( tobe_myevent, 0, sizeof( struct my_event_s ) );155. tobe_myevent->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. {168. 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;182. 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 );200. num = 0;201. last = current;202. }203.204. memcpy( tobe_myevent->send, ¤t, sizeof(time_t) );205.206. flag = recv( sock_client, tobe_myevent->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->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. {226. tobe_event.events = EPOLLET | EPOLLOUT;227. 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->fd = 0;233. close( sock_client );234.235. }236. 237. }238. else 239. {240. 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->fd = 0;247. close( sock_client );248. continue;249. }250. 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. {260. 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. {288. 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. {301. flag = recv( sock_client, tobe_myevent->recv + tobe_myevent->rc_pos, 64 - tobe_myevent->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.314. if( tobe_myevent->recv[31] || tobe_myevent->recv[63] )315. {316. printf( "your recv does follow the protocal\n");317. tobe_myevent->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 );330. 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.}
查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Servers/zs/
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索int
, include
, send recv
, time_t
, recv
, flag
, continue
close
,以便于您获取更多的相关知识。
时间: 2024-09-29 07:01:19