c++实现简单的即时通讯软件代码
时间: 2023-08-25 18:04:37 浏览: 85
以下是一个简单的C++即时通讯软件的实现代码,包括服务器和客户端:
服务器端代码:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
using namespace std;
const int PORT = 8888;
struct Client {
int fd;
string name;
bool online;
};
vector<Client> clients;
mutex mtx;
condition_variable cv;
void broadcast(string msg, int exclude_fd = -1) {
unique_lock<mutex> lock(mtx);
for (auto& client : clients) {
if (client.fd != exclude_fd && client.online) {
send(client.fd, msg.c_str(), msg.size(), 0);
}
}
}
void handle_client(int fd) {
char buf[1024];
int len;
string name;
// 接收客户端发送的昵称
len = recv(fd, buf, sizeof(buf), 0);
if (len <= 0) {
close(fd);
return;
}
name = string(buf, len - 1);
// 添加到客户端列表
{
unique_lock<mutex> lock(mtx);
clients.push_back({fd, name, true});
}
// 广播新用户加入
string msg = name + " joined the chat room\n";
broadcast(msg);
// 接收客户端发送的消息并广播给其他客户端
while ((len = recv(fd, buf, sizeof(buf), 0)) > 0) {
string msg = name + ": " + string(buf, len);
broadcast(msg, fd);
}
// 客户端下线
{
unique_lock<mutex> lock(mtx);
for (auto& client : clients) {
if (client.fd == fd) {
client.online = false;
break;
}
}
}
// 广播用户离线
string msg = name + " left the chat room\n";
broadcast(msg);
close(fd);
}
int main() {
int server_fd, client_fd;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_len;
// 创建socket
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) {
perror("socket");
exit(1);
}
// 绑定地址和端口
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("bind");
exit(1);
}
// 监听端口
if (listen(server_fd, 5) < 0) {
perror("listen");
exit(1);
}
// 接收客户端连接并创建线程处理
while (true) {
client_addr_len = sizeof(client_addr);
client_fd = accept(server_fd, (struct sockaddr*)&client_addr, &client_addr_len);
if (client_fd < 0) {
perror("accept");
continue;
}
// 创建线程处理客户端请求
thread t(handle_client, client_fd);
t.detach();
}
close(server_fd);
return 0;
}
```
客户端代码:
```c++
#include <iostream>
#include <string>
#include <thread>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
using namespace std;
const int PORT = 8888;
void receive_msg(int fd) {
char buf[1024];
int len;
// 接收服务器发送的消息并显示在屏幕上
while ((len = recv(fd, buf, sizeof(buf), 0)) > 0) {
buf[len] = '\0';
cout << buf;
}
}
int main() {
int client_fd;
struct sockaddr_in server_addr;
// 创建socket
client_fd = socket(AF_INET, SOCK_STREAM, 0);
if (client_fd < 0) {
perror("socket");
exit(1);
}
// 连接服务器
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
server_addr.sin_port = htons(PORT);
if (connect(client_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("connect");
exit(1);
}
// 发送昵称
string name;
cout << "Enter your name: ";
getline(cin, name);
send(client_fd, name.c_str(), name.size() + 1, 0);
// 创建线程接收消息
thread t(receive_msg, client_fd);
t.detach();
// 输入消息并发送给服务器
string msg;
while (getline(cin, msg)) {
send(client_fd, msg.c_str(), msg.size() + 1, 0);
}
close(client_fd);
return 0;
}
```
运行以上代码,可以在同一台电脑上启动多个客户端,进行简单的即时通讯。
阅读全文