C++利用多线程技术实现高并发访问 客户端和服务端代码
时间: 2023-11-04 12:04:19 浏览: 81
以下是一个简单的基于C++11标准的多线程TCP服务端和客户端代码示例:
服务端代码:
```c++
#include <iostream>
#include <thread>
#include <mutex>
#include <vector>
#include <cstring>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
const int MAX_CONNECTIONS = 10; // 最大连接数
std::mutex g_mutex; // 互斥量
std::vector<int> g_connections; // 客户端连接列表
void client_handler(int client_socket) {
char buffer[1024];
while (true) {
memset(buffer, 0, sizeof(buffer));
int bytes_received = recv(client_socket, buffer, sizeof(buffer), 0);
if (bytes_received <= 0) {
// 客户端断开连接
break;
}
// 处理客户端请求
// ...
// 发送响应
send(client_socket, buffer, bytes_received, 0);
}
// 关闭客户端连接
close(client_socket);
// 删除客户端连接
std::lock_guard<std::mutex> lock(g_mutex);
auto it = std::find(g_connections.begin(), g_connections.end(), client_socket);
if (it != g_connections.end()) {
g_connections.erase(it);
}
}
int main() {
int server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
std::cerr << "Failed to create server socket" << std::endl;
return 1;
}
// 绑定监听地址和端口
sockaddr_in server_address;
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(12345);
if (bind(server_socket, (sockaddr*)&server_address, sizeof(server_address)) == -1) {
std::cerr << "Failed to bind server socket" << std::endl;
return 1;
}
// 开始监听
if (listen(server_socket, MAX_CONNECTIONS) == -1) {
std::cerr << "Failed to listen on server socket" << std::endl;
return 1;
}
std::cout << "Server started" << std::endl;
while (true) {
// 等待客户端连接
sockaddr_in client_address;
socklen_t client_address_size = sizeof(client_address);
int client_socket = accept(server_socket, (sockaddr*)&client_address, &client_address_size);
if (client_socket == -1) {
std::cerr << "Failed to accept client connection" << std::endl;
continue;
}
// 添加客户端连接
std::lock_guard<std::mutex> lock(g_mutex);
if (g_connections.size() >= MAX_CONNECTIONS) {
std::cerr << "Max connections reached" << std::endl;
close(client_socket);
continue;
}
g_connections.push_back(client_socket);
// 处理客户端请求
std::thread t(client_handler, client_socket);
t.detach();
}
// 关闭服务器
close(server_socket);
return 0;
}
```
客户端代码:
```c++
#include <iostream>
#include <cstring>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
int main() {
int client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1) {
std::cerr << "Failed to create client socket" << std::endl;
return 1;
}
// 连接服务器
sockaddr_in server_address;
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
server_address.sin_port = htons(12345);
if (connect(client_socket, (sockaddr*)&server_address, sizeof(server_address)) == -1) {
std::cerr << "Failed to connect to server" << std::endl;
return 1;
}
std::cout << "Connected to server" << std::endl;
char buffer[1024];
while (true) {
// 发送请求
std::string request;
std::cout << "Enter request: ";
std::getline(std::cin, request);
if (request.empty()) {
break;
}
send(client_socket, request.c_str(), request.length(), 0);
// 接收响应
memset(buffer, 0, sizeof(buffer));
int bytes_received = recv(client_socket, buffer, sizeof(buffer), 0);
if (bytes_received <= 0) {
break;
}
// 处理响应
std::cout << "Response: " << buffer << std::endl;
}
// 关闭客户端
close(client_socket);
return 0;
}
```
阅读全文