diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..0f5b5ae --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "select.h": "c" + } +} \ No newline at end of file diff --git a/main b/main index d9a096f..1a9e150 100755 Binary files a/main and b/main differ diff --git a/server.c b/server.c index 38beec9..8e7a212 100644 --- a/server.c +++ b/server.c @@ -17,7 +17,7 @@ #define max(a,b) (((a) > (b)) ? (a) : (b)) #endif -int fd,errcode, newfd, afd=0, second_tcp_fd = 0; +int fd,errcode, newfd, client_sockets[5]; int maxfd, counter; ssize_t n; fd_set rfds; @@ -26,7 +26,6 @@ struct addrinfo hints,*res; struct sockaddr_in addr; char buffer[128]; -typedef enum {idle, busy} state; void createServer(char*port) { @@ -56,72 +55,67 @@ void createServer(char*port) { if(listen(fd,5)==-1)/*error*/exit(1); - state state_first_tcp; - state state_second_tcp; - - state_first_tcp = idle; - while(1) { + // Clear all sockets to set FD_ZERO(&rfds); + + // Add the main socket to set FD_SET(fd, &rfds); maxfd = fd; - if(state_first_tcp == busy) { - FD_SET(afd, &rfds); - maxfd = max(maxfd, afd); - } - - if(state_second_tcp == busy) { - FD_SET(second_tcp_fd, &rfds); - maxfd = max(maxfd, second_tcp_fd); + // Add all available child sockets to set + for (int i = 0; i < 5; i++) { + newfd = client_sockets[i]; + // If there are available sockets + if(newfd > 0) { + FD_SET(newfd, &rfds); + } + // Get the highest file descriptor number + if(newfd > maxfd) { + maxfd = newfd; + } } + counter = select(maxfd + 1, &rfds, (fd_set*)NULL, (fd_set*)NULL, (struct timeval *)NULL); if(counter <= 0) exit(1); + // If something is happening in the main socket, it means there's a new connection if(FD_ISSET(fd, &rfds)) { addrlen = sizeof(addr); if((newfd = accept(fd, (struct sockaddr*)&addr, &addrlen)) == -1) exit(1); - switch(state_first_tcp) { - case idle: - afd = newfd; - state_first_tcp = busy; - break; - case busy: - if(state_second_tcp == idle) { - second_tcp_fd = newfd; - state_second_tcp = busy; - break; - } - close(newfd); - break; - } - } + //inform user of socket number - used in send and receive commands + printf("New connection , socket fd is %d , ip is : %s , port : %d \n" , newfd , inet_ntoa(addr.sin_addr) , ntohs (addr.sin_port)); - if(FD_ISSET(afd, &rfds)) { - if((n = read(afd, buffer, 128)) != 0) { - if(n==-1) exit(1); - write(1,"received: ",10);write(1,buffer,n); // Print incoming message - n=write(afd,"Server Response\n",n); - } else { - printf("\n\n\n\nCLLLOOSSEEE\n\n\n\n"); - close(afd); - state_first_tcp = idle; + for(int i = 0; i < 5; i++) { + if(client_sockets[i] == 0) { + client_sockets[i] = newfd; + break; + } + if(i == 4) { + close(newfd); } + } } - if(FD_ISSET(second_tcp_fd, &rfds)) { - if((n = read(second_tcp_fd, buffer, 128)) != 0) { - if(n==-1) exit(1); - write(1,"received: ",10);write(1,buffer,n); // Print incoming message - n=write(second_tcp_fd,buffer,n); + //Else its on some other socket + for(int i = 0; i < 5; i++) { + newfd = client_sockets[i]; + if(FD_ISSET(newfd, &rfds)) { + if((n = read(newfd, buffer, 128)) == 0) { + close(newfd); + printf("Connection closed %d\n\n", i); + client_sockets[i] = 0; } else { - close(second_tcp_fd); - state_second_tcp = idle; + buffer[n] = '\0'; + write(1,"received: ",10); write(1,buffer,n); // Print incoming message + n = write(newfd,"Server Response\n",n); + if(n==-1)/*error*/exit(1); } + } } - } + freeaddrinfo(res); close(fd); } diff --git a/tester b/tester index 080dd0f..b80002e 100755 Binary files a/tester and b/tester differ diff --git a/tester.c b/tester.c index d4a5d6c..9855815 100644 --- a/tester.c +++ b/tester.c @@ -10,7 +10,7 @@ #include #include #include -#define PORT "58006" +#define PORT "58012" #ifndef max #define max(a,b) (((a) > (b)) ? (a) : (b)) @@ -46,7 +46,7 @@ void main() { n=read(fd,buffer,128); if(n==-1)/*error*/exit(1); - write(1,"Teste: ",6); write(1,buffer,n); + write(1,"Teste: ",6); write(1,buffer,n); write(1, "\n", 3); } freeaddrinfo(res);