论坛首页 入门技术论坛

Linux UDP EchoServer守护进程编写

浏览 2584 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-11-13  
平时工作中主要用java,不过还是对Linux非常喜爱,虽然学习时间有限,每天还是能学点小东西,呵呵,写了一个HelloWorld版的Linux守护进程,启动后监听UDP端口做简单的echo back

Server代码

#include<sys/socket.h>
#include<netinet/in.h>
#include<strings.h>
#include<stdio.h>
#include<syslog.h>
#include<stdlib.h>
#include<signal.h>
#include        <sys/types.h>   /* basic system data types */
#include        <sys/socket.h>  /* basic socket definitions */
#include        <sys/time.h>    /* timeval{} for select() */
#include        <time.h>                /* timespec{} for pselect() */
#include        <netinet/in.h>  /* sockaddr_in{} and other Internet defns */
#include        <arpa/inet.h>   /* inet(3) functions */
#include        <errno.h>
#include        <fcntl.h>               /* for nonblocking */
#include        <netdb.h>
#include        <signal.h>
#include        <stdio.h>
#include        <stdlib.h>
#include        <string.h>
#include        <sys/stat.h>    /* for S_xxx file mode constants */
#include        <sys/uio.h>             /* for iovec{} and readv/writev */
#include        <unistd.h>
#include        <sys/wait.h>
#include        <sys/un.h>              /* for Unix domain sockets */
int init_deamon(){
  pid_t pid;
  if((pid=fork())<0){
     return (-1);
  }else if(pid){
    exit(0);
  }
  if(setsid()<0){
    return (-1);
  }
  signal(SIGHUP,SIG_IGN);
  if((pid=fork())<0){
     return (-1);
  }else if(pid){
    exit(0);
  }
  chdir("/");
  int i=0;
  for(;i<64;i++){
    close(i);
  }
  open("/dev/null",O_RDONLY);
  open("/dev/null",O_RDWR);
  open("/dev/null",O_RDWR);
  openlog("deamon server start for udp",LOG_PID,0);
  syslog(LOG_INFO|LOG_LOCAL2,"Yep,That's me");
  return (0);
}
void dg_echo(int sockfd,struct sockaddr *cliaddr,socklen_t clilen){
   int n;
   socklen_t len;
   char str[16];
   char msg[1024];
   for(;;){
      len=clilen;
      n=recvfrom(sockfd,msg,1024,0,cliaddr,&clilen);     
      printf("recv from %s \n",inet_ntop(AF_INET,&cliaddr,str,sizeof(str))); 
      sendto(sockfd,msg,n,0,cliaddr,clilen);
      msg[n]=0;
      fputs(msg,stdout);
      syslog(LOG_INFO|LOG_LOCAL2,msg);
   }
}
int main(int argc,char **argv){
   int sockfd;
   struct sockaddr_in servaddr,cliaddr;
   init_deamon();
   sockfd=socket(AF_INET,SOCK_DGRAM,0);
   bzero(&servaddr,sizeof(servaddr));
   servaddr.sin_family=AF_INET;
   servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
   servaddr.sin_port=htons(22111);
   bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
   dg_echo(sockfd,(struct sockaddr *)&cliaddr,sizeof(cliaddr));
}



client 代码

#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<strings.h>
void dg_cli(FILE *fp,int sockfd,struct sockaddr *servaddr,socklen_t servlen){
   int n;
   socklen_t len;
   struct sockaddr *replyaddr;
   replyaddr=malloc(servlen);
   char readline[1024],sendline[1025];
   char str[16];
   connect(sockfd,servaddr,servlen);
   while(fgets(sendline,1024,fp)!=NULL){
      write(sockfd,sendline,strlen(sendline));
      n=read(sockfd,readline,1024);
    //  if(len!=servlen||memcmp(servaddr,replyaddr,len)!=0){
    //      printf("reply from %s (ignored)\n",inet_ntop(AF_INET,&replyaddr,str,sizeof(str)));
    //      continue;
    //  }
      readline[n]=0;
      fputs(readline,stdout);
   }
}
int main(int argc,char **argv){
   int sockfd;
   struct sockaddr_in servaddr;
   if(argc!=2){
      printf("usage:udpcli <IPAddress>\n");
      exit(1);
   }
   bzero(&servaddr,sizeof(servaddr));
   servaddr.sin_family=AF_INET;
   servaddr.sin_port=htons(22111);
   inet_pton(AF_INET,argv[1],&servaddr.sin_addr);

   sockfd=socket(AF_INET,SOCK_DGRAM,0);
   dg_cli(stdin,sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));

   exit(0);
}
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics