echo_server
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 7406
void handle_client(int client_socket, char *student_id) {
char buffer[1024];
ssize_t bytes_received;
// Receive data from the client
bytes_received = recv(client_socket, buffer, sizeof(buffer), 0);
if (bytes_received < 0) {
perror("Error receiving data from client");
close(client_socket);
exit(EXIT_FAILURE);
}
buffer[bytes_received] = '\0';
// Check if the received data follows the specified format
if (sscanf(buffer, "%*[^:]:%*[^:]:%*d:%*d:%s", student_id) != 1) {
fprintf(stderr, "Error: Invalid data format received from client.\n");
close(client_socket);
exit(EXIT_FAILURE);
}
// Reverse the received data
int len = strlen(buffer);
for (int i = 0; i < len / 2; i++) {
char temp = buffer[i];
buffer[i] = buffer[len - i - 1];
buffer[len - i - 1] = temp;
}
// Send the reversed data back to the client
ssize_t bytes_sent = send(client_socket, buffer, strlen(buffer), 0);
if (bytes_sent < 0) {
perror("Error sending data to client");
}
// Close the client socket
close(client_socket);
exit(EXIT_SUCCESS);
}
int main() {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_len = sizeof(client_addr);
char student_id[9];
// Create socket
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket < 0) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
// Configure server address
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
// Bind socket
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Error binding socket");
close(server_socket);
exit(EXIT_FAILURE);
}
// Listen for incoming connections
if (listen(server_socket, 5) < 0) {
perror("Error listening for connections");
close(server_socket);
exit(EXIT_FAILURE);
}
while (1) {
// Accept connection from client
client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_len);
if (client_socket < 0) {
perror("Error accepting connection");
continue;
}
// Fork a new process to handle the client
pid_t pid = fork();
if (pid < 0) {
perror("Error forking process");
close(client_socket);
continue;
} else if (pid == 0) {
// Child process handles the client
close(server_socket);
handle_client(client_socket, student_id);
} else {
// Parent process continues to accept new connections
close(client_socket);
}
}
// Close the server socket
close(server_socket);
return 0;
}
echo_client
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#define PORT 7406
int main() {
int client_socket;
struct sockaddr_in server_addr;
char student_id[9] = "YOUR_ID"; // Replace with your 8-digit student ID
// Create socket
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket < 0) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
// Configure server address
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Assuming server is running locally
// Connect to the server
if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Error connecting to server");
close(client_socket);
exit(EXIT_FAILURE);
}
// Send data to the server
char message[1024];
snprintf(message, sizeof(message), "客户端实现者%s:消息", student_id);
ssize_t bytes_sent = send(client_socket, message, strlen(message), 0);
if (bytes_sent < 0) {
perror("Error sending data to server");
}
// Receive and print the response from the server
char buffer[1024];
ssize_t bytes_received = recv(client_socket, buffer, sizeof(buffer), 0);
if (bytes_received < 0) {
perror("Error receiving data from server");
} else {
buffer[bytes_received] = '\0';
printf("Server Response:\n%s\n", buffer);
}
// Close the client socket
close(client_socket);
return 0;
}