Question
LwIP_HTTP_Server_Socket_RTOS More sockets opened at once
How to make more socket connectrions work simultaneously? Is it thread safe sockets Lwip made in demo. If I try to make 2 connectrions in two threads they seems to colidate. Its is not possible to keep two at same time. Thing like this does not work :
// Vlákno pro HTTP server
static void http_server_socket_thread(void *arg) {
int sock1, sock2, newconn;
struct sockaddr_in address, remotehost;
/* create TCP socket for port 80 */
if ((sock1 = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
return;
}
/* bind to port 80 at any interface */
address.sin_family = AF_INET;
address.sin_port = htons(PORT1);
address.sin_addr.s_addr = INADDR_ANY;
if (bind(sock1, (struct sockaddr *)&address, sizeof(address)) < 0) {
return;
}
/* listen for incoming connections (TCP listen backlog = 5) */
listen(sock1, 5);
/* create TCP socket for port 1028 */
if ((sock2 = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
return;
}
/* bind to port 1028 at any interface */
address.sin_port = htons(PORT2);
if (bind(sock2, (struct sockaddr *)&address, sizeof(address)) < 0) {
return;
}
/* listen for incoming connections (TCP listen backlog = 5) */
listen(sock2, 5);
while (1) {
fd_set fds;
int maxfd = (sock1 > sock2) ? sock1 : sock2;
FD_ZERO(&fds);
FD_SET(sock1, &fds);
FD_SET(sock2, &fds);
if (select(maxfd + 1, &fds, NULL, NULL, NULL) < 0) {
continue;
}
if (FD_ISSET(sock1, &fds)) {
// Accept connection on port 80
newconn = accept(sock1, (struct sockaddr *)&remotehost, (socklen_t *)&size);
http_server_serve(newconn);
}
if (FD_ISSET(sock2, &fds)) {
// Accept connection on port 1028
newconn = accept(sock2, (struct sockaddr *)&remotehost, (socklen_t *)&size);
http_server_serve(newconn);
}
}
}
// Inicializace HTTP serveru (spuštění vlákna)
void http_server_socket_init() {
sys_thread_new("http", http_server_socket_thread, NULL, DEFAULT_THREAD_STACKSIZE * 2, WEBSERVER_THREAD_PRIO);
}
