服务器、客户端之syslog()

#include <stdlib.h>
#include <stdio.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>

#define PATH "xxxxxx"
//#define PATH "/dev/log"

int main(int argc, char *argv[])
{
 char buf[100];
 int sock_fd, len, ret, client_fd, result, flag, val;
 pid_t fd;
 fd_set R_FD;
 struct timeval timeout;
 struct sockaddr_un server_addr, cilent_addr;


 unlink(PATH);
 sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
 if (sock_fd < 0) {
  printf("socket fail, %s\n", strerror(errno));
  return 0; 
 }
 
 if (fcntl(sock_fd, F_GETFL, flag) >= 0) {
  printf("flag: %x\n", flag);
  flag |= O_NONBLOCK;
  if (fcntl(sock_fd, F_SETFL, flag) <0)
   printf("set noblock error:%s\n", strerror(errno));
 }

 server_addr.sun_family = AF_UNIX;
 strcpy(server_addr.sun_path, PATH);
 len = sizeof(server_addr);

 val = 1;
 setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));

 ret = bind(sock_fd, (struct sockaddr *)&server_addr, len);
 if (ret < 0) {
  printf("bind error%s\n", strerror(errno));
  return 0;
 }
 listen(sock_fd, 2);
 printf("sever running...\n");
 while (1) {
  timeout.tv_sec = 10; //10 seconds
  timeout.tv_usec = 0;
  len = sizeof(cilent_addr);
  client_fd = accept(sock_fd, (struct sockaddr *)&cilent_addr, &len);
  
  /*
  if (client_fd < 0) {
   printf("client_fd: %d, %s\n", client_fd, strerror(errno));
  }
  */

  if (client_fd > 0) {
   if (fork() == 0) { //child process
    FD_ZERO(&R_FD);
    FD_SET(client_fd, &R_FD);
    printf("create child process: %d\n", getpid());
    while (1) {
     //result = select(client_fd + 1, &R_FD, NULL, NULL, &timeout); 
     result = select(client_fd + 1, &R_FD, NULL, NULL, NULL);   
     if (result > 0 && FD_ISSET(client_fd, &R_FD)) {
      memset(buf, 0, strlen(buf));
      ret = read(client_fd, buf, sizeof(buf));
      if (ret > 0) {
       printf("Server receive data: %s\n", buf);
       memset(buf, 0, sizeof(buf));
       sprintf(buf, "time: %d, pid: %d", time(NULL), getpid());
       ret = write(client_fd, buf, strlen(buf));
       if (ret > 0)
        printf("Server Send data: %s\n", buf);
      }
     }    
    }   
   }
   else //father process
   {
    close(client_fd); //涓昏繘绋嬩笉鍐嶄娇鐢ㄨ瀹㈡埛绔殑client_fd锛屽瓙杩涚▼浠ュ鐞?    continue;
   }
       
  }
  
 }
}

#include <stdlib.h>
#include <stdio.h>
#include <sys/syslog.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/time.h>
#include <errno.h>

#define PATH "xxxxxx"

int main(int argc, char *argv[])
{
 const char *ptr = "Unix Socket syslog test";
 char buf[100];
 int len, sockfd, result;
 pid_t pid;
 fd_set readfd;
 struct sockaddr_un address;
 struct timeval timeout;
 
 memset(buf, 0, sizeof(buf));
 timeout.tv_sec = 10; //2 seconds
 timeout.tv_usec = 0;
 sockfd=socket(AF_UNIX,SOCK_STREAM,0);
 if (sockfd < 0) {
  printf("%s\n", strerror(errno));
  exit(1);
 }
 address.sun_family=AF_UNIX;
 strcpy(address.sun_path,PATH);
 len=sizeof(address);
 result=connect(sockfd,(struct sockaddr *)&address,len);
 
 if(result==-1){
  perror("oops:client1");
  exit(1);
 }

 FD_ZERO(&readfd);
 FD_SET(sockfd, &readfd);
  while (1) {
  timeout.tv_sec = 10; //2 seconds
  timeout.tv_usec = 0;

  sleep(3);
  sprintf(buf, "%d", time(NULL));
  result = write(sockfd, buf, strlen(buf));  
  if (result > 0) {
   printf("Client Send data: %s\n", buf);
   memset(buf, 0, sizeof(buf));
   result = select(sockfd + 1, &readfd, NULL, NULL, &timeout);
   if (result > 0 && FD_ISSET(sockfd, &readfd)) {
    result = read(sockfd, buf, sizeof(buf));
    if (result > 0)
     printf("Client Receive data: %s\n", buf);
   }
  }
 }
}

 

结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值