Aug 23, 2024 Child Processes
Forking child processes to handle interactions from the client. Server Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8080
#define BUFFER_SIZE 1024
void handle_client(int client_socket)
{
char buffer[BUFFER_SIZE];
int valread;
while ((valread = read(client_socket, buffer, BUFFER_SIZE)) > 0)
{
buffer[valread] = '\0';
printf("Client: %s\n", buffer);
send(client_socket, buffer, strlen(buffer), 0);
}
close(client_socket);
exit(0);
}
int main()
{
int server_fd, new_socket;
struct sockaddr_in address;
socklen_t addr_len = sizeof(address);
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("Socket failed");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0)
{
perror("ERROR | Bind failed.");
close(server_fd);
exit(EXIT_FAILURE);
}
if (listen(server_fd, 10) < 0)
{
perror("ERROR | Listen failed.");
close(server_fd);
exit(EXIT_FAILURE);
}
printf("Server listening on port %d\n", PORT);
while (1)
{
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, &addr_len)) < 0)
{
perror("ERROR | Accept failed.");
close(server_fd);
exit(EXIT_FAILURE);
}
printf("INFO | New connection from %s:%d\n", inet_ntoa(address.sin_addr), ntohs(address.sin_port));
pid_t pid = fork();
if (pid == 0)
{
close(server_fd);
handle_client(new_socket);
}
else if (pid > 0)
{
close(new_socket);
}
else
{
perror("ERROR | Fork failed.");
close(new_socket);
}
}
close(server_fd);
return 0;
}
Client Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define PORT 8080
#define BUFFER_SIZE 1024
int main()
{
int sockfd = 0;
struct sockaddr_in serv_addr;
char buffer[BUFFER_SIZE];
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("ERROR | Socket creation error.\n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0)
{
printf("ERROR | Invalid address.\n");
return -1;
}
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("ERROR | Connection failed.\n");
return -1;
}
printf("INFO | Connected to the server...\n");
while (1)
{
printf("INPUT | Enter message: ");
fgets(buffer, BUFFER_SIZE, stdin);
send(sockfd, buffer, strlen(buffer), 0);
int valread = read(sockfd, buffer, BUFFER_SIZE);
buffer[valread] = '\0';
printf("INFO | Server: %s\n", buffer);
}
close(sockfd);
return 0;
}