recvfrom()
是一個用于接收來自套接字的數據的函數,通常用于無連接的數據報套接字(如 UDP)
#include<iostream>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>
const int BUFFER_SIZE = 1024;
int main() {
int sockfd;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_size;
char buffer[BUFFER_SIZE];
// 創建套接字
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
std::cerr << "Error creating socket"<< std::endl;
return 1;
}
// 配置服務器地址
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(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
std::cerr << "Error binding socket"<< std::endl;
close(sockfd);
return 1;
}
std::cout << "Server listening on port 8080..."<< std::endl;
while (true) {
// 接收數據
client_addr_size = sizeof(client_addr);
ssize_t received = recvfrom(sockfd, buffer, BUFFER_SIZE - 1, 0, (struct sockaddr *)&client_addr, &client_addr_size);
if (received < 0) {
std::cerr << "Error receiving data"<< std::endl;
continue;
}
buffer[received] = '\0';
std::cout << "Received message: "<< buffer<< std::endl;
// 發送響應
sendto(sockfd, "Message received", strlen("Message received"), 0, (struct sockaddr *)&client_addr, client_addr_size);
}
close(sockfd);
return 0;
}
這個示例展示了如何使用 recvfrom()
函數創建一個簡單的 UDP 服務器。服務器監聽端口 8080,接收客戶端發送的數據,并向客戶端發送確認消息。
#include<iostream>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>
#include<thread>
#include <mutex>
const int BUFFER_SIZE = 1024;
std::mutex mtx;
void handle_client(int sockfd, struct sockaddr_in client_addr, socklen_t client_addr_size) {
char buffer[BUFFER_SIZE];
while (true) {
// 接收數據
ssize_t received = recvfrom(sockfd, buffer, BUFFER_SIZE - 1, 0, (struct sockaddr *)&client_addr, &client_addr_size);
if (received < 0) {
std::cerr << "Error receiving data"<< std::endl;
break;
}
buffer[received] = '\0';
mtx.lock();
std::cout << "Received message from client: "<< buffer<< std::endl;
mtx.unlock();
// 發送響應
sendto(sockfd, "Message received", strlen("Message received"), 0, (struct sockaddr *)&client_addr, client_addr_size);
}
}
int main() {
int sockfd;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_size;
// 創建套接字
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
std::cerr << "Error creating socket"<< std::endl;
return 1;
}
// 配置服務器地址
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(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
std::cerr << "Error binding socket"<< std::endl;
close(sockfd);
return 1;
}
std::cout << "Server listening on port 8080..."<< std::endl;
while (true) {
// 等待客戶端連接
client_addr_size = sizeof(client_addr);
if (recvfrom(sockfd, nullptr, 0, MSG_PEEK, (struct sockaddr *)&client_addr, &client_addr_size) < 0) {
std::cerr << "Error waiting for client connection"<< std::endl;
continue;
}
// 為客戶端創建新線程
std::thread client_thread(handle_client, sockfd, client_addr, client_addr_size);
client_thread.detach();
}
close(sockfd);
return 0;
}
這個示例展示了如何使用 recvfrom()
函數創建一個多線程 UDP 服務器。服務器監聽端口 8080,為每個連接的客戶端創建一個新線程以處理請求。這樣可以同時處理多個客戶端請求。
這些示例僅用于說明 recvfrom()
函數在實際項目中的應用。在實際開發中,你可能需要根據項目需求進行更多的錯誤處理和功能實現。