Fixed segfault issue by using MAX_CONNECTIONS instead of 'sizeof'

master
Aadhavan Srinivasan 2 years ago
parent 423404826e
commit e5e3e8db72

@ -20,7 +20,7 @@ int main() {
socklen_t temp_addrlen;
fd_set read_fd_set;
int conn_sockets[MAX_CONNECTIONS] = {0};
int conn_sockets[MAX_CONNECTIONS] = {-1};
FD_ZERO(&read_fd_set);
char buffer[BUFFER_SIZE];
User* users = create_user_list("user_file.txt");
@ -33,7 +33,7 @@ int main() {
while (true) {
FD_ZERO(&read_fd_set);
for (int i=0; i < sizeof(conn_sockets); i++) {
for (int i=0; i < MAX_CONNECTIONS; i++) {
if (conn_sockets[i] > 0) {
FD_SET(conn_sockets[i],&read_fd_set);
}
@ -44,7 +44,7 @@ int main() {
if (num_conns > 0) {
if (FD_ISSET(conn_sockets[0],&read_fd_set)) {
int client_sock = accept(conn_sockets[0],&temp_addr,&temp_addrlen);
for (int i=0;i < sizeof(conn_sockets); i++) {
for (int i=0;i < MAX_CONNECTIONS; i++) {
if (conn_sockets[i] <= 0) {
conn_sockets[i] = client_sock;
break;
@ -52,13 +52,12 @@ int main() {
}
}
for (int i=1; i < sizeof(conn_sockets); i++) {
printf("Checking FD: %d\n",i);
for (int i=1; i < MAX_CONNECTIONS; i++) {
if (FD_ISSET(conn_sockets[i],&read_fd_set)) {
int num_bytes_read = recv(conn_sockets[i],buffer,sizeof(buffer),0);
if (num_bytes_read <= 0) {
close(conn_sockets[i]);
conn_sockets[i] = -1;
conn_sockets[i] = 0;
} else {
for (int i=0;i<num_bytes_read;i++) {
printf("%c",buffer[i]);

Loading…
Cancel
Save