/*----------------------------------------------------------------------------*/ int RunServerThread() { int ret; int server_sockfd; int client_sockfd; struct sockaddr_in my_addr; struct sockaddr_in remote_addr; int sin_size; int do_accept; int opt_val = 1; ret = anssock_init(NULL); if (ret != 0) printf("init sock failed \n"); // end initialized memset(&my_addr, 0, sizeof(my_addr)); my_addr.sin_family = AF_INET; my_addr.sin_addr.s_addr = INADDR_ANY; my_addr.sin_port = htons(8089); if ((server_sockfd = anssock_socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf("socket error \n"); return 1; } if(anssock_setsockopt(server_sockfd, SOL_SOCKET, SO_REUSEPORT, &opt_val, sizeof(int)) < 0) { printf("set socket option failed \n"); } if (anssock_bind(server_sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) < 0) { printf("bind error \n"); return 1; } if (anssock_listen(server_sockfd, 2048) < 0) { printf("listen error \n"); return 1; } sin_size = sizeof(struct sockaddr_in); /* wait for incoming accept events */ /* create epoll descriptor */ int epoll_fd; epoll_fd = anssock_epoll_create(MAX_EVENTS); if (epoll_fd == -1) { printf("epoll_create failed \n"); return 1; } struct epoll_event ev; struct epoll_event events[MAX_EVENTS]; ev.events = EPOLLIN | EPOLLET; ev.data.fd = server_sockfd; if (anssock_epoll_ctl(epoll_fd, EPOLL_CTL_ADD, server_sockfd, &ev) == -1) { printf("epll_ctl:server_sockfd register failed"); anssock_close(server_sockfd); anssock_close(epoll_fd); return 1; } int nfds; struct sockaddr_in* pV4Addr = (struct sockaddr_in*)&my_addr; int ipAddr = pV4Addr->sin_addr.s_addr; char ipstr[INET_ADDRSTRLEN]; inet_ntop( AF_INET, &ipAddr, ipstr, INET_ADDRSTRLEN ); printf("open socket on ip:%s port: %d\n", ipstr, ntohs(my_addr.sin_port)); while (1) { nfds = anssock_epoll_wait(epoll_fd, events, MAX_EVENTS, -1); if (nfds == -1) { printf("start epoll_wait failed"); anssock_close(server_sockfd); anssock_close(epoll_fd); return 1; } int i; do_accept = 0; for (i = 0; i < nfds; i++) { int sockid = events[i].data.fd; if (sockid == server_sockfd) { //accept case do_accept = 1; } else { if (events[i].events & EPOLLERR) { //epoll error event int err; socklen_t len = sizeof(err); /* error on the connection */ anssock_epoll_ctl(epoll_fd, EPOLL_CTL_DEL, sockid, NULL); anssock_close(sockid); } if (events[i].events == EPOLLIN) { //epollin read and write int ret = HandleReadEvent(epoll_fd, events[i]); anssock_close(sockid); } if (events[i].events == EPOLLOUT) { //epollout write int LEN = strlen(http_200); anssock_send(sockid, http_200, LEN, 0); anssock_epoll_ctl(epoll_fd, EPOLL_CTL_DEL, sockid, NULL); anssock_close(sockid); } if (events[i].events == EPOLLHUP) { //remote close the socket anssock_epoll_ctl(epoll_fd, EPOLL_CTL_DEL, sockid, NULL); anssock_close(sockid); } } } if (do_accept) { while (1) { int c = anssock_accept(server_sockfd, NULL, NULL); if (c >= 0) { if (c >= MAX_FLOW_NUM) { printf("Invalid socket id %d.\n", c); exit(-1); } struct epoll_event ev; //accept connection and wait EPOLLIN EVENT ev.events = EPOLLIN | EPOLLET; ev.data.fd = c; anssock_epoll_ctl(epoll_fd, EPOLL_CTL_ADD, c, &ev); // printf("Socket %d registered.\n", c); } else { //c<0 /* printf("mtcp_accept() error %s\n", strerror(errno));*/ break; } } }//end if } anssock_close(server_sockfd); return 0; }
int main(int argc, char * argv[]) { int ret; int server_sockfd; int client_sockfd; struct sockaddr_in my_addr; struct sockaddr_in remote_addr; int sin_size; ret = anssock_init(NULL); if(ret != 0) printf("init sock failed \n"); memset(&my_addr,0,sizeof(my_addr)); my_addr.sin_family=AF_INET; my_addr.sin_addr.s_addr=INADDR_ANY; my_addr.sin_port=htons(8000); if((server_sockfd=anssock_socket(PF_INET,SOCK_STREAM, 0)) < 0) { printf("socket error \n"); return 1; } if (anssock_bind(server_sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr)) < 0) { printf("bind error \n"); return 1; } if (anssock_listen(server_sockfd, 5) < 0) { printf("listen error \n"); return 1; } sin_size=sizeof(struct sockaddr_in); int epoll_fd; epoll_fd=anssock_epoll_create(MAX_EVENTS); if(epoll_fd==-1) { printf("epoll_create failed \n"); anssock_close(server_sockfd); return 1; } struct epoll_event ev; struct epoll_event events[MAX_EVENTS]; ev.events=EPOLLIN; ev.data.fd=server_sockfd; if(anssock_epoll_ctl(epoll_fd,EPOLL_CTL_ADD,server_sockfd,&ev)==-1) { printf("epll_ctl:server_sockfd register failed"); anssock_close(server_sockfd); anssock_close(epoll_fd); return 1; } int nfds; printf("dpdk tcp server is running \n"); while(1) { nfds=anssock_epoll_wait(epoll_fd, events, MAX_EVENTS, -1); if(nfds==-1) { printf("start epoll_wait failed \n"); anssock_close(server_sockfd); anssock_close(epoll_fd); return 1; } else if(nfds == 0) { printf("epoll timeout \n"); continue; } int i; for(i = 0; i < nfds; i++) { if(events[i].data.fd==server_sockfd) { if((client_sockfd = anssock_accept(server_sockfd, (struct sockaddr *)&remote_addr,&sin_size)) < 0) { printf("accept client_sockfd failed \n"); anssock_close(server_sockfd); anssock_close(epoll_fd); return 1; } ev.events=EPOLLIN; ev.data.fd=client_sockfd; if(anssock_epoll_ctl(epoll_fd, EPOLL_CTL_ADD,client_sockfd,&ev)==-1) { printf("epoll_ctl:client_sockfd register failed \n"); anssock_close(server_sockfd); anssock_close(epoll_fd); return 1; } printf("accept client %s, family: %d, port %d \n",inet_ntoa(remote_addr.sin_addr), remote_addr.sin_family, remote_addr.sin_port); anssock_send(client_sockfd, "I have received your message.", 20, 0); } else { ret = dpdk_handle_event(events[i]); } } } return 0; }
int main(void) { int ret; int i = 0 ; int fd, recvfd = 0; int epfd; int data_num =0; char send_data[2048]; struct timeval start, end; struct sockaddr_in addr_in; struct sockaddr_in remote_addr; struct epoll_event event; char recv_buf[2038]; int recv_len; ret = anssock_init(NULL); if(ret != 0) printf("init sock ring failed \n"); /* create epoll socket */ epfd = anssock_epoll_create(0); if(epfd < 0) { printf("create epoll socket failed \n"); return -1; } fd = anssock_socket(AF_INET, SOCK_DGRAM, 0); if(fd < 0) { printf("create socket failed \n"); anssock_close(epfd); return -1; } memset(&addr_in, 0, sizeof(addr_in)); addr_in.sin_family = AF_INET; addr_in.sin_port = htons(8888); addr_in.sin_addr.s_addr = htonl(0x02020202); ret = anssock_bind(fd, (struct sockaddr *)&addr_in, sizeof(addr_in) ); if(ret != 0) { printf("bind socket failed \n"); anssock_close(fd); anssock_close(epfd); return -1; } memset(&remote_addr, 0, sizeof(remote_addr)); remote_addr.sin_family = AF_INET; remote_addr.sin_port = htons(9999); remote_addr.sin_addr.s_addr = htonl(0x02020205); event.data.fd = fd; event.events = EPOLLIN | EPOLLET; ret = anssock_epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event); if(ret != 0) { printf("epoll ctl failed \n"); anssock_close(fd); anssock_close(epfd); return -1; } printf("start dpdk udp application \n"); int event_num = 0; memset(send_data, 0, sizeof(send_data)); while(1) { event_num = anssock_epoll_wait (epfd, events, 20, -1); if(event_num <= 0) { printf("epoll_wait failed \n"); continue; } for(i = 0; i < event_num; i++) { if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN))) { printf("dpdk socket(%d) error\n", events[i].data.fd); anssock_close (events[i].data.fd); continue; } if (events[i].events & EPOLLIN) { while(1) { recv_len = anssock_recvfrom(events[i].data.fd, recv_buf, 2048, 0, NULL, NULL); if(recv_len > 0) { printf("Recv: %s \n", recv_buf); data_num++; sprintf(send_data, "Hello, linux_udp, num:%d !", data_num); anssock_sendto(events[i].data.fd, send_data, strlen(send_data) + 1, 0, (struct sockaddr *)&remote_addr, sizeof(remote_addr)); } else if(recv_len < 0) { if (errno == ANS_EAGAIN) { break; } else { printf("remote close the socket, errno %d \n", errno); anssock_close(events[i].data.fd); break; } } else { printf("remote close the socket, len %d \n", recv_len); anssock_close(events[i].data.fd); break; } } } else { printf("unknow event %x, fd:%d \n", events[i].events, events[i].data.fd); } } } anssock_close(fd); anssock_close(epfd); return 0; }