manual testing
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
@ -115,13 +116,26 @@ void handler_tcp_echo(Server *s) {
|
||||
printf("Received connection from %s\n", buf);
|
||||
|
||||
// Start child process
|
||||
if (!fork()) {
|
||||
close(s->fd); // Child doesn't need the initial socket
|
||||
if (send(new_fd, "Hello, world!", 13, 0) == -1) {
|
||||
fprintf(stderr, "Failed to send hello world\n");
|
||||
}
|
||||
close(new_fd);
|
||||
_exit(0);
|
||||
if (!fork()) {
|
||||
close(s->fd); // Child doesn't need the initial socket
|
||||
|
||||
char recv_buf[256];
|
||||
int r = recv(new_fd, recv_buf, 256, 0);
|
||||
if (r == -1) {
|
||||
fprintf(stderr, "Failed to recv. Errno: %d\n", errno);
|
||||
goto CHILD_END;
|
||||
} else if (r == 0) {
|
||||
fprintf(stderr, "Client closed connection\n");
|
||||
goto CHILD_END;
|
||||
}
|
||||
|
||||
if (send(new_fd, recv_buf, strlen(recv_buf), 0) == -1) {
|
||||
fprintf(stderr, "Failed to send echo\n");
|
||||
}
|
||||
|
||||
CHILD_END:
|
||||
close(new_fd);
|
||||
_exit(0);
|
||||
}
|
||||
// End child process
|
||||
|
||||
|
Reference in New Issue
Block a user