您好,登錄后才能下訂單哦!
在C++中,使用Socket庫進行日志記錄時,通常需要將日志消息發送到遠程服務器或本地文件。為了實現這一功能,我們可以使用C++的Socket編程技巧來創建一個簡單的日志記錄客戶端或服務端。
以下是一個簡單的C++ Socket日志記錄客戶端示例:
#include <iostream>
#include <string>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define SERVER_IP "127.0.0.1"
#define SERVER_PORT 12345
#define BUFFER_SIZE 1024
void logMessage(const std::string& message) {
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
std::cerr << "Error creating socket" << std::endl;
return;
}
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(SERVER_PORT);
serverAddr.sin_addr.s_addr = inet_addr(SERVER_IP);
if (connect(sock, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == -1) {
std::cerr << "Error connecting to server" << std::endl;
close(sock);
return;
}
send(sock, message.c_str(), message.size(), 0);
close(sock);
}
int main() {
std::string logMessage = "This is a test log message";
logMessage += "\n";
logMessage += "------------------------------------\n";
logMessage(logMessage);
return 0;
}
以下是一個簡單的C++ Socket日志記錄服務端示例:
#include <iostream>
#include <string>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fstream>
#define SERVER_IP "127.0.0.1"
#define SERVER_PORT 12345
#define BUFFER_SIZE 1024
void logMessage(const std::string& message) {
std::ofstream logFile("log.txt", std::ios::app);
if (!logFile.is_open()) {
std::cerr << "Error opening log file" << std::endl;
return;
}
logFile << message;
logFile.close();
}
int main() {
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == -1) {
std::cerr << "Error creating socket" << std::endl;
return 1;
}
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(SERVER_PORT);
serverAddr.sin_addr.s_addr = INADDR_ANY;
if (bind(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == -1) {
std::cerr << "Error binding socket" << std::endl;
close(serverSocket);
return 1;
}
if (listen(serverSocket, 5) == -1) {
std::cerr << "Error listening on socket" << std::endl;
close(serverSocket);
return 1;
}
struct sockaddr_in clientAddr;
socklen_t clientAddrSize = sizeof(clientAddr);
int clientSocket = accept(serverSocket, (struct sockaddr *)&clientAddr, &clientAddrSize);
if (clientSocket == -1) {
std::cerr << "Error accepting client connection" << std::endl;
close(serverSocket);
return 1;
}
char buffer[BUFFER_SIZE];
while (true) {
ssize_t bytesReceived = recv(clientSocket, buffer, BUFFER_SIZE - 1, 0);
if (bytesReceived == -1) {
std::cerr << "Error receiving data from client" << std::endl;
break;
} else if (bytesReceived == 0) {
std::cerr << "Client disconnected" << std::endl;
break;
}
buffer[bytesReceived] = '\0';
logMessage(buffer);
}
close(clientSocket);
close(serverSocket);
return 0;
}
在這個示例中,我們創建了一個簡單的日志記錄服務端,它監聽指定的端口并接收客戶端發送的日志消息。接收到的日志消息將被追加到名為log.txt
的文件中。同時,我們還提供了一個簡單的日志記錄客戶端,用于將日志消息發送到服務端。
請注意,這個示例僅用于演示目的,實際應用中可能需要考慮更多的錯誤處理和功能。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。