能用c++写一个基于tcp协议的简易聊天机械人代码吗?能同时允许多个客户端同时连接的且分为客户端和服务器,能用vs2020写吗
时间: 2024-03-27 18:37:55 浏览: 44
C++聊天程序源程序,有服务器和客户端(Visual studio C++源代码程序)
5星 · 资源好评率100%
当然可以!以下是一个简单的基于TCP协议的聊天机器人的C++代码,它可以同时允许多个客户端连接:
服务器端代码:
```c++
#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main()
{
// 初始化Winsock
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
cout << "WSAStartup failed: " << iResult << endl;
return 1;
}
// 创建监听socket
SOCKET listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (listenSocket == INVALID_SOCKET) {
cout << "socket failed with error: " << WSAGetLastError() << endl;
WSACleanup();
return 1;
}
// 绑定监听socket
sockaddr_in serverAddr;
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddr.sin_port = htons(12345);
iResult = bind(listenSocket, (sockaddr*)&serverAddr, sizeof(serverAddr));
if (iResult == SOCKET_ERROR) {
cout << "bind failed with error: " << WSAGetLastError() << endl;
closesocket(listenSocket);
WSACleanup();
return 1;
}
// 开始监听
iResult = listen(listenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
cout << "listen failed with error: " << WSAGetLastError() << endl;
closesocket(listenSocket);
WSACleanup();
return 1;
}
// 等待客户端连接
SOCKET clientSocket;
sockaddr_in clientAddr;
int clientAddrSize = sizeof(clientAddr);
while (true) {
clientSocket = accept(listenSocket, (sockaddr*)&clientAddr, &clientAddrSize);
if (clientSocket == INVALID_SOCKET) {
cout << "accept failed with error: " << WSAGetLastError() << endl;
closesocket(listenSocket);
WSACleanup();
return 1;
}
// 显示客户端信息
char clientIpStr[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &clientAddr.sin_addr, clientIpStr, INET_ADDRSTRLEN);
cout << "Client " << clientIpStr << ":" << ntohs(clientAddr.sin_port) << " connected" << endl;
// 创建子线程处理客户端请求
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)[](LPVOID lpParam) -> DWORD {
SOCKET clientSocket = (SOCKET)lpParam;
// 接收客户端数据
char recvBuf[1024];
while (true) {
int recvLen = recv(clientSocket, recvBuf, sizeof(recvBuf), 0);
if (recvLen == SOCKET_ERROR || recvLen == 0) {
break;
}
// 处理客户端请求
// TODO: 实现机器人的聊天功能
// ...
// 发送响应给客户端
send(clientSocket, recvBuf, recvLen, 0);
}
// 关闭客户端socket
closesocket(clientSocket);
cout << "Client disconnected" << endl;
return 0;
}, (LPVOID)clientSocket, 0, NULL);
}
// 关闭监听socket
closesocket(listenSocket);
// 清理Winsock
WSACleanup();
return 0;
}
```
客户端代码:
```c++
#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main()
{
// 初始化Winsock
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
cout << "WSAStartup failed: " << iResult << endl;
return 1;
}
// 创建socket
SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (clientSocket == INVALID_SOCKET) {
cout << "socket failed with error: " << WSAGetLastError() << endl;
WSACleanup();
return 1;
}
// 连接服务器
sockaddr_in serverAddr;
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
serverAddr.sin_port = htons(12345);
iResult = connect(clientSocket, (sockaddr*)&serverAddr, sizeof(serverAddr));
if (iResult == SOCKET_ERROR) {
cout << "connect failed with error: " << WSAGetLastError() << endl;
closesocket(clientSocket);
WSACleanup();
return 1;
}
// 发送数据给服务器
char sendBuf[1024];
while (true) {
cout << "Input message: ";
cin.getline(sendBuf, sizeof(sendBuf));
if (strcmp(sendBuf, "exit") == 0) {
break;
}
send(clientSocket, sendBuf, strlen(sendBuf), 0);
// 接收服务器响应
char recvBuf[1024];
int recvLen = recv(clientSocket, recvBuf, sizeof(recvBuf), 0);
if (recvLen == SOCKET_ERROR || recvLen == 0) {
cout << "recv failed with error: " << WSAGetLastError() << endl;
break;
}
recvBuf[recvLen] = '\0';
cout << "Server response: " << recvBuf << endl;
}
// 关闭socket
closesocket(clientSocket);
// 清理Winsock
WSACleanup();
return 0;
}
```
注意:以上代码只是一个简单的示例,实际应用中还需要进行错误处理、协议设计、数据加密等方面的工作。
阅读全文