Linux下C++使用多线程技术实现多客户端并发访问指定服务器 客户端和服务端代码
时间: 2023-11-09 22:05:38 浏览: 80
LINUX下的一个多线程的服务器和客户端的网络传输程序
5星 · 资源好评率100%
以下是一个简单的多线程 C++ 客户端和服务端代码,可以实现多客户端并发访问指定服务器:
### 服务端代码
```c++
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
using namespace std;
#define PORT 8888
#define MAX_CLIENTS 10
#define BUFFER_SIZE 1024
void* handle_client(void* client_socket_void_ptr) {
int client_socket = *(int*)client_socket_void_ptr;
char buffer[BUFFER_SIZE];
while (true) {
memset(buffer, 0, sizeof(buffer));
int bytes_received = recv(client_socket, buffer, sizeof(buffer), 0);
if (bytes_received == -1) {
cerr << "Error in recv." << endl;
close(client_socket);
return NULL;
} else if (bytes_received == 0) {
cout << "Client disconnected." << endl;
close(client_socket);
return NULL;
}
cout << "Received: " << buffer << endl;
string response = "Server received: ";
response += buffer;
int bytes_sent = send(client_socket, response.c_str(), response.length(), 0);
if (bytes_sent == -1) {
cerr << "Error in send." << endl;
close(client_socket);
return NULL;
}
}
}
int main() {
int server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
cerr << "Error creating server socket." << endl;
return 1;
}
sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(PORT);
server_address.sin_addr.s_addr = INADDR_ANY;
if (bind(server_socket, (sockaddr*)&server_address, sizeof(server_address)) == -1) {
cerr << "Error binding server socket." << endl;
return 1;
}
if (listen(server_socket, MAX_CLIENTS) == -1) {
cerr << "Error listening on server socket." << endl;
return 1;
}
cout << "Server started. Listening on port " << PORT << "." << 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) {
cerr << "Error accepting client socket." << endl;
continue;
}
cout << "Client connected from " << inet_ntoa(client_address.sin_addr) << ":" << ntohs(client_address.sin_port) << "." << endl;
pthread_t thread;
if (pthread_create(&thread, NULL, handle_client, &client_socket) != 0) {
cerr << "Error creating thread." << endl;
close(client_socket);
continue;
}
pthread_detach(thread);
}
close(server_socket);
return 0;
}
```
### 客户端代码
```c++
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
using namespace std;
#define PORT 8888
#define BUFFER_SIZE 1024
void* receive_messages(void* socket_void_ptr) {
int socket = *(int*)socket_void_ptr;
char buffer[BUFFER_SIZE];
while (true) {
memset(buffer, 0, sizeof(buffer));
int bytes_received = recv(socket, buffer, sizeof(buffer), 0);
if (bytes_received == -1) {
cerr << "Error in recv." << endl;
close(socket);
exit(1);
} else if (bytes_received == 0) {
cout << "Server disconnected." << endl;
close(socket);
exit(0);
}
cout << "Received: " << buffer << endl;
}
}
int main(int argc, char** argv) {
if (argc != 2) {
cerr << "Usage: " << argv[0] << " <server_ip_address>" << endl;
return 1;
}
int client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1) {
cerr << "Error creating client socket." << endl;
return 1;
}
sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(PORT);
if (inet_pton(AF_INET, argv[1], &server_address.sin_addr) == -1) {
cerr << "Error converting server IP address." << endl;
return 1;
}
if (connect(client_socket, (sockaddr*)&server_address, sizeof(server_address)) == -1) {
cerr << "Error connecting to server." << endl;
return 1;
}
cout << "Connected to server " << argv[1] << ":" << PORT << "." << endl;
pthread_t thread;
if (pthread_create(&thread, NULL, receive_messages, &client_socket) != 0) {
cerr << "Error creating thread." << endl;
close(client_socket);
return 1;
}
string message;
while (true) {
getline(cin, message);
if (message.length() == 0) {
continue;
}
int bytes_sent = send(client_socket, message.c_str(), message.length(), 0);
if (bytes_sent == -1) {
cerr << "Error in send." << endl;
close(client_socket);
return 1;
}
}
close(client_socket);
return 0;
}
```
在 Linux 系统中编译这些代码可以使用以下命令:
```
g++ -pthread server.cpp -o server
g++ -pthread client.cpp -o client
```
然后分别运行 `server` 和 `client` 可执行文件即可。
阅读全文