用c++中写出客户端和服务端代码利用udp协议引入txt文档进行机器人一问一答,而不是直接把文档的所有内容打印出来。
时间: 2024-01-22 22:20:52 浏览: 70
以下是一个简单的UDP客户端和服务端代码,可以引入txt文档进行机器人一问一答。在这个示例中,客户端将从文本文件中读取问题,并将其发送到服务器。服务器将从文本文件中读取答案,并将其发送回客户端。
客户端代码:
```cpp
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using namespace std;
const int BUFFER_SIZE = 1024;
const int SERVER_PORT = 8888;
const char* SERVER_IP = "127.0.0.1";
int main() {
// 创建UDP套接字
int client_socket = socket(AF_INET, SOCK_DGRAM, 0);
if (client_socket < 0) {
cerr << "Failed to create socket." << endl;
return -1;
}
// 设置服务器地址
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT);
server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
// 从文件中读取问题并发送到服务器
string line;
ifstream infile("questions.txt");
char buffer[BUFFER_SIZE];
while (getline(infile, line)) {
strcpy(buffer, line.c_str());
int len = strlen(buffer);
sendto(client_socket, buffer, len, 0, (struct sockaddr*)&server_addr, sizeof(server_addr));
// 接收服务器的答案
memset(buffer, 0, BUFFER_SIZE);
int recv_len = recvfrom(client_socket, buffer, BUFFER_SIZE, 0, NULL, NULL);
if (recv_len > 0) {
cout << "Q: " << line << endl;
cout << "A: " << buffer << endl;
}
}
// 关闭套接字和文件
close(client_socket);
infile.close();
return 0;
}
```
服务端代码:
```cpp
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using namespace std;
const int BUFFER_SIZE = 1024;
const int SERVER_PORT = 8888;
const char* SERVER_IP = "127.0.0.1";
int main() {
// 创建UDP套接字
int server_socket = socket(AF_INET, SOCK_DGRAM, 0);
if (server_socket < 0) {
cerr << "Failed to create socket." << endl;
return -1;
}
// 绑定服务器地址
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT);
server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
cerr << "Failed to bind address." << endl;
close(server_socket);
return -1;
}
// 从文件中读取答案并发送到客户端
string line;
ifstream infile("answers.txt");
char buffer[BUFFER_SIZE];
while (getline(infile, line)) {
strcpy(buffer, line.c_str());
int len = strlen(buffer);
// 接收来自客户端的问题
memset(buffer, 0, BUFFER_SIZE);
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
int recv_len = recvfrom(server_socket, buffer, BUFFER_SIZE, 0, (struct sockaddr*)&client_addr, &client_addr_len);
if (recv_len > 0) {
cout << "Q: " << buffer << endl;
sendto(server_socket, line.c_str(), len, 0, (struct sockaddr*)&client_addr, client_addr_len);
}
}
// 关闭套接字和文件
close(server_socket);
infile.close();
return 0;
}
```
在这个示例中,我们假设有两个文本文件:questions.txt 包含一系列问题,每行一个问题;answers.txt 包含一系列答案,每行一个答案。
当客户端运行时,它会从 questions.txt 文件中读取问题,并将其发送到服务器。服务器会接收问题,从 answers.txt 文件中读取相应的答案,并将其发送回客户端。客户端接收答案并将其打印出来。整个过程会一直进行,直到读取完所有的问题和答案。
这只是一个简单的示例,实际上,您可能需要更多的代码来处理错误和异常情况,并确保程序的正确性和稳定性。
阅读全文