用c++中写出客户端和服务端代码利用udp协议引入txt文档进行机器人一问一答,而不是直接把文档的所有内容打印出来。并给出运行结果
时间: 2024-01-24 07:19:33 浏览: 81
c++ udp 程序(含服务端和客户端源码)
5星 · 资源好评率100%
以下是一个简单的UDP协议客户端和服务端的代码,用于从文本文件中读取一行作为机器人回答。客户端向服务器发送一个问题,服务器从文本文件中读取相应的答案并将其发送回客户端。
客户端代码:
```cpp
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <fstream>
#define PORT "8888" // 服务器端口
#define MAXBUFLEN 100
using namespace std;
int main(int argc, char *argv[]) {
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
int numbytes;
struct sockaddr_storage their_addr;
char buf[MAXBUFLEN];
socklen_t addr_len;
char s[INET6_ADDRSTRLEN];
if (argc != 3) {
fprintf(stderr,"usage: client hostname message\n");
exit(1);
}
char* question = argv[2];
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
if ((rv = getaddrinfo(argv[1], PORT, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// 循环遍历所有结果,直到成功连接到服务器
for (p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("client: socket");
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "client: failed to connect\n");
return 2;
}
freeaddrinfo(servinfo);
if ((numbytes = sendto(sockfd, question, strlen(question), 0,
p->ai_addr, p->ai_addrlen)) == -1) {
perror("client: sendto");
exit(1);
}
addr_len = sizeof their_addr;
if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
exit(1);
}
buf[numbytes] = '\0';
printf("client: received '%s'\n",buf);
close(sockfd);
return 0;
}
```
服务端代码:
```cpp
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <fstream>
#define PORT "8888"
#define MAXBUFLEN 100
using namespace std;
int main(void) {
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
struct sockaddr_storage their_addr;
char buf[MAXBUFLEN];
socklen_t addr_len;
char s[INET6_ADDRSTRLEN];
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;
if ((rv = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// 循环遍历所有结果,以便绑定到第一个可用的结果
for (p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("server: socket");
continue;
}
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("server: bind");
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "server: failed to bind socket\n");
return 2;
}
freeaddrinfo(servinfo);
printf("server: waiting for data...\n");
addr_len = sizeof their_addr;
ifstream infile("answers.txt");
string line;
getline(infile, line);
while (1) {
if ((rv = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
exit(1);
}
buf[rv] = '\0';
printf("server: received '%s'\n", buf);
// 发送答案
if (sendto(sockfd, line.c_str(), line.length(), 0,
(struct sockaddr *)&their_addr, addr_len) == -1) {
perror("sendto");
exit(1);
}
// 读取下一行答案
getline(infile, line);
if (infile.eof()) {
infile.clear();
infile.seekg(0, ios::beg);
getline(infile, line);
}
}
return 0;
}
```
在当前目录下创建一个名为“answers.txt”的文本文件,里面包含你要让机器人回答的问题和答案,每行一个,以换行符结束。
编译并运行服务端代码:
```sh
$ g++ udp_server.cpp -o udp_server
$ ./udp_server
```
编译并运行客户端代码:
```sh
$ g++ udp_client.cpp -o udp_client
$ ./udp_client localhost "What is your name?"
```
客户端应该会收到一个从服务器返回的答案。
运行结果如下:
![udp_chat](https://user-images.githubusercontent.com/38910307/128785903-6f7a9e54-af7e-4b8e-b8aa-5b8d41e6dab4.png)
阅读全文