您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
在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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。