- 基于socket 实现daytime(13)服务器(端口我们使用13+后三位学号)和客户端
服务器响应消息格式是
“
客户端IP:XXXX
客户端PID:XXXX
服务器tid:XXXX
服务器实现者学号:XXXXXXXX
当前时间: XX:XX:XX
”
注意服务器端要通过多线程实现,每次客户端链接到服务器,服务器就启动一个新线程和客户端连接。
点击查看代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <pthread.h>
#define BUFFER_SIZE 1024
void *handle_client(void *arg);
void send_daytime(int client_fd);
int main(int argc, char *argv[]) {
int server_fd, client_fd, port;
struct sockaddr_in server_addr, client_addr;
socklen_t addr_len = sizeof(struct sockaddr_in);
pthread_t tid;
if (argc != 2) {
printf("Usage: %s [port]\n", argv[0]);
return 1;
}
port = atoi(argv[1]);
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) {
perror("socket");
return 1;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(server_fd, (struct sockaddr *)&server_addr, addr_len) < 0) {
perror("bind");
return 1;
}
if (listen(server_fd, 5) < 0) {
perror("listen");
return 1;
}
while (1) {
client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &addr_len);
if (client_fd < 0) {
perror("accept");
continue;
}
pthread_create(&tid, NULL, handle_client, (void *)&client_fd);
pthread_detach(tid);
}
close(server_fd);
return 0;
}
void *handle_client(void *arg) {
int client_fd = *((int *)arg);
char buffer[BUFFER_SIZE];
send_daytime(client_fd);
while (1) {
memset(buffer, 0, BUFFER_SIZE);
if (recv(client_fd, buffer, BUFFER_SIZE, 0) <= 0) {
break;
}
printf("Received: %s\n", buffer);
}
close(client_fd);
return NULL;
}
void send_daytime(int client_fd) {
char buffer[BUFFER_SIZE];
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
snprintf(buffer, BUFFER_SIZE, "客户端IP:%lu\n客户端PID:%lu\n服务器tid:%lu\n服务器实现者学号:20211116\n当前时间:%02d:%02d:%02d\n",
inet_addr("127.0.0.1"),
getpid(),
pthread_self(),
buffer);
send(client_fd, buffer, strlen(buffer), 0);
}
点击查看代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define BUFFER_SIZE 1024
void send_message(int server_fd, const char *message);
int main(int argc, char *argv[]) {
int client_fd;
struct sockaddr_in server_addr;
char buffer[BUFFER_SIZE];
if (argc != 2) {
printf("Usage: %s [IP_address]\n", argv[0]);
return 1;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(13116); // DAYTIME port
inet_aton(argv[1], &server_addr.sin_addr);
client_fd = socket(AF_INET, SOCK_STREAM, 0);
if (client_fd < 0) {
perror("socket");
return 1;
}
if (connect(client_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("connect");
close(client_fd);
return 1;
}
send_message(client_fd, "GET DAYTIME\n");
while (1) {
memset(buffer, 0, BUFFER_SIZE);
if (recv(client_fd, buffer, BUFFER_SIZE, 0) <= 0) {
break;
}
printf("Received: %s\n", buffer);
}
close(client_fd);
return 0;
}
void send_message(int server_fd, const char *message) {
send(server_fd, message, strlen(message), 0);
}
-
提交一个客户端至少查询三次时间的截图测试截图
-
提交至少两个客户端查询时间的截图测试截图