c++基于socket的多线程聊天程序
时间: 2023-08-26 22:05:19 浏览: 112
C++基于socket的多线程聊天程序.rar
5星 · 资源好评率100%
基于socket的多线程聊天程序可以使用C++语言实现。下面是一个简单的示例:
1. 客户端代码
```c++
#include <iostream>
#include <unistd.h>
#include <cstring>
#include <cstdlib>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <pthread.h>
#define MAX_MSG_SIZE 1024
#define SERVER_PORT 8080
#define SERVER_IP "127.0.0.1"
void *receive_message(void *arg) {
int sockfd = *(int *) arg;
char message[MAX_MSG_SIZE];
while (true) {
memset(message, 0, MAX_MSG_SIZE);
int len = recv(sockfd, message, MAX_MSG_SIZE, 0);
if (len > 0) {
std::cout << message << std::endl;
} else if (len == 0) {
std::cout << "Server disconnected." << std::endl;
break;
} else {
std::cerr << "Error receiving message from server." << std::endl;
break;
}
}
close(sockfd);
exit(0);
}
int main() {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
std::cerr << "Error creating socket." << std::endl;
exit(1);
}
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT);
inet_pton(AF_INET, SERVER_IP, &server_addr.sin_addr);
if (connect(sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) {
std::cerr << "Error connecting to server." << std::endl;
exit(1);
}
std::cout << "Connected to server." << std::endl;
pthread_t thread;
if (pthread_create(&thread, NULL, receive_message, &sockfd) != 0) {
std::cerr << "Error creating thread." << std::endl;
exit(1);
}
std::string message;
while (true) {
std::getline(std::cin, message);
if (message == "exit") {
break;
}
send(sockfd, message.c_str(), message.size(), 0);
}
close(sockfd);
return 0;
}
```
2. 服务器端代码
```c++
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <pthread.h>
#define MAX_MSG_SIZE 1024
#define SERVER_PORT 8080
struct client_info {
int sockfd;
struct sockaddr_in addr;
};
void *handle_client(void *arg) {
struct client_info *client = (struct client_info *) arg;
char message[MAX_MSG_SIZE];
while (true) {
memset(message, 0, MAX_MSG_SIZE);
int len = recv(client->sockfd, message, MAX_MSG_SIZE, 0);
if (len > 0) {
std::cout << inet_ntoa(client->addr.sin_addr) << ":" << ntohs(client->addr.sin_port) << " - "
<< message << std::endl;
} else if (len == 0) {
std::cout << inet_ntoa(client->addr.sin_addr) << ":" << ntohs(client->addr.sin_port)
<< " disconnected." << std::endl;
break;
} else {
std::cerr << "Error receiving message from client." << std::endl;
break;
}
}
close(client->sockfd);
delete client;
return NULL;
}
int main() {
int server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (server_sockfd < 0) {
std::cerr << "Error creating socket." << std::endl;
exit(1);
}
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(server_sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) {
std::cerr << "Error binding socket." << std::endl;
exit(1);
}
if (listen(server_sockfd, 10) < 0) {
std::cerr << "Error listening on socket." << std::endl;
exit(1);
}
std::cout << "Server started. Listening on port " << SERVER_PORT << std::endl;
while (true) {
struct sockaddr_in client_addr;
socklen_t addr_len = sizeof(client_addr);
int client_sockfd = accept(server_sockfd, (struct sockaddr *) &client_addr, &addr_len);
if (client_sockfd < 0) {
std::cerr << "Error accepting connection." << std::endl;
continue;
}
std::cout << inet_ntoa(client_addr.sin_addr) << ":" << ntohs(client_addr.sin_port)
<< " connected." << std::endl;
struct client_info *client = new client_info();
client->sockfd = client_sockfd;
client->addr = client_addr;
pthread_t thread;
if (pthread_create(&thread, NULL, handle_client, client) != 0) {
std::cerr << "Error creating thread." << std::endl;
break;
}
}
close(server_sockfd);
return 0;
}
```
该示例实现了一个简单的基于socket的多线程聊天程序,可以让客户端和服务器之间相互发送消息。在该示例中,客户端和服务器使用TCP/IP协议进行通信,客户端连接到服务器后,将通过一个线程持续接收服务器发送的消息,而主线程则负责发送消息给服务器。服务器会在接收到客户端的连接请求后,为该连接创建一个新的线程,并持续接收该客户端发送的消息。
阅读全文