写出一个c++代码的简单聊天室
时间: 2023-07-30 15:06:07 浏览: 75
一个C++写的聊天室
由于一个完整的聊天室涉及到较多的代码实现,我在这里提供一个简单的 C++ Socket 编程实现的聊天室示例代码,供您参考。
服务端代码:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
const int PORT = 8080;
const int MAX_CLIENTS = 10;
const int BUFFER_SIZE = 1024;
vector<SOCKET> clients;
void broadcast(const char* message, SOCKET sender)
{
for (SOCKET client : clients) {
if (client != sender) {
send(client, message, strlen(message), 0);
}
}
}
int main()
{
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
cout << "WSAStartup failed with error: " << iResult << endl;
return 1;
}
SOCKET listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (listenSocket == INVALID_SOCKET) {
cout << "socket failed with error: " << WSAGetLastError() << endl;
WSACleanup();
return 1;
}
sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = INADDR_ANY;
serverAddress.sin_port = htons(PORT);
iResult = bind(listenSocket, (sockaddr*)&serverAddress, sizeof(serverAddress));
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;
}
cout << "Server started, listening on port " << PORT << endl;
while (true) {
SOCKET clientSocket = accept(listenSocket, NULL, NULL);
if (clientSocket == INVALID_SOCKET) {
cout << "accept failed with error: " << WSAGetLastError() << endl;
closesocket(listenSocket);
WSACleanup();
return 1;
}
clients.push_back(clientSocket);
cout << "New client connected, total " << clients.size() << " clients" << endl;
char welcomeMessage[BUFFER_SIZE] = "Welcome to the chat room!\r\n";
send(clientSocket, welcomeMessage, strlen(welcomeMessage), 0);
while (true) {
char buffer[BUFFER_SIZE] = "";
iResult = recv(clientSocket, buffer, BUFFER_SIZE, 0);
if (iResult == SOCKET_ERROR) {
cout << "recv failed with error: " << WSAGetLastError() << endl;
closesocket(clientSocket);
clients.erase(find(clients.begin(), clients.end(), clientSocket));
break;
}
else if (iResult == 0) {
closesocket(clientSocket);
clients.erase(find(clients.begin(), clients.end(), clientSocket));
cout << "Client disconnected, total " << clients.size() << " clients" << endl;
break;
}
else {
buffer[iResult] = '\0';
cout << "Received message from client: " << buffer << endl;
broadcast(buffer, clientSocket);
}
}
}
closesocket(listenSocket);
WSACleanup();
return 0;
}
```
客户端代码:
```c++
#include <iostream>
#include <string>
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
const int PORT = 8080;
const int BUFFER_SIZE = 1024;
int main()
{
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
cout << "WSAStartup failed with error: " << iResult << endl;
return 1;
}
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 serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
serverAddress.sin_port = htons(PORT);
iResult = connect(clientSocket, (sockaddr*)&serverAddress, sizeof(serverAddress));
if (iResult == SOCKET_ERROR) {
cout << "connect failed with error: " << WSAGetLastError() << endl;
closesocket(clientSocket);
WSACleanup();
return 1;
}
char buffer[BUFFER_SIZE] = "";
iResult = recv(clientSocket, buffer, BUFFER_SIZE, 0);
if (iResult == SOCKET_ERROR) {
cout << "recv failed with error: " << WSAGetLastError() << endl;
closesocket(clientSocket);
WSACleanup();
return 1;
}
else {
cout << buffer << endl;
}
while (true) {
cout << "Enter your message: ";
string message;
getline(cin, message);
iResult = send(clientSocket, message.c_str(), message.length(), 0);
if (iResult == SOCKET_ERROR) {
cout << "send failed with error: " << WSAGetLastError() << endl;
closesocket(clientSocket);
WSACleanup();
return 1;
}
}
closesocket(clientSocket);
WSACleanup();
return 0;
}
```
这段代码实现了一个简单的聊天室,服务端可以接受多个客户端连接,并将客户端发送的消息广播给其他客户端。客户端可以连接到服务端,并发送消息到服务端。需要注意的是,这段代码没有实现用户界面和消息协议,只是一个简单的 Socket 编程示例。
阅读全文