c++聊天室
时间: 2023-06-29 21:05:29 浏览: 116
C++聊天室的实现可以采用网络编程中的套接字(Socket)通信方式。实现的基本思路是:建立服务器端和客户端程序,服务器端先启动并等待客户端连接,客户端连接上服务器后,服务器与客户端之间可以实现双向通信。下面提供一个简单的C++聊天室程序示例:
1. 服务器端代码:
```cpp
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
using namespace std;
const int MAX_CLIENTS = 10; // 最大客户端数
const int BUFFER_SIZE = 1024; // 缓冲区大小
int clients[MAX_CLIENTS]; // 客户端socket数组
int client_count = 0; // 当前连接的客户端数
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // 互斥锁
void error(const char *msg)
{
perror(msg);
exit(1);
}
void broadcast(int sender, const char *msg)
{
pthread_mutex_lock(&mutex);
for (int i = 0; i < client_count; i++) {
if (clients[i] != sender) {
if (send(clients[i], msg, strlen(msg), 0) == -1) {
error("Error: send");
}
}
}
pthread_mutex_unlock(&mutex);
}
void *handle_client(void *arg)
{
int client_socket = *(int *) arg;
char buffer[BUFFER_SIZE];
while (true) {
memset(buffer, 0, BUFFER_SIZE);
if (recv(client_socket, buffer, BUFFER_SIZE, 0) == -1) {
pthread_mutex_lock(&mutex);
for (int i = 0; i < client_count; i++) {
if (clients[i] == client_socket) {
clients[i] = clients[client_count - 1];
client_count--;
break;
}
}
pthread_mutex_unlock(&mutex);
close(client_socket);
return NULL;
}
broadcast(client_socket, buffer);
}
}
int main(int argc, char *argv[])
{
if (argc < 2) {
cout << "Usage: " << argv[0] << " <port>" << endl;
return 1;
}
int server_socket, client_socket;
struct sockaddr_in server_address, client_address;
socklen_t addr_size;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
error("Error: socket");
}
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = INADDR_ANY;
server_address.sin_port = htons(atoi(argv[1]));
if (bind(server_socket, (struct sockaddr *) &server_address, sizeof(server_address)) == -1) {
error("Error: bind");
}
if (listen(server_socket, MAX_CLIENTS) == -1) {
error("Error: listen");
}
cout << "Server started." << endl;
while (true) {
addr_size = sizeof(client_address);
client_socket = accept(server_socket, (struct sockaddr *) &client_address, &addr_size);
if (client_socket == -1) {
error("Error: accept");
}
pthread_mutex_lock(&mutex);
if (client_count < MAX_CLIENTS) {
clients[client_count] = client_socket;
client_count++;
pthread_t thread;
if (pthread_create(&thread, NULL, handle_client, &client_socket) != 0) {
error("Error: pthread_create");
}
cout << "Client connected. IP address: " << inet_ntoa(client_address.sin_addr) << endl;
}
pthread_mutex_unlock(&mutex);
}
close(server_socket);
return 0;
}
```
2. 客户端代码:
```cpp
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
using namespace std;
const int BUFFER_SIZE = 1024; // 缓冲区大小
int client_socket; // 客户端socket
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // 互斥锁
void error(const char *msg)
{
perror(msg);
exit(1);
}
void *receive_message(void *arg)
{
char buffer[BUFFER_SIZE];
while (true) {
memset(buffer, 0, BUFFER_SIZE);
if (recv(client_socket, buffer, BUFFER_SIZE, 0) == -1) {
error("Error: recv");
}
cout << buffer << endl;
}
}
int main(int argc, char *argv[])
{
if (argc < 3) {
cout << "Usage: " << argv[0] << " <server_address> <port>" << endl;
return 1;
}
struct sockaddr_in server_address;
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1) {
error("Error: socket");
}
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr(argv[1]);
server_address.sin_port = htons(atoi(argv[2]));
if (connect(client_socket, (struct sockaddr *) &server_address, sizeof(server_address)) == -1) {
error("Error: connect");
}
cout << "Connected to server." << endl;
pthread_t thread;
if (pthread_create(&thread, NULL, receive_message, NULL) != 0) {
error("Error: pthread_create");
}
char buffer[BUFFER_SIZE];
while (true) {
memset(buffer, 0, BUFFER_SIZE);
fgets(buffer, BUFFER_SIZE, stdin);
if (send(client_socket, buffer, strlen(buffer), 0) == -1) {
error("Error: send");
}
}
close(client_socket);
return 0;
}
```
使用方法:
1. 在终端中先启动服务器端程序:`./server <port>`
2. 在另一个终端中启动客户端程序:`./client <server_address> <port>`
3. 在客户端终端中输入消息,回车发送
4. 在服务器端终端中可以看到客户端发送的消息,同时也可以看到其他客户端发送的消息
注意:此示例程序仅为演示用,还需要进行更多的完善和安全性的考虑。
阅读全文