C++实现多并发访问指定网站或者服务器 客户端和服务端 代码
时间: 2023-09-14 15:06:25 浏览: 108
以下是一个基本的客户端和服务端实现,利用多线程实现多并发访问指定网站或者服务器:
客户端代码:
```c++
#include <iostream>
#include <thread>
#include <vector>
#include <chrono>
#include <string>
#include <cstring>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
void send_request(int n, std::string server_ip, int server_port) {
// 创建socket
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
std::cout << "Error: Failed to create socket." << std::endl;
return;
}
// 连接服务器
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(server_port);
server_addr.sin_addr.s_addr = inet_addr(server_ip.c_str());
if (connect(sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
std::cout << "Error: Failed to connect to server." << std::endl;
close(sock);
return;
}
// 发送请求
std::string message = "Hello server " + std::to_string(n);
send(sock, message.c_str(), message.size(), 0);
// 接收响应
char buffer[1024] = {0};
int valread = read(sock, buffer, 1024);
std::cout << "Server response for client " << n << ": " << buffer << std::endl;
// 关闭socket
close(sock);
}
int main(int argc, char* argv[]) {
if (argc != 4) {
std::cout << "Usage: ./client [server_ip] [server_port] [num_clients]" << std::endl;
return 0;
}
std::string server_ip = argv[1];
int server_port = std::stoi(argv[2]);
int num_clients = std::stoi(argv[3]);
std::vector<std::thread> threads;
for (int i = 0; i < num_clients; ++i) {
threads.emplace_back(send_request, i, server_ip, server_port);
}
for (auto& thread : threads) {
thread.join();
}
return 0;
}
```
服务端代码:
```c++
#include <iostream>
#include <thread>
#include <chrono>
#include <cstring>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
void handle_connection(int sock) {
// 接收请求
char buffer[1024] = {0};
int valread = read(sock, buffer, 1024);
std::cout << "Received: " << buffer << std::endl;
// 发送响应
std::string message = "Server response: " + std::string(buffer);
send(sock, message.c_str(), message.size(), 0);
// 关闭socket
close(sock);
}
int main(int argc, char* argv[]) {
if (argc != 2) {
std::cout << "Usage: ./server [port]" << std::endl;
return 0;
}
int port = std::stoi(argv[1]);
// 创建socket
int server_sock = socket(AF_INET, SOCK_STREAM, 0);
if (server_sock < 0) {
std::cout << "Error: Failed to create socket." << std::endl;
return 0;
}
// 绑定socket
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(server_sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
std::cout << "Error: Failed to bind socket." << std::endl;
close(server_sock);
return 0;
}
// 监听socket
if (listen(server_sock, 5) < 0) {
std::cout << "Error: Failed to listen on socket." << std::endl;
close(server_sock);
return 0;
}
// 处理连接
while (true) {
int sock = accept(server_sock, NULL, NULL);
if (sock < 0) {
std::cout << "Error: Failed to accept connection." << std::endl;
continue;
}
std::thread(handle_connection, sock).detach();
}
return 0;
}
```
在使用时,可以在终端中分别运行客户端和服务端程序:
```bash
# 启动服务端
$ ./server 8888
# 启动多个客户端
$ ./client 127.0.0.1 8888 10
```
其中,`./server 8888` 表示启动服务端程序,监听本地 8888 端口;`./client 127.0.0.1 8888 10` 表示启动 10 个客户端程序,分别连接到本地 8888 端口。
阅读全文