// 服务器端代码

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h>

#define MAX_LINE 1024 #define PORT 8888

struct word { char en[MAX_LINE]; char ch[MAX_LINE]; };

struct client_info { int socket_fd; struct sockaddr_in client_addr; int client_len; };

void handle_query(struct client_info *client, struct word *words, int count) { char buf[MAX_LINE]; int n; while (1) { memset(buf, 0, MAX_LINE); n = recvfrom(client->socket_fd, buf, MAX_LINE, 0, (struct sockaddr *)&client->client_addr, &client->client_len); if (n < 0) { perror("recvfrom"); return; } printf("Received query request from %s:%d\n", inet_ntoa(client->client_addr.sin_addr), ntohs(client->client_addr.sin_port)); int exist = 0; for (int i = 0; i < count; i++) { if (strcmp(words[i].en, buf) == 0) { exist = 1; sendto(client->socket_fd, words[i].ch, strlen(words[i].ch), 0, (struct sockaddr *)&client->client_addr, client->client_len); printf("Sent translation of %s to %s:%d\n", buf, inet_ntoa(client->client_addr.sin_addr), ntohs(client->client_addr.sin_port)); break; } } if (!exist) { sendto(client->socket_fd, "Word not found", strlen("Word not found"), 0, (struct sockaddr *)&client->client_addr, client->client_len); printf("Sent 'Word not found' to %s:%d\n", inet_ntoa(client->client_addr.sin_addr), ntohs(client->client_addr.sin_port)); } } }

void handle_quiz(struct client_info *client, struct word *words, int count) { char buf[MAX_LINE]; int n; while (1) { memset(buf, 0, MAX_LINE); n = recvfrom(client->socket_fd, buf, MAX_LINE, 0, (struct sockaddr *)&client->client_addr, &client->client_len); if (n < 0) { perror("recvfrom"); return; } printf("Received quiz request from %s:%d\n", inet_ntoa(client->client_addr.sin_addr), ntohs(client->client_addr.sin_port)); int i = rand() % count; sendto(client->socket_fd, words[i].en, strlen(words[i].en), 0, (struct sockaddr *)&client->client_addr, client->client_len); printf("Sent quiz question %s to %s:%d\n", words[i].en, inet_ntoa(client->client_addr.sin_addr), ntohs(client->client_addr.sin_port)); memset(buf, 0, MAX_LINE); n = recvfrom(client->socket_fd, buf, MAX_LINE, 0, (struct sockaddr *)&client->client_addr, &client->client_len); if (n < 0) { perror("recvfrom"); return; } if (strcmp(words[i].ch, buf) == 0) { sendto(client->socket_fd, "Correct", strlen("Correct"), 0, (struct sockaddr *)&client->client_addr, client->client_len); printf("Sent 'Correct' to %s:%d\n", inet_ntoa(client->client_addr.sin_addr), ntohs(client->client_addr.sin_port)); } else { sendto(client->socket_fd, "Wrong", strlen("Wrong"), 0, (struct sockaddr *)&client->client_addr, client->client_len); printf("Sent 'Wrong' to %s:%d\n", inet_ntoa(client->client_addr.sin_addr), ntohs(client->client_addr.sin_port)); } } }

void handle_client(struct client_info *client, struct word *words, int count) { char buf[MAX_LINE]; int n; while (1) { memset(buf, 0, MAX_LINE); n = recvfrom(client->socket_fd, buf, MAX_LINE, 0, (struct sockaddr *)&client->client_addr, &client->client_len); if (n < 0) { perror("recvfrom"); return; } printf("Received request from %s:%d\n", inet_ntoa(client->client_addr.sin_addr), ntohs(client->client_addr.sin_port)); if (strcmp(buf, "query") == 0) { handle_query(client, words, count); } else if (strcmp(buf, "quiz") == 0) { handle_quiz(client, words, count); } else { sendto(client->socket_fd, "Invalid request", strlen("Invalid request"), 0, (struct sockaddr *)&client->client_addr, client->client_len); printf("Sent 'Invalid request' to %s:%d\n", inet_ntoa(client->client_addr.sin_addr), ntohs(client->client_addr.sin_port)); } } }

int main() { int sockfd; struct sockaddr_in server_addr, client_addr; int client_len; char buf[MAX_LINE]; int n; struct word words[100]; int count = 0;

// 读取单词数据文件
FILE *fp = fopen("words.txt", "r");
if (!fp) {
    perror("fopen");
    return 1;
}
while (fgets(buf, MAX_LINE, fp)) {
    sscanf(buf, "%s %s", words[count].en, words[count].ch);
    count++;
}
fclose(fp);

// 创建UDP套接字
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
    perror("socket");
    return 1;
}

// 绑定服务器地址和端口号
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(PORT);
if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
    perror("bind");
    return 1;
}

printf("Server started\n");

while (1) {
    struct client_info client;
    client.client_len = sizeof(client_addr);
    client.socket_fd = sockfd;
    client.client_addr = client_addr;
    handle_client(&client, words, count);
}

return 0;

}

// 客户端代码

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h>

#define MAX_LINE 1024 #define SERVER_IP "127.0.0.1" #define PORT 8888

void query(int sockfd) { char buf[MAX_LINE]; int n; while (1) { printf("Enter word to query (or 'exit' to quit): "); fgets(buf, MAX_LINE, stdin); buf[strlen(buf) - 1] = '\0'; if (strcmp(buf, "exit") == 0) { break; } sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)&server_addr, sizeof(server_addr)); n = recvfrom(sockfd, buf, MAX_LINE, 0, NULL, NULL); if (n < 0) { perror("recvfrom"); return; } printf("Translation: %s\n", buf); } }

void quiz(int sockfd) { char buf[MAX_LINE]; int n; while (1) { printf("Enter 'start' to begin quiz (or 'exit' to quit): "); fgets(buf, MAX_LINE, stdin); buf[strlen(buf) - 1] = '\0'; if (strcmp(buf, "exit") == 0) { break; } if (strcmp(buf, "start") == 0) { int correct = 0; int wrong = 0; for (int i = 0; i < 10; i++) { n = recvfrom(sockfd, buf, MAX_LINE, 0, NULL, NULL); if (n < 0) { perror("recvfrom"); return; } printf("Question %d: %s\n", i + 1, buf); printf("Enter your answer: "); fgets(buf, MAX_LINE, stdin); buf[strlen(buf) - 1] = '\0'; sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)&server_addr, sizeof(server_addr)); n = recvfrom(sockfd, buf, MAX_LINE, 0, NULL, NULL); if (n < 0) { perror("recvfrom"); return; } if (strcmp(buf, "Correct") == 0) { printf("Correct!\n"); correct++; } else { printf("Wrong! The correct answer is %s.\n", buf); wrong++; } } printf("Quiz finished. You got %d correct and %d wrong.\n", correct, wrong); } else { printf("Invalid input.\n"); } } }

int main() { int sockfd; struct sockaddr_in server_addr; char buf[MAX_LINE]; int n;

// 创建UDP套接字
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
    perror("socket");
    return 1;
}

// 设置服务器地址和端口号
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
server_addr.sin_port = htons(PORT);

// 连接服务器
if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
    perror("connect");
    return 1;
}

printf("Connected to server\n");

while (1) {
    printf("Enter 'query' to query word, 'quiz' to start quiz, or 'exit' to quit: ");
    fgets(buf, MAX_LINE, stdin);
    buf[strlen(buf) - 1] = '\0';
    if (strcmp(buf, "exit") == 0) {
        break;
    }
    sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
    if (strcmp(buf, "query") == 0) {
        query(sockfd);
    } else if (strcmp(buf, "quiz") == 0) {
        quiz(sockfd);
    } else {
        printf("Invalid input.\n");
    }
}

close(sockfd);

return 0;
C语言代码基于UDP的网络单词学习软件使用UDP协议编写通信应用程序分为服务器和客户端两个部分。服务器负责为客户端提供英文单词的查询、单词测验服务其主要功能包括以下几方面。1 为客户端提供英文单词的汉语含义查询2 为客户提供单词浏览、测验功能3 管理客户端的连接、支持多用户访问3 添加、编辑和删除英文单词及其汉语含义。4 自动记录未知含义的单词。当用户查询某个英文单词时如果该单词不存在于服务器数据

原文地址: https://www.cveoy.top/t/topic/eBxO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录