Use 'client_socket' instead of child_socket

This commit is contained in:
flyingscorpio@clevo 2022-10-21 10:20:27 +02:00
parent d3312c2898
commit 5420a3b264

View file

@ -12,9 +12,9 @@
int RUN = 1; int RUN = 1;
int main(void) { int main(void) {
int serv_sockid, child_sockid; int serv_sockid, client_sockid;
struct sockaddr_in server_socket; struct sockaddr_in server_socket;
struct sockaddr_in child_socket; struct sockaddr_in client_socket;
pid_t pid; pid_t pid;
long addr_len; long addr_len;
@ -65,23 +65,23 @@ int main(void) {
/* Loop to accept incoming connections */ /* Loop to accept incoming connections */
// TODO: make threads instead of fork() // TODO: make threads instead of fork()
while (RUN) { while (RUN) {
if ((child_sockid = accept(serv_sockid, (struct sockaddr *) &child_socket, (socklen_t *) &addr_len)) < 0) { if ((client_sockid = accept(serv_sockid, (struct sockaddr *) &client_socket, (socklen_t *) &addr_len)) < 0) {
perror("accept"); perror("accept");
} else { } else {
printf("received connection from %s\n", ip4tostring(ntohl(child_socket.sin_addr.s_addr))); printf("received connection from %s\n", ip4tostring(ntohl(client_socket.sin_addr.s_addr)));
/* Create a dedicated process for the client */ /* Create a dedicated process for the client */
if ((pid = fork()) == -1) { if ((pid = fork()) == -1) {
perror("fork"); perror("fork");
write(child_sockid, "server error\n", 14); write(client_sockid, "server error\n", 14);
} else { } else {
if (pid == 0) { if (pid == 0) {
service(child_sockid); service(client_sockid);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
} }
/* Let the child handle the file descriptor that was just created */ /* Let the child handle the file descriptor that was just created */
close(child_sockid); close(client_sockid);
} }
} }