C/C++设计高并发访问:多客户端并发访问服务器 服务器端和客户端 代码
时间: 2023-06-12 22:07:38 浏览: 138
这个问题涉及到的知识点比较广泛,需要包括网络编程、并发编程等方面的内容。
首先,服务器端需要创建一个监听套接字,并绑定到指定的IP地址和端口上,然后通过调用listen函数将该套接字设置为监听状态。
接着,服务器端需要采用多线程或多进程的方式,对于每个新的客户端连接,都创建一个新的线程或进程来处理,避免单线程或单进程的情况下,一个客户端的阻塞操作会影响到其他客户端的正常访问。
在服务器端线程或进程中,需要通过accept函数接收客户端的连接请求,并获得与该客户端通信的套接字,然后将该套接字交给一个新的线程或进程来处理客户端请求。
对于客户端,需要创建一个套接字,并向服务器端发起连接请求。如果连接成功,则可以通过send和recv函数与服务器端进行数据交换。
下面是一个简单的C++实现:
服务器端代码:
```cpp
#include <iostream>
#include <cstring>
#include <thread>
#include <mutex>
#include <vector>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
std::mutex mtx;
void handle_client(int client_socket) {
// 处理客户端请求
char buffer[1024];
while(true) {
memset(buffer, 0, sizeof(buffer));
int ret = recv(client_socket, buffer, sizeof(buffer), 0);
if(ret <= 0) {
break;
}
std::cout << "Receive message from client: " << buffer << std::endl;
std::string reply = "Hello, I am server!";
send(client_socket, reply.c_str(), reply.size(), 0);
}
close(client_socket);
}
void start_server(int port) {
// 创建监听套接字
int server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(server_socket < 0) {
std::cerr << "Create socket failed!" << std::endl;
return;
}
// 绑定IP地址和端口
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(port);
int ret = bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr));
if(ret < 0) {
std::cerr << "Bind socket failed!" << std::endl;
close(server_socket);
return;
}
// 监听
ret = listen(server_socket, 10);
if(ret < 0) {
std::cerr << "Listen socket failed!" << std::endl;
close(server_socket);
return;
}
std::cout << "Server start listening on port " << port << " ..." << std::endl;
// 处理客户端请求
while(true) {
struct sockaddr_in client_addr;
socklen_t client_addrlen = sizeof(client_addr);
int client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_addrlen);
if(client_socket < 0) {
std::cerr << "Accept socket failed!" << std::endl;
continue;
}
std::cout << "Accept client connection from " << inet_ntoa(client_addr.sin_addr) << ":" << ntohs(client_addr.sin_port) << std::endl;
std::thread th(handle_client, client_socket);
th.detach();
}
close(server_socket);
}
int main() {
int port = 9000;
start_server(port);
return 0;
}
```
客户端代码:
```cpp
#include <iostream>
#include <cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main() {
// 创建套接字
int client_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(client_socket < 0) {
std::cerr << "Create socket failed!" << std::endl;
return -1;
}
// 连接服务器
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
server_addr.sin_port = htons(9000);
int ret = connect(client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr));
if(ret < 0) {
std::cerr << "Connect server failed!" << std::endl;
close(client_socket);
return -1;
}
std::cout << "Connect server success!" << std::endl;
// 发送和接收数据
std::string message = "Hello, I am client!";
send(client_socket, message.c_str(), message.size(), 0);
char buffer[1024];
memset(buffer, 0, sizeof(buffer));
ret = recv(client_socket, buffer, sizeof(buffer), 0);
std::cout << "Receive message from server: " << buffer << std::endl;
// 关闭套接字
close(client_socket);
return 0;
}
```
阅读全文