【问题标题】:what wrong with "<>" missing缺少“<>”有什么问题
【发布时间】:2015-10-20 08:07:14
【问题描述】:

我写了一个程序来发送系统日志到远程服务器。我的部分代码如下:

sprintf(syslogBuf,"<%d>LogTime=\"%s\";user=\"%s\";IP=\"%s\";Sip=\"%s\";OpType=\"%s\";OpResult=\"%d\";OpText=\"%s %s\"",
        pri,p[0],p[1],IP,p[2],p[3],flag,p[1],p[3]);

我使用sendto()syslogBuf 发送到远程sylog 服务器。但是服务器收到的是:

LogTime="2015-10-20 14:33:57";user="root";IP="127.0.1.12";Sip="127.0.1.14";OpType="show meminfo";OpResult="0";OpText="root show meminfo"

为什么&lt;%d&gt; 错过了? 发送代码如下:

if(UdpSendData(sockfd,syslogBuf,strlen(syslogBuf),syslogServer,SYSLOGPORT) == -1);
perror("send failed");

UdpSendData:

int UdpSendData(int sockfd,const char *buf, UINT32 dataLen, char *remoteIP, int remotePort)
{

struct sockaddr_in RemoteAddr;
int len;

if( sockfd <= -1)
{
    printf("%s send fail,sock error.\n",__FUNCTION__);
    return -1;
}
if((buf == NULL)||(remoteIP==NULL))
{
    printf("%s send fail,buf error or remote ip error.\n",__FUNCTION__);
    return -1;
}
//printf("udp send remote ip %s port %d.\n",remoteIP,remotePort);

memset(&RemoteAddr,0,sizeof(RemoteAddr));  
RemoteAddr.sin_family = AF_INET;
RemoteAddr.sin_port = htons( remotePort );
RemoteAddr.sin_addr.s_addr = inet_addr( remoteIP );
int addrLen = sizeof(RemoteAddr);

len = sendto(sockfd, buf, dataLen, 0, (struct sockaddr*)&RemoteAddr, addrLen); 

if  (len <= 0)
{   
    printf("%s udp send to %s %d fail.\n",__FUNCTION__,remoteIP,remotePort);
    return -1;
}   
return len;

}

【问题讨论】:

  • syslogbuf 是否包含您在发送之前 所期望的内容?
  • 里面包含了我需要发送的内容。在发送之前,我将它打印到stdout,它显示了包括&lt;&gt;在内的所有信息
  • 如果使用{..}而不是尖括号,文本是否会出现?
  • @EK.CR 请向我们展示代码的发送部分。
  • 您是如何确定没有发送 的? syslog 会将其解释为优先级,它不会出现在 syslog 实际记录的消息中。

标签: c udp syslog sendto


【解决方案1】:
<%d>

已发送,但 syslog 将其解释为优先级标记而不是消息的一部分。您可以简单地改用[%d](%d)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 2014-08-08
    • 2021-12-02
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多