有没有办法在 C++ 代码中向远程系统日志发送消息?
相当于 linux 命令中的这一行:
nc -w0 -u 192.168.1.1 514 <<< "logging from remote"
这段代码应该用套接字做一些事情,但它不起作用。
int main(int argc, char *argv[])
{
int listenfd = 0, connfd = 0,n;
struct sockaddr_in serv_addr;
struct hostent *server;
char sendBuff[1025];
sprintf(sendBuff,"bla bla");
time_t ticks;
listenfd = socket(AF_INET, SOCK_DGRAM , 0);
memset(&serv_addr, '0', sizeof(serv_addr));
memset(sendBuff, '0', sizeof(sendBuff));
serv_addr.sin_family = AF_INET;
server = gethostbyname("192.168.1.108");
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
//serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(514);
if ((n=connect(connfd,(const sockaddr*)&serv_addr,sizeof(serv_addr))) < 0){
printf(" Connection to has failed Server ");
}
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
listen(listenfd, 10);
while(1)
{
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
write(connfd, sendBuff, strlen(sendBuff));
close(connfd);
sleep(60);
}
}