实验四 Web服务器1-socket编程
基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用Linux Socket实现: 1. time服务器的客户端服务器,提交程序运行截图 2. echo服务器的客户端服务器,提交程序运行截图,服务器把客户端传进来的内容加入“服务器进程pid 你的学号 姓名 echo :”返回给客户端 3. 服务器部署到华为云服务器,客户端用Ubuntu虚拟机。 4. 要用多线程或者多进程实现,至少连接两个客户端。 5. 把服务器部署到试验箱。(加分项)
time_server.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <sys/socket.h> 5 #include <netinet/in.h> 6 #include <time.h> 7 8 #define PORT 12345 9 10 int main() { 11 int server_fd, new_socket; 12 struct sockaddr_in address; 13 int opt = 1; 14 int addrlen = sizeof(address); 15 16 // Create socket file descriptor 17 if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { 18 perror("socket failed"); 19 exit(EXIT_FAILURE); 20 } 21 22 // Forcefully attaching socket to the port 12345 23 if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) { 24 perror("setsockopt"); 25 exit(EXIT_FAILURE); 26 } 27 address.sin_family = AF_INET; 28 address.sin_addr.s_addr = INADDR_ANY; 29 address.sin_port = htons(PORT); 30 31 // Forcefully attaching socket to the port 12345 32 if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0) { 33 perror("bind failed"); 34 exit(EXIT_FAILURE); 35 } 36 if (listen(server_fd, 3) < 0) { 37 perror("listen"); 38 exit(EXIT_FAILURE); 39 } 40 while (1) { 41 if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0) { 42 perror("accept"); 43 exit(EXIT_FAILURE); 44 } 45 char buffer[1024] = {0}; 46 time_t current_time; 47 time(¤t_time); 48 snprintf(buffer, sizeof(buffer), "%s", ctime(¤t_time)); 49 send(new_socket, buffer, strlen(buffer), 0); 50 printf("Time sent to the client\n"); 51 close(new_socket); 52 } 53 return 0; 54 }
time_client.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <sys/socket.h> 5 #include <netinet/in.h> 6 7 #define PORT 12345 8 9 int main() { 10 struct sockaddr_in serv_addr; 11 int sock = 0, valread; 12 char buffer[1024] = {0}; 13 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { 14 printf("\n Socket creation error \n"); 15 return -1; 16 } 17 serv_addr.sin_family = AF_INET; 18 serv_addr.sin_port = htons(PORT); 19 20 // Convert IPv4 and IPv6 addresses from text to binary form 21 if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0) { 22 printf("\nInvalid address/ Address not supported \n"); 23 return -1; 24 } 25 if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { 26 printf("\nConnection Failed \n"); 27 return -1; 28 } 29 valread = read(sock, buffer, 1024); 30 printf("%s\n", buffer); 31 return 0; 32 }
运行截图
echo_server.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <sys/socket.h> 5 #include <netinet/in.h> 6 #include <unistd.h> 7 #include <sys/types.h> 8 9 #define PORT 12345 10 11 void handle_client(int new_socket) { 12 char buffer[1024] = {0}; 13 int valread = read(new_socket, buffer, 1024); 14 printf("Received message from client: %s\n", buffer); 15 16 // 构造回复消息 17 pid_t pid = getpid(); 18 char student_id[] = "20211102"; 19 char student_name[] = "尹子扬"; 20 char reply_message[1024]; 21 sprintf(reply_message, "服务器进程pid %d %s %s echo: %s", pid, student_id, student_name, buffer); 22 23 // 发送回复消息给客户端 24 send(new_socket, reply_message, strlen(reply_message), 0); 25 printf("Reply sent to the client\n"); 26 close(new_socket); 27 } 28 29 int main() { 30 int server_fd, new_socket; 31 struct sockaddr_in address; 32 int opt = 1; 33 int addrlen = sizeof(address); 34 35 // 创建socket文件描述符 36 if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { 37 perror("socket failed"); 38 exit(EXIT_FAILURE); 39 } 40 41 // 设置socket选项,允许地址重用 42 if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) { 43 perror("setsockopt"); 44 exit(EXIT_FAILURE); 45 } 46 address.sin_family = AF_INET; 47 address.sin_addr.s_addr = INADDR_ANY; 48 address.sin_port = htons(PORT); 49 50 // 将socket绑定到指定的IP地址和端口号 51 if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0) { 52 perror("bind failed"); 53 exit(EXIT_FAILURE); 54 } 55 if (listen(server_fd, 3) < 0) { 56 perror("listen"); 57 exit(EXIT_FAILURE); 58 } 59 60 while (1) { 61 if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0) { 62 perror("accept"); 63 exit(EXIT_FAILURE); 64 } 65 handle_client(new_socket); 66 } 67 return 0; 68 }
echo_client.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <sys/socket.h> 5 #include <netinet/in.h> 6 #include <unistd.h> 7 8 #define PORT 12345 9 10 int main() { 11 struct sockaddr_in serv_addr; 12 int sock = 0, valread; 13 char buffer[1024] = {0}; 14 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { 15 printf("\n Socket creation error \n"); 16 return -1; 17 } 18 serv_addr.sin_family = AF_INET; 19 serv_addr.sin_port = htons(PORT); 20 21 // 将IPv4地址从文本转换为二进制形式 22 if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0) { 23 printf("\nInvalid address/ Address not supported \n"); 24 return -1; 25 } 26 if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { 27 printf("\nConnection Failed \n"); 28 return -1; 29 } 30 31 // 发送消息给服务器 32 char message[] = "Hello, server!"; 33 send(sock, message, strlen(message), 0); 34 35 // 从服务器接收回复 36 valread = read(sock, buffer, 1024); 37 printf("Reply from server: %s\n", buffer); 38 39 return 0; 40 }
运行截图
第一个客户端:
第二个客户端:
标签:Web,socket,int,编程,address,include,sin,addr From: https://www.cnblogs.com/20211115fyq/p/17882081.html