forked from kerasking/KendyNet
/
tcp_echoserver.c
45 lines (37 loc) · 989 Bytes
/
tcp_echoserver.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
#include <stdio.h>
#include "kendynet.h"
#include "kn_time.h"
#include "session.h"
void on_accept(kn_fd_t s,void *ud){
printf("on_accept\n");
kn_proactor_t p = (kn_proactor_t)ud;
struct session *session = calloc(1,sizeof(*session));
session->s = s;
kn_fd_setud(s,session);
kn_proactor_bind(p,s,transfer_finish);
session_recv(session);
++client_count;
printf("%d\n",client_count);
}
int main(int argc,char **argv){
kn_net_open();
kn_proactor_t p = kn_new_proactor();
kn_sockaddr local;
kn_addr_init_in(&local,argv[1],atoi(argv[2]));
kn_listen(p,&local,on_accept,(void*)p);
uint64_t tick,now;
tick = now = kn_systemms64();
while(1){
kn_proactor_run(p,50);
now = kn_systemms64();
if(now - tick > 1000)
{
uint32_t elapse = (uint32_t)(now-tick);
totalbytes = (totalbytes/elapse)/1024;
printf("client_count:%d,totalbytes:%lldMB/s\n",client_count,totalbytes);
tick = now;
totalbytes = 0;
}
}
return 0;
}