sockets - Is there a connection limitation using LWIP? -
i'm writing tcp server using lwip protocol, server can connect multiple tcp clients. have problem , server can't connect more 3 active connections. 4th or more clients not connect unknown reasons. looked around forums , tried increasing number limit memp_num_tcp_pcb
in opt.h (lwip-x86_64\include\lwip\opt.h) didn't help.
following code, , questions whether there max connection limitation using lwip ? maximum number of connections 3 or more?
//server code #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define myport 1234 // port users connecting #define backlog 2// how many pending connections queue hold #define buf_size 1024 int fd_a[backlog]; // accepted connection fd int conn_amount; // current connection amount void showclient() { int i; printf("-----> client amount: %d\n", conn_amount); (i = 0; < backlog; i++) { printf("backlog%d ---> fd = %d\n", i, fd_a[i]); } printf("\n\n"); } int main(void) { sleep(1); printf("start server\n"); int sock_fd, new_fd; // listen on sock_fd, new connection on new_fd struct sockaddr_in server_addr; // server address information struct sockaddr_in client_addr; // connector's address information socklen_t sin_size; int yes = 1; char buf[buf_size]; int ret; int i; if ((sock_fd = socket(af_inet, sock_stream, 0)) == -1) { perror("socket"); exit(1); } //if (setsockopt(sock_fd, sol_socket, so_reuseaddr, &yes, sizeof(int)) == -1) { /*if (setsockopt(sock_fd, sol_socket, so_reuseaddr, &yes, sizeof(int)) == -1) { //perror("setsockopt"); //exit(1); } if (fcntl(sock_fd, f_setfl, o_nonblock) == -1) { printf("set server socket nonblock failed\n"); exit(1); }*/ memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = af_inet; // host byte order server_addr.sin_port = htons(myport); // short, network byte order server_addr.sin_addr.s_addr = htonl(inaddr_any); // automatically fill ip //memset(server_addr.sin_zero, '0', sizeof(server_addr.sin_zero)); if (bind(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) { perror("bind"); exit(1); } printf("listen...\n"); if (listen(sock_fd, backlog) == -1) { perror("listen"); exit(1); } printf("listen port %d\n", myport); fd_set fdsr, wfds, efds; int maxsock; struct timeval tv; conn_amount = 0; sin_size = sizeof(client_addr); maxsock = sock_fd; while (1) { sleep(1); // it's necessary // initialize file descriptor set fd_zero(&fdsr); fd_zero(&wfds); //fd_zero(&efds); fd_set(sock_fd, &fdsr); // add fd fd_set(sock_fd, &wfds); // add fd //fd_set(sock_fd, &efds); // add fd // timeout setting tv.tv_sec = 30; tv.tv_usec = 0; // add active connection fd set (i = 0; < backlog; i++) { if (fd_a[i] != 0) { fd_set(fd_a[i], &fdsr); } } //printf("before select!!!!!!!!!!!! ret = %d\n", ret); if ((select(maxsock + 1, &fdsr, &wfds, (fd_set*) 0, (struct timeval*) 0)) < 0) { perror("select"); break; } // check every fd in set (i = 0; < conn_amount; i++) { if (fd_isset(fd_a[i], &fdsr)) // check fd ready { ret = recv(fd_a[i], buf, sizeof(buf), 0); if (ret <= 0) { // client close printf("ret : %d , client[%d] close\n", ret, i); close(fd_a[i]); fd_clr(fd_a[i], &fdsr); // delete fd fd_a[i] = 0; conn_amount--; } else { // receive data if (ret < buf_size) memset(&buf[ret], '\0', 1); // add null('/0') printf("client[%d] send:%s\n", i, buf); } } } // check whether new connection comes if (fd_isset(sock_fd, &fdsr)) // accept new connection { new_fd = accept(sock_fd, (struct sockaddr *)&client_addr, &sin_size); if (new_fd <= 0) { perror("accept"); continue; } // add fd queue if (conn_amount < backlog) { fd_a[conn_amount++] = new_fd; printf("------> new connection client[%d] %s:%d\n", conn_amount, inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); if (new_fd > maxsock) // update maxsock fd select function maxsock = new_fd; } else { printf("max connections arrive, exit\n"); send(new_fd, "bye", 4, 0); close(new_fd); break; } } //showclient(); } // close other connections (i = 0; < backlog; i++) { if (fd_a[i] != 0) { close(fd_a[i]); } } exit(0); } //client code #include <sys/stat.h> #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <malloc.h> #include <netdb.h> #include <fcntl.h> #include <unistd.h> #include <netinet/in.h> #include <arpa/inet.h> #include <string.h> #define res_length 10240 int connect_socket(char * server,int serverport); int send_msg(int sockfd,char * sendbuff); char * recv_msg(int sockfd); int close_socket(int sockfd); int main(int argc, char ** argv) { int sockfd[1024] = {0}; char sendmsg[30]="zhangchengfei\r\n\r"; char* res; int port = 1234; char ip[128] = {0}; strncpy(ip, "10.107.19.62", 128); if(argc > 2) { strncpy(ip, argv[1], 128); port = atoi(argv[2]); printf("input ip: %s, port : %d\n", ip, port); } else if(argc > 1) { //port = atoi(argv[1]); //printf("input port : %d\n", port); // int num_client = atoi(argv[1]); int = 0; (i = 0; <= num_client; i++) { printf("start connect %d\n", + 1); sockfd[i]=connect_socket(ip, port); printf("connect %d ok\n", + 1); getchar(); } (i = 0; <= num_client; i++) { send_msg(sockfd[i], sendmsg); /* res=recv_msg(sockfd); */ printf("client %d send msg = %s\n", + 1, sendmsg); //printf(res); //free(res); getchar(); } (i = 0; <= num_client; i++) { close_socket(sockfd[i]); printf("close socket %d\n", + 1); } } return 0; } int connect_socket(char * server,int serverport){ int sockfd=0; struct sockaddr_in addr; struct hostent * phost; if((sockfd=socket(af_inet,sock_stream,0))<0){ herror("init socket error!"); return -1; } bzero(&addr,sizeof(addr)); addr.sin_family = af_inet; addr.sin_port = htons(serverport); addr.sin_addr.s_addr = inet_addr(server); if(addr.sin_addr.s_addr == inaddr_none){ phost = (struct hostent*)gethostbyname(server); if(phost==null){ herror("init socket s_addr error!"); return -1; } addr.sin_addr.s_addr =((struct in_addr*)phost->h_addr)->s_addr; } if(connect(sockfd,(struct sockaddr*)&addr, sizeof(addr))<0) { perror("connect server fail!"); return -1; } else return sockfd; } int send_msg(int sockfd,char * sendbuff) { int sendsize=0; if((sendsize=send(sockfd,sendbuff,strlen(sendbuff),0))<=0){ herror("send msg error!"); return -1; }else return sendsize; } char* recv_msg(int sockfd){ char * response; int flag=0,reclenth=0; response=(char *)malloc(res_length); memset(response,0,res_length); for(flag=0;;) { printf("======recv data:\n"); if(( reclenth=recv(sockfd,response+flag,res_length-flag,0))==-1 ) { free(response); printf("return value : %d\n", reclenth); perror("recv msg error : "); return null; } else if(reclenth==0) break; else { printf("%d char recieved data : %s.\n", reclenth, response+flag); flag+=reclenth; reclenth=0; } } printf("return value : %d\n", reclenth); response[flag]='0'; return response; } int close_socket(int sockfd) { close(sockfd); return 0; }
i have solved problem caused makefile
bug in ./stubdom
, after executing make crossclean
, found .o
files still existed in lwip-x86_64
folder. add command find . -name "*.o" | xargs rm -f
in makefile , ok.
the reason lwip configure given macro definition, macro definition have been replaced @ pre-compilation stage, if modify lwip configure , did not delete .o
files, there no effect. following macro definitions have modified in opt.h
.
memp_num_tcp_pcb 100 memp_num_tcp_pcb_listen 100 memp_num_netconn 100
now server can break through connection limitations , more 3 active tcp connections can established.
Comments
Post a Comment