您好,登錄后才能下訂單哦!
在C++中,使用Socket庫進(jìn)行多線程應(yīng)用開發(fā)時,需要注意線程安全和資源管理。以下是一個簡單的示例,展示了如何在多線程環(huán)境中使用Socket庫進(jìn)行通信。
首先,需要包含必要的頭文件并創(chuàng)建一個基本的Socket服務(wù)器:
#include <iostream>
#include <thread>
#include <mutex>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
std::mutex mtx;
void handle_client(int client_socket) {
char buffer[1024] = {0};
while (true) {
int len = recv(client_socket, buffer, sizeof(buffer) - 1, 0);
if (len <= 0) {
break;
}
std::lock_guard<std::mutex> lock(mtx);
std::cout << "Received from client: " << buffer << std::endl;
send(client_socket, buffer, len, 0);
}
close(client_socket);
}
int main() {
int server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
std::cerr << "Error creating socket" << 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 = INADDR_ANY;
server_addr.sin_port = htons(8080);
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
std::cerr << "Error binding socket" << std::endl;
return 1;
}
if (listen(server_socket, 3) == -1) {
std::cerr << "Error listening on socket" << std::endl;
return 1;
}
while (true) {
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
int client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_len);
if (client_socket == -1) {
std::cerr << "Error accepting client connection" << std::endl;
continue;
}
std::thread client_thread(handle_client, client_socket);
client_thread.detach();
}
close(server_socket);
return 0;
}
在這個示例中,我們創(chuàng)建了一個簡單的Socket服務(wù)器,監(jiān)聽8080端口。當(dāng)客戶端連接時,服務(wù)器會創(chuàng)建一個新線程來處理客戶端的請求。我們使用std::mutex
來確保在多線程環(huán)境下對共享資源(如std::cout
)的安全訪問。
客戶端可以使用以下代碼連接到服務(wù)器并發(fā)送接收消息:
#include <iostream>
#include <thread>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
void handle_client() {
int client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1) {
std::cerr << "Error creating socket" << std::endl;
return;
}
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(8080);
if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
std::cerr << "Error connecting to server" << std::endl;
return;
}
std::string message = "Hello, server!";
send(client_socket, message.c_str(), message.size(), 0);
char buffer[1024] = {0};
int len = recv(client_socket, buffer, sizeof(buffer) - 1, 0);
std::cout << "Received from server: " << buffer << std::endl;
close(client_socket);
}
int main() {
std::thread client_thread(handle_client);
client_thread.join();
return 0;
}
這個示例展示了如何在多線程環(huán)境中使用Socket庫進(jìn)行簡單的客戶端和服務(wù)器通信。請注意,這個示例僅用于演示目的,實際應(yīng)用中可能需要處理更多的錯誤情況和邊緣情況。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。