#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define PORT 7000 // Replace with your desired port 7XXX
#define MAXLINE 1024
void handle_client(int connfd) {
char buffer[MAXLINE];
int n;
// Read message from client
n = read(connfd, buffer, sizeof(buffer) - 1);
if (n < 0) {
perror("Read error");
close(connfd);
return;
}
buffer[n] = '\0'; // Null-terminate the message
// Print received message
printf("Received from client: %s\n", buffer);
// Echo the message back to the client
write(connfd, buffer, strlen(buffer));
close(connfd);
}
int main() {
int listenfd, connfd;
struct sockaddr_in servaddr, cliaddr;
socklen_t len;
pid_t childpid;
// Create server socket
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd < 0) {
perror("Socket creation failed");
exit(1);
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY; // Listen on all available interfaces
servaddr.sin_port = htons(PORT);
// Bind the socket to the address and port
if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
perror("Bind failed");
exit(1);
}
// Listen for incoming connections
if (listen(listenfd, 10) < 0) {
perror("Listen failed");
exit(1);
}
printf("Server started on port %d\n", PORT);
// Main loop to accept and handle clients
for (;;) {
len = sizeof(cliaddr);
connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &len);
if (connfd < 0) {
perror("Accept failed");
continue;
}
if ((childpid = fork()) == 0) {
close(listenfd); // Child doesn't need the listening socket
handle_client(connfd);
exit(0); // Child process terminates here
}
close(connfd); // Parent closes the connection socket
while (waitpid(-1, NULL, WNOHANG) > 0); // Clean up any terminated child processes
}
return 0;
}
------------------------------------------------------------------
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#define PORT 7000 // Replace with the same port used in the server
#define MAXLINE 1024
int main() {
int sockfd;
struct sockaddr_in servaddr;
char send_buffer[MAXLINE] = "Hello from ITxxxxxxxx";
char recv_buffer[MAXLINE];
int n;
// Create socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("Socket creation failed");
exit(1);
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
if (inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr) <= 0) { // Assuming server is on localhost
perror("Invalid address or address not supported");
close(sockfd);
exit(1);
}
// Connect to server
if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
perror("Connection failed");
close(sockfd);
exit(1);
}
// Send message to the server
write(sockfd, send_buffer, strlen(send_buffer));
// Receive the echo from server
n = read(sockfd, recv_buffer, MAXLINE);
if (n < 0) {
perror("Read error");
close(sockfd);
exit(1);
}
recv_buffer[n] = '\0';
// Print the echo response
printf("Received from server: %s\n", recv_buffer);
close(sockfd);
return 0;
}