Commit b006c025 by Michael Pastushkov

fix

parent 86bb5003
#!/bin/sh
make clean && make && ./bytevia --local-port=1948 --remote-host=p4pn.net --remote-port=1984 --proto=udp --secret=52341 --fork
\ No newline at end of file
make clean && make && ./bytevia --local-port=1948 --remote-host=p4pn.net --remote-port=1984 --proto=udp --secret=52341
\ No newline at end of file
......@@ -255,18 +255,6 @@ int wait_connection(void) {
perror("wait_connection: accept()");
return 1;
}
// if (options.fork && options.proto == SOCK_DGRAM && !is_child) {
// if (udp_fork(client_addr) == 1)
// continue;
// }
// fd_set io;
// FD_ZERO(&io);
// FD_SET(rc.client_socket, &io);
// if (select(rc.client_socket+1, &io, NULL, NULL, NULL) < 0) {
// if (errno != EINTR)
// perror("wait_connection: select()");
// return 1;
// }
} else {
rc.client_socket = accept(rc.server_socket, (struct sockaddr *) &rc.client_addr, &client_addr_size);
if (rc.client_socket < 0) {
......@@ -323,14 +311,25 @@ int add_udp_client(struct sockaddr_in client_addr, pid_t pid) {
if (clients[i].pid == 0) {
clients[i].addr = client_addr;
clients[i].pid = pid;
return 0;
}
}
return -1; /* No space for new clients */
}
printf("client %s added ...\n", inet_ntoa(client_addr.sin_addr));
return 0; // Successfully added client
void remove_udp_client(pid_t pid) {
int i;
for (i = 0; i<MAX_UDP_CLIENTS; i++) {
if (clients[i].pid == pid) {
if (options.log)
printf("removing client %s, pid: %d\n", inet_ntoa(clients[i].addr.sin_addr), pid);
clients[i].pid = 0;
memset(&clients[i].addr, 0, sizeof(clients[i].addr));
break;
}
}
return -1; // No space for new clients
}
int compare_clients(struct sockaddr_in client1, struct sockaddr_in client2) {
return (client1.sin_addr.s_addr == client2.sin_addr.s_addr &&
client1.sin_port == client2.sin_port);
......@@ -340,7 +339,6 @@ int find_udp_client(struct sockaddr_in client_addr) {
int i;
for (i=0; i<MAX_UDP_CLIENTS; i++)
if (clients[i].pid > 0 && compare_clients(clients[i].addr, client_addr)) {
printf("client %s found %d ...\n", inet_ntoa(client_addr.sin_addr), i);
return i;
}
return -1;
......@@ -443,17 +441,22 @@ int fork_udp() {
if (client_index == -1) {
if ((child_pid = fork()) == 0) {
if (options.log)
printf("forking for %s, %d ...\n", inet_ntoa(rc.client_addr.sin_addr), getpid());
printf("request from %s, forking %d ...\n", inet_ntoa(rc.client_addr.sin_addr), getpid());
run();
exit(0);
}
add_udp_client(rc.client_addr, child_pid);
if (add_udp_client(rc.client_addr, child_pid) < 0) {
printf("maximum clients %d reached: \n", MAX_UDP_CLIENTS);
}
// while (waitpid(-1, NULL, WNOHANG) > 0)
// ; /* Reap any zombie processes (clients that exited) */
// Reap any zombie processes (clients that exited)
while ((child_pid = waitpid(-1, NULL, WNOHANG)) > 0) {
remove_udp_client(child_pid);
}
} else {
printf("existing udp client %s, handled by %d ...\n", inet_ntoa(rc.client_addr.sin_addr), clients[client_index].pid);
if (options.log > 2)
printf("existing udp client %s, handled by %d ...\n", inet_ntoa(rc.client_addr.sin_addr), clients[client_index].pid);
}
return 1;
}
......
......@@ -173,9 +173,10 @@ int main()
printf("Existing client sent a request, handled by child process with PID: %d\n", clients[client_index].pid);
}
// Reap any zombie processes (clients that exited)
while (waitpid(-1, NULL, WNOHANG) > 0)
;
// // Reap any zombie processes (clients that exited)
// while ((child_pid = waitpid(-1, NULL, WNOHANG)) > 0) {
// remove_client_by_pid(child_pid);
// }
}
close(sockfd);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment