#include #include #include #include #include #define PORT 8081 #define BUFFER_SIZE 1024 int main() { int server_fd, new_socket; struct sockaddr_in address; int opt = 1; int addrlen = sizeof(address); char buffer[BUFFER_SIZE] = {0}; 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("Bind failed"); exit(EXIT_FAILURE); } if (listen(server_fd, 3) < 0) { perror("Listen"); exit(EXIT_FAILURE); } printf("Server listening on port %d\n", PORT); if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) { perror("Accept"); exit(EXIT_FAILURE); } while (1) { memset(buffer, 0, BUFFER_SIZE); int read_size = read(new_socket, buffer, BUFFER_SIZE); if (read_size == 0) { printf("Client disconnected\n"); break; } printf("Received: %s", buffer); send(new_socket, buffer, read_size, 0); printf("Echoed back: %s", buffer); } close(new_socket); close(server_fd); return 0; }