实验四 Web服务器1-socket编程
time服务器的客户端服务器
time_server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
void handle_client(int client_socket) {
time_t current_time;
char time_string[50];
// Get current time
time(¤t_time);
snprintf(time_string, sizeof(time_string), "%s", ctime(¤t_time));
// Send current time to the client
send(client_socket, time_string, strlen(time_string), 0);
close(client_socket);
}
int main() {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_len = sizeof(client_addr);
// Create socket
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
// Bind socket to address and port
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(12345); // Use a specific port number, e.g., 12345
if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
perror("Error binding socket");
exit(EXIT_FAILURE);
}
// Listen for incoming connections
if (listen(server_socket, 5) == -1) {
perror("Error listening for connections");
exit(EXIT_FAILURE);
}
printf("Time Server listening on port 12345...\n");
while (1) {
// Accept connection from client
client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_addr_len);
if (client_socket == -1) {
perror("Error accepting connection");
continue;
}
printf("Accepted connection from %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
// Fork a child process to handle the client
if (fork() == 0) {
// Child process
close(server_socket); // Close server socket in child process
handle_client(client_socket);
exit(EXIT_SUCCESS);
} else {
// Parent process
close(client_socket); // Close client socket in parent process
waitpid(-1, NULL, WNOHANG); // Reap zombie processes
}
}
return 0;
}
time_client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
void time_client() {
int client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Replace with your server's IP address
server_addr.sin_port = htons(12345); // Use the port number used by the time server
if (connect(client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
perror("Error connecting to the server");
close(client_socket);
exit(EXIT_FAILURE);
}
char buffer[1024];
ssize_t recv_size = recv(client_socket, buffer, sizeof(buffer) - 1, 0);
if (recv_size <= 0) {
perror("Error receiving data");
} else {
buffer[recv_size] = '\0'; // Null-terminate the received data
printf("Time from server: %s", buffer);
}
close(client_socket);
}
int main() {
// Time Client
printf("Time Client:\n");
time_client();
return 0;
}
echo服务器的客户端服务器
echo_server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
void handle_echo(int client_socket) {
char buffer[1024];
int pid = getpid();
char student_id[] = "20211327";
char name[] = "sjx";
// Receive data from client
ssize_t recv_size = recv(client_socket, buffer, sizeof(buffer), 0);
if (recv_size <= 0) {
perror("Error receiving data");
close(client_socket);
return;
}
// Process data and prepare response
char response[1024];
int snprintf_result = snprintf(response, sizeof(response), "Server process pid %d %s %s echo: %s",
pid, student_id, name, buffer);
if (snprintf_result < 0 || snprintf_result >= sizeof(response)) {
// Handle error or truncate situation
perror("Error formatting response");
close(client_socket);
return;
}
// Send response to the client
send(client_socket, response, strlen(response), 0);
close(client_socket);
}
int main() {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_len = sizeof(client_addr);
// Create socket
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
// Bind socket to address and port
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(12346); // Use a different port number, e.g., 12346
if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
perror("Error binding socket");
exit(EXIT_FAILURE);
}
// Listen for incoming connections
if (listen(server_socket, 5) == -1) {
perror("Error listening for connections");
exit(EXIT_FAILURE);
}
printf("Echo Server listening on port 12346...\n");
while (1) {
// Accept connection from client
client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_addr_len);
if (client_socket == -1) {
perror("Error accepting connection");
continue;
}
printf("Accepted connection from %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
// Fork a child process to handle the client
if (fork() == 0) {
// Child process
close(server_socket); // Close server socket in child process
handle_echo(client_socket);
exit(EXIT_SUCCESS);
} else {
// Parent process
close(client_socket); // Close client socket in parent process
waitpid(-1, NULL, WNOHANG); // Reap zombie processes
}
}
return 0;
}
echo_client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
void echo_client() {
int client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Replace with your server's IP address
server_addr.sin_port = htons(12346); // Use the port number used by the echo server
if (connect(client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
perror("Error connecting to the server");
close(client_socket);
exit(EXIT_FAILURE);
}
char message[] = "Hello, server!"; // Replace with your message
send(client_socket, message, strlen(message), 0);
char buffer[1024];
ssize_t recv_size = recv(client_socket, buffer, sizeof(buffer) - 1, 0);
if (recv_size <= 0) {
perror("Error receiving data");
} else {
buffer[recv_size] = '\0'; // Null-terminate the received data
printf("Response from server: %s\n", buffer);
}
close(client_socket);
}
int main() {
// Echo Client
printf("\nEcho Client:\n");
echo_client();
return 0;
}