|
in main.c
........
for (int i=0;i<get_max_threads();i++){
args.id=i;
pthread_create(threads_tab+i,NULL,worker,&args);
}
.........
in udp.h
..........
struct worker_args {
int id;
int * fd_inet4;
int * fd_inet6;
char ** buffer_udp;
struct sockaddr ** addr_tab;
char ** ip_from_chbfr;
};
void * worker(void * args){
pthread_detach(pthread_self());
if (args == NULL){
printf("[E] Crash! \n");
exit(0);
}
int id=( ( (struct worker_args *)args ) -> id);
int * fd_inet4 = ( ( (struct worker_args *)args ) -> fd_inet4);
int * fd_inet6 = ( ((struct worker_args *)args) -> fd_inet6);
char ** buffer_udp = ( ((struct worker_args *)args) -> buffer_udp);
struct sockaddr ** addr_tab = ( ((struct worker_args *)args) -> addr_tab);
char ** ip_from_chbfr = ( ((struct worker_args *)args) -> ip_from_chbfr);
printf("[I] Worker %d is listener ! \n",id );
for(;;){
memset(buffer_udp[id],0,512);
int size=sizeof(struct sockaddr_in);
unsigned int got_len=recvfrom(fd_inet4[id],buffer_udp[id],512,MSG_DONTWAIT,addr_tab[id],(socklen_t *)&size
sendto(fd_inet4[id],buffer_udp[id],got_len,0,addr_tab[id],size);
}
return NULL;
}
.............
fd_inet4 , buffer_UDP, addr-tab 按照 id 取值,id是用for循环获得(id=1,2,3,4,5,6,0)
八个线程同时recvfrom八个socket 绑定在同一个ip:port udp,使用reuseport获得任务负载均衡
已知问题:加上其他源码,在android ndk中编译通过,但是启动后用netcat一连,发送几个数据包,多次重连后没有了返回
编译为单线程啥事没有
|
|