socket於syslog通信

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <pthread.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include <fcntl.h>

#define SYSLOG_PORT 514

int main()
{
  struct sockaddr_in  addr;
  long     s_addr;
  int      fd;
  int      count = 10;
  char      msg_rendered[300] = "123456789";
  int      msg_rendered_len = 0;
  int           write_len = 0;
  memset((void *)&addr, 0, sizeof(struct sockaddr_in));
  

  if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) > 0)
  {
   addr.sin_family = AF_INET;
   s_addr = inet_addr("192.168.3.79");
   if (s_addr == -1)
   {
    fprintf(stderr,"WARRING: invalid ipaddress %s/n","192.168.3.79");
    close(fd);
    fd = -1;
   }
   else
   {
    addr.sin_addr.s_addr = s_addr;
    addr.sin_port = htons(SYSLOG_PORT);
    if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    {
     fprintf(stderr, "WARNING: can not connect %s:%d. %s/n", "192.168.3.79", SYSLOG_PORT, "error");
     close(fd);
     fd = -1;
    }
   }  
   
   while (count > 0)
   {
    write_len = write(fd, msg_rendered, strlen(msg_rendered));
    sleep(2);
    count --;
    printf("write_len = %d/n", write_len);
   }
   
  }
  
  
  
  
  return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章