课上测试4 socket
1.socket测试
要求:基于socket 基于TCP协议实现echo服务器(端口我们使用7+后三位学号)和客户端
客户端发送数据格式为:“客户端实现者8位学号:消息” 不符合规则的数据忽略,并提示出错。(5‘)
服务器响应消息格式是(10’)
“
客户端IP:XXXX
客户端PID:XXXX
服务器PID:XXXX
服务器实现者学号:XXXXXXXX
客户端内容发送内容倒序(比如:客户端发abc, 服务器返回cba)
”
注意服务器端要通过多进程实现,每次客户端链接到服务器,服务器就启动一个新进程程和客户端连接。(10‘)
代码:
Echo服务器代码(echo_server.c):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 7403
void handle_client(int client_socket) {
char buffer[256];
ssize_t bytes_read;
// 读取客户端发送的数据
bytes_read = read(client_socket, buffer, sizeof(buffer));
if (bytes_read < 0) {
perror("Error reading from socket");
exit(1);
}
// 解析客户端发送的数据
char student_id[9];
char message[256];
sscanf(buffer, "%8s:%[^\n]", student_id, message);
// 检查数据格式是否正确
if (strlen(student_id) != 8 || message[0] == '\0') {
printf("Invalid data format from client\n");
close(client_socket);
exit(1);
}
// 获取客户端信息
struct sockaddr_in client_address;
socklen_t client_address_length = sizeof(client_address);
getpeername(client_socket, (struct sockaddr*)&client_address, &client_address_length);
char client_ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(client_address.sin_addr), client_ip, sizeof(client_ip));
pid_t client_pid = getpid();
pid_t server_pid = getpid();
// 将客户端发送的内容倒序
int message_length = strlen(message);
char reversed_message[message_length + 1];
for (int i = 0; i < message_length; i++) {
reversed_message[i] = message[message_length - i - 1];
}
reversed_message[message_length] = '\0';
// 构建服务器响应消息
char response[512];
snprintf(response, sizeof(response),
"客户端IP:%s\n"
"客户端PID:%d\n"
"服务器PID:%d\n"
"服务器实现者学号:%s\n"
"客户端内容发送内容倒序:%s\n",
client_ip, client_pid, server_pid, student_id, reversed_message);
// 发送响应消息给客户端
ssize_t bytes_written = write(client_socket, response, strlen(response));
if (bytes_written < 0) {
perror("Error writing to socket");
exit(1);
}
// 关闭客户端连接
close(client_socket);
exit(0);
}
int main() {
int server_socket, client_socket;
struct sockaddr_in server_address, client_address;
socklen_t client_address_length;
// 创建套接字
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket < 0) {
perror("Error opening socket");
exit(1);
}
// 设置服务器地址
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = INADDR_ANY;
server_address.sin_port = htons(PORT);
// 绑定套接字到指定端口
if (bind(server_socket, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) {
perror("Error binding socket");
exit(1);
}
// 监听连接
if (listen(server_socket, 5) < 0) {
perror("Error listening on socket");
exit(1);
}
while (1) {
// 接受客户端连接
client_address_length = sizeof(client_address);
client_socket = accept(server_socket, (struct sockaddr*)&client_address, &client_address_length);
if (client_socket < 0) {
perror("Error accepting client connection");
exit(1);
}
// 创建子进程处理客户端连接
pid_t child_pid = fork();
if (child_pid < 0) {
perror("Error forking new process");
exit(1);
} else if (child_pid == 0) {
// 子进程处理客户端连接
close(server_socket);
handle_client(client_socket);
} else {
// 父进程继续监听连接
close(client_socket);
}
}
// 关闭服务器套接字
close(server_socket);
return 0;
}
Echo客户端代码(echo_client.c):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SERVER_IP "127.0.0.1" // 服务器IP地址
#define SERVER_PORT 7403 // 使用端口号7+后三位学号
int main() {
int client_socket;
struct sockaddr_in server_address;
// 创建套接字
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket < 0) {
perror("Error opening socket");
exit(1);
}
// 设置服务器地址
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_port = htons(SERVER_PORT);
if (inet_pton(AF_INET, SERVER_IP, &(server_address.sin_addr)) <= 0) {
perror("Error converting server IP address");
exit(1);
}
// 连接到服务器
if (connect(client_socket, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) {
perror("Error connecting to server");
exit(1);
}
// 构建客户端发送的数据
char student_id[9] = "20211403"; // 请替换为自己的学号
char message[256];
printf("请输入消息:");
fgets(message, sizeof(message), stdin);
// 检查数据格式是否正确
if (strlen(student_id) != 8 || message[0] == '\0') {
printf("Invalid data format\n");
close(client_socket);
exit(1);
}
// 构建发送的数据
char data[512];
snprintf(data, sizeof(data), "%s:%s", student_id, message);
// 发送数据给服务器
ssize_t bytes_written = write(client_socket, data, strlen(data));
if (bytes_written < 0) {
perror("Error writing to socket");
exit(1);
}
// 接收服务器响应
char response[512];
ssize_t bytes_read = read(client_socket, response, sizeof(response));
if (bytes_read < 0) {
perror("Error reading from socket");
exit(1);
}
// 打印服务器响应
printf("服务器响应:\n%s\n", response);
// 关闭套接字
close(client_socket);
return 0;
}
运行截图如下: