生成一份可以在vs2022上运行的c++多人聊天室代码
时间: 2023-10-01 20:09:09 浏览: 262
好的,这里提供一份在VS2022上可以运行的基于C++的多人聊天室代码。该代码使用TCP协议进行通信,支持多个客户端同时连接服务器并进行聊天。
```
#include <iostream>
#include <winsock2.h>
#include <WS2tcpip.h>
#include <vector>
#include <string>
#pragma comment(lib, "Ws2_32.lib")
#define DEFAULT_PORT "27015"
#define DEFAULT_BUFLEN 512
int main() {
WSADATA wsaData;
int iResult;
// 初始化Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
std::cout << "WSAStartup failed: " << iResult << std::endl;
return 1;
}
// 创建Socket
struct addrinfo* result = NULL, * ptr = NULL, hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
std::cout << "getaddrinfo failed: " << iResult << std::endl;
WSACleanup();
return 1;
}
SOCKET ListenSocket = INVALID_SOCKET;
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
std::cout << "Error at socket(): " << WSAGetLastError() << std::endl;
freeaddrinfo(result);
WSACleanup();
return 1;
}
// 绑定Socket
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
std::cout << "bind failed with error: " << WSAGetLastError() << std::endl;
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
return 1;
}
freeaddrinfo(result);
// 监听Socket
if (listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR) {
std::cout << "listen failed with error: " << WSAGetLastError() << std::endl;
closesocket(ListenSocket);
WSACleanup();
return 1;
}
std::cout << "Server started, listening on port " << DEFAULT_PORT << std::endl;
// 等待客户端连接
std::vector<SOCKET> ClientSockets;
while (true) {
SOCKET ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
std::cout << "accept failed with error: " << WSAGetLastError() << std::endl;
closesocket(ListenSocket);
WSACleanup();
return 1;
}
ClientSockets.push_back(ClientSocket);
std::cout << "New client connected" << std::endl;
// 处理客户端消息
std::string msg;
while (true) {
char buf[DEFAULT_BUFLEN] = { 0 };
iResult = recv(ClientSocket, buf, DEFAULT_BUFLEN, 0);
if (iResult > 0) {
msg += std::string(buf, iResult);
if (msg.back() == '\n') {
std::cout << "Message received: " << msg;
// 将消息广播给所有客户端
for (auto it = ClientSockets.begin(); it != ClientSockets.end(); ++it) {
iResult = send(*it, msg.c_str(), msg.length(), 0);
if (iResult == SOCKET_ERROR) {
std::cout << "send failed with error: " << WSAGetLastError() << std::endl;
closesocket(ListenSocket);
WSACleanup();
return 1;
}
}
msg.clear();
}
}
else if (iResult == 0) {
std::cout << "Connection closed" << std::endl;
closesocket(ClientSocket);
break;
}
else {
std::cout << "recv failed with error: " << WSAGetLastError() << std::endl;
closesocket(ClientSocket);
WSACleanup();
return 1;
}
}
}
// 关闭Socket和Winsock
closesocket(ListenSocket);
WSACleanup();
return 0;
}
```
上述代码中,首先初始化Winsock库,然后创建Socket,绑定Socket并开始监听客户端连接。当有客户端连接时,将客户端Socket加入到`ClientSockets`向量中,并在接收到客户端发来的消息时将该消息广播给所有客户端。如果客户端断开连接,则将其Socket从`ClientSockets`向量中删除。
请注意,在运行此代码之前,您需要在VS2022中打开项目属性,添加`Ws2_32.lib`库文件,以便正确链接Winsock库。
希望这份代码对你有帮助。
阅读全文