以下是一个基本的多线程网络通信的 C++ 代码案例,其中有一个服务端和多个客户端,它们可以通过TCP/IP协议进行通信。
服务端代码:
#include <iostream>
#include <thread>
#include <cstring>
#include <sys/socket.h>
#include <arpa/inet.h>
using namespace std;
const int PORT = 8080;
const int MAX_CLIENTS = 10;
void clientHandler(int sockfd) {
char buffer[1024];
while (true) {
memset(buffer, 0, sizeof(buffer));
if (recv(sockfd, buffer, sizeof(buffer), 0) <= 0) {
cout << "Client disconnected" << endl;
close(sockfd);
return;
} else {
cout << "Received message: " << buffer << endl;
send(sockfd, buffer, strlen(buffer), 0);
}
}
}
int main() {
int server_fd, new_sockfd;
struct sockaddr_in address;
int addrlen = sizeof(address);
// Create socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
// Set socket options to reuse address and port
int opt = 1;
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt))) {
perror("Setsockopt failed");
exit(EXIT_FAILURE);
}
// Bind socket to a specific port
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("Bind failed");
exit(EXIT_FAILURE);
}
// Listen for incoming connections
if (listen(server_fd, MAX_CLIENTS) < 0) {
perror("Listen failed");
exit(EXIT_FAILURE);
}
cout << "Server is listening on port " << PORT << endl;
while (true) {
// Accept incoming connection
if ((new_sockfd = accept(server_fd, (struct sockaddr *)&address,
(socklen_t *)&addrlen)) < 0) {
perror("Accept failed");
exit(EXIT_FAILURE);
}
cout << "New client connected" << endl;
// Create a new thread to handle the connection
thread t(clientHandler, new_sockfd);
t.detach();
}
return 0;
}
客户端代码:
#include <iostream>
#include <cstring>
#include <sys/socket.h>
#include <arpa/inet.h>
using namespace std;
const char *SERVER_IP = "127.0.0.1";
const int SERVER_PORT = 8080;
int main() {
int sockfd;
struct sockaddr_in server_address;
// Create socket file descriptor
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
cout << "Socket creation failed" << endl;
return -1;
}
// Set server address and port
server_address.sin_family = AF_INET;
server_address.sin_port = htons(SERVER_PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if (inet_pton(AF_INET, SERVER_IP, &server_address.sin_addr) <= 0) {
cout << "Invalid address/ Address not supported" << endl;
return -1;
}
// Connect to the server
if (connect(sockfd, (struct sockaddr *)&server_address, sizeof(server_address)) < 0) {
cout << "Connection failed" << endl;
return -1;
}
cout << "Connected to server" << endl;
while (true) {
// Send a message to the server
char buffer[1024];
memset(buffer, 0, sizeof(buffer));
cout << "Enter message: ";
cin.getline(buffer, sizeof(buffer));
send(sockfd, buffer, strlen(buffer), 0);
// Receive a message from the server
memset(buffer, 0, sizeof(buffer));
recv(sockfd, buffer, sizeof(buffer), 0);
cout << "Received message: " << buffer << endl;
}
close(sockfd);
return 0;
}
在这个案例中,服务端与客户端通过 TCP/IP 进行通信,服务端使用一个线程来处理每个连接,而客户端发送消息并接收服务器的响应。当客户端断开连接时,服务端会关闭与该客户端的连接,并释放其线程。
需要注意的是,此代码未考虑一些错误处理和异常情况,仅供参考。