实验四 Web服务器1-socket编程
基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用Linux Socket实现:
- time服务器的客户端服务器,提交程序运行截图
- echo服务器的客户端服务器,提交程序运行截图,服务器把客户端传进来的内容加入“服务器进程pid 你的学号 姓名 echo :”返回给客户端
- 服务器部署到华为云服务器,客户端用Ubuntu虚拟机。
- 要用多线程或者多进程实现,至少连接两个客户端。
- 把服务器部署到试验箱。(加分项)
1。time服务器
服务器代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <time.h>
#include <unistd.h>
#define PORT 8080
#define BUFFSIZE 1024
void handle_client(int client_socket) {
char buffer[BUFFSIZE];
time_t current_time;
struct tm *time_info;
time(¤t_time);
time_info = localtime(¤t_time);
snprintf(buffer, sizeof(buffer), "%s", asctime(time_info));
send(client_socket, buffer, strlen(buffer), 0);
close(client_socket);
}
int main() {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
socklen_t client_len = sizeof(client_addr);
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
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);
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("Error binding socket");
exit(EXIT_FAILURE);
}
if (listen(server_socket, 5) == -1) {
perror("Error listening on socket");
exit(EXIT_FAILURE);
}
printf("Time server is running...\n");
while (1) {
client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_len);
if (fork() == 0) { // child process
close(server_socket); // child doesn't need the listener
handle_client(client_socket);
exit(0);
} else { // parent process
close(client_socket); // parent doesn't need the client socket
}
}
close(server_socket);
return 0;
}
客户端代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#define PORT 8080
#define BUFFSIZE 1024
int main() {
int client_socket;
struct sockaddr_in server_addr;
char buffer[BUFFSIZE];
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
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 = INADDR_ANY;
if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("Error connecting to server");
exit(EXIT_FAILURE);
}
recv(client_socket, buffer, sizeof(buffer), 0);
printf("Server Time: %s\n", buffer);
close(client_socket);
return 0;
}
2.echo服务器
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#define PORT 8081
#define BUFFSIZE 1024
void handle_client(int client_socket) {
char buffer[BUFFSIZE];
int bytes_received;
bytes_received = recv(client_socket, buffer, sizeof(buffer), 0);
if (bytes_received > 0) {
// Process the received data (e.g., add server process ID, student ID, name, etc.)
// and send it back to the client.
snprintf(buffer, sizeof(buffer), "服务器进程pid %d 20211317lzt echo : %s", getpid(), buffer);
send(client_socket, buffer, strlen(buffer), 0);
}
close(client_socket);
}
int main() {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
socklen_t client_len = sizeof(client_addr);
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
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);
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("Error binding socket");
exit(EXIT_FAILURE);
}
if (listen(server_socket, 5) == -1) {
perror("Error listening on socket");
exit(EXIT_FAILURE);
}
printf("Echo server is running...\n");
while (1) {
client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_len);
if (fork() == 0) { // child process
close(server_socket); // child doesn't need the listener
handle_client(client_socket);
exit(0);
} else { // parent process
close(client_socket); // parent doesn't need the client socket
}
}
close(server_socket);
return 0;
}
客户端代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#define PORT 8081
#define BUFFSIZE 1024
int main() {
int client_socket;
struct sockaddr_in server_addr;
char buffer[BUFFSIZE];
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
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 = INADDR_ANY;
if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("Error connecting to server");
exit(EXIT_FAILURE);
}
// Send data to the server
printf("Enter data to send to server: ");
fgets(buffer, sizeof(buffer), stdin);
send(client_socket, buffer, strlen(buffer), 0);
// Receive and print the response from the server
recv(client_socket, buffer, sizeof(buffer), 0);
printf("Server Response: %s\n", buffer);
close(client_socket);
return 0;
}
标签:Web,addr,buffer,编程,server,client,include,socket
From: https://www.cnblogs.com/lizhuotong/p/17884845.html