client.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define SERVER_IP "127.0.0.1"
#define PORT 7312
#define MAX_MSG_SIZE 1024
int main() {
int client_socket;
struct sockaddr_in server_address;
// 创建客户端套接字
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1) {
perror("创建套接字失败");
exit(EXIT_FAILURE);
}
// 设置服务器地址
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr(SERVER_IP);
server_address.sin_port = htons(PORT);
// 建立与服务器的连接
if (connect(client_socket, (struct sockaddr *)&server_address, sizeof(server_address)) < 0) {
perror("连接服务器失败");
exit(EXIT_FAILURE);
}
// 输入消息
char input[MAX_MSG_SIZE];
printf("请输入消息:");
fgets(input, sizeof(input), stdin);
// 检查消息格式
if (strncmp(input, "客户端实现者20211312:", 17) != 0) {
perror("错误:消息格式不正确");
exit(EXIT_FAILURE);
}
// 向服务器发送消息
write(client_socket, input, strlen(input));
// 读取服务器的响应
char response[MAX_MSG_SIZE];
ssize_t bytesRead = read(client_socket, response, sizeof(response));
if (bytesRead > 0) {
printf("服务器的响应:\n%s\n", response);
}
// 关闭套接字
close(client_socket);
return 0;
}
server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define PORT 7312
#define MAX_MSG_SIZE 1024
void handle_client(int client_socket) {
char buffer[MAX_MSG_SIZE];
char response[MAX_MSG_SIZE];
// 读取客户端发送的消息
ssize_t bytesRead = read(client_socket, buffer, sizeof(buffer));
if (bytesRead > 0) {
// 解析客户端发送的消息
if (strncmp(buffer, "客户端实现者20211312:", 17) == 0) {
char *message = buffer + 17;
int messageLength = strlen(message);
// 倒序处理客户端发送的消息
for (int i = 0; i < messageLength; i++) {
response[i] = message[messageLength - i - 1];
}
response[messageLength] = '\0';
// 获取客户端信息
struct sockaddr_in client_address;
socklen_t client_address_len = sizeof(client_address);
getpeername(client_socket, (struct sockaddr *)&client_address, &client_address_len);
char client_ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(client_address.sin_addr), client_ip, INET_ADDRSTRLEN);
int client_pid = getpid();
int server_pid = getpid();
// 构建响应消息
sprintf(buffer, "客户端IP:%s\n客户端PID:%d\n服务器PID:%d\n服务器实现者学号:20211312\n客户端内容发送内容倒序:%s\n",
client_ip, client_pid, server_pid, response);
// 向客户端发送响应
write(client_socket, buffer, strlen(buffer));
} else {
// 不符合规则的消息,发送错误提示
char *error = "错误:消息格式不正确\n";
write(client_socket, error, strlen(error));
}
}
// 关闭客户端连接
close(client_socket);
}
int main() {
int server_socket, client_socket;
struct sockaddr_in server_address, client_address;
socklen_t client_address_len = sizeof(client_address);
// 创建服务器套接字
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
perror("创建套接字失败");
exit(EXIT_FAILURE);
}
// 设置服务器地址
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("绑定套接字失败");
exit(EXIT_FAILURE);
}
// 监听客户端连接
if (listen(server_socket, 5) < 0) {
perror("监听失败");
exit(EXIT_FAILURE);
}
printf("服务器已启动,监听端口:%d\n", PORT);
// 处理客户端连接
while (1) {
// 接受客户端连接
client_socket = accept(server_socket, (struct sockaddr *)&client_address, &client_address_len);
if (client_socket < 0) {
perror("接受连接失败");
exit(EXIT_FAILURE);
}
// 创建子进程来处理客户端连接
pid_t pid = fork();
if (pid < 0) {
perror("创建子进程失败");
exit(EXIT_FAILURE);
} else if (pid == 0) {
// 在子进程中处理客户端连接
close(server_socket);
handle_client(client_socket);
exit(EXIT_SUCCESS);
} else {
// 在父进程中继续监听客户端连接
close(client_socket);
}
}
// 关闭服务器套接字
close(server_socket);
return 0;
}