c语言中利用udp协议引入txt文档进行机器人一问一答,而不是直接把文档的所有内容打印出来。需要客户端和服务端的两个代码
时间: 2024-01-21 19:20:14 浏览: 85
使用C语言实现服务器/客户端的UDP通信
以下是一个简单的示例,包括客户端和服务端的代码。服务端将从文本文件中读取问题和答案,客户端将向服务端发送问题并接收答案。
服务端代码(server.c):
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define BUF_SIZE 1024
#define FILE_NAME "qa.txt"
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr, cli_addr;
socklen_t clilen;
char buffer[BUF_SIZE];
FILE *fp;
if (argc < 2) {
fprintf(stderr, "Usage: %s port\n", argv[0]);
exit(1);
}
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
memset(&serv_addr, 0, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
error("ERROR on binding");
clilen = sizeof(cli_addr);
fp = fopen(FILE_NAME, "r");
if (fp == NULL)
error("ERROR opening file");
while (1) {
memset(buffer, 0, BUF_SIZE);
n = recvfrom(sockfd, buffer, BUF_SIZE - 1, 0,
(struct sockaddr *)&cli_addr, &clilen);
if (n < 0)
error("ERROR receiving from socket");
buffer[n] = '\0';
char *ptr;
ptr = strstr(buffer, "?");
if (ptr != NULL) {
*ptr = '\0';
ptr++;
printf("Question: %s\n", buffer);
int found = 0;
char line[BUF_SIZE];
while (fgets(line, BUF_SIZE, fp) != NULL) {
if (strncmp(line, buffer, strlen(buffer)) == 0) {
found = 1;
break;
}
}
if (found) {
char *answer = strtok(line, "\t");
answer = strtok(NULL, "\n");
printf("Answer: %s\n", answer);
n = sendto(sockfd, answer, strlen(answer), 0,
(struct sockaddr *)&cli_addr, clilen);
if (n < 0)
error("ERROR sending to socket");
} else {
printf("Answer not found.\n");
char *not_found = "I don't know.";
n = sendto(sockfd, not_found, strlen(not_found), 0,
(struct sockaddr *)&cli_addr, clilen);
if (n < 0)
error("ERROR sending to socket");
}
rewind(fp);
}
}
fclose(fp);
close(sockfd);
return 0;
}
```
客户端代码(client.c):
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define BUF_SIZE 1024
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
char buffer[BUF_SIZE];
if (argc < 3) {
fprintf(stderr, "Usage: %s hostname port\n", argv[0]);
exit(1);
}
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
memset(&serv_addr, 0, sizeof(serv_addr));
portno = atoi(argv[2]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
if (inet_pton(AF_INET, argv[1], &serv_addr.sin_addr) <= 0)
error("ERROR invalid address");
while (1) {
printf("Question: ");
memset(buffer, 0, BUF_SIZE);
fgets(buffer, BUF_SIZE - 1, stdin);
if (buffer[strlen(buffer) - 1] == '\n')
buffer[strlen(buffer) - 1] = '\0';
n = sendto(sockfd, buffer, strlen(buffer), 0,
(struct sockaddr *)&serv_addr, sizeof(serv_addr));
if (n < 0)
error("ERROR sending to socket");
memset(buffer, 0, BUF_SIZE);
n = recvfrom(sockfd, buffer, BUF_SIZE - 1, 0, NULL, NULL);
if (n < 0)
error("ERROR receiving from socket");
buffer[n] = '\0';
printf("Answer: %s\n", buffer);
}
close(sockfd);
return 0;
}
```
注意:这只是一个简单的示例,并不具有完整性和健壮性。实际应用中需要考虑更多的因素,例如文件读取和网络通信的错误处理、并发访问等。
阅读全文