欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

网络编程Server端 --- Linux版

程序员文章站 2024-01-21 12:25:16
之前我们已经编写了一个,我们现在增加对此类操作特性的一些code!        大家都知道fork() 只有在uni...

之前我们已经编写了一个,我们现在增加对此类操作特性的一些code!

       大家都知道fork() 只有在unix/linux类操作系统才有!因为他们没有线程这一说。他们只有子进程。

       要用到fork那么就必须用到 waitpid() !

       waitpid函数原型:

 

       #include<sys/types.h>   /* 提供类型pid_t的定义 */

       #include<sys/wait.h>

 

       pid_twaitpid(pid_tpid, int *status, int options);

 

       参数pid=-1时,等待任何一个子进程退出,没有任何限制,此时waitpid和wait的作用一模一样。

       参数 status我们设置为null。

       参数options提供了一些额外的选项来控制waitpid,wnohang常量表示,即使没有子进程退出,它也会立即返回,不会像wait那样永远等下去。

       我们希望只有给waitpid()信号时,才使用它!

       这时,我们需要另外一个函数sigaction()。

       sigaction函数原型:

       #include<signal.h>

 

   int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);

 

       参数 signum=sigchld时,只有子进程停止或者退出才进行调用。

       参数 act是一个指向sigaction结构的指针。

       参数 oldact一般废止不用。

 

       我们主要设置结构sigaction的sa_handler、sa_mask和sa_flags三个成员变量即可!

      

       下面是示例:

 /**
 * version: 0.2
 *
 * description: add signal
 *
 */ 
 
#include <stdio.h>  
#include <string.h>  
#include <sys/types.h>  
#include <sys/socket.h>  
#include <netdb.h>  
#include <arpa/inet.h>  
#include <signal.h>  
#include <sys/wait.h>  
#include <stdlib.h>       /* exit declare */  
#include <unistd.h>       /* fork declare */  
 
#define servport "2349"  
 
void sigchild_handler() 

    while (waitpid(-1, null, wnohang) > 0);   

 
int main(int argc, char *argv[]) 

    struct addrinfo hints, *res; 
    int status; 
    int sockfd; 
     
    int connfd; 
    /* struct sockaddr_in cliaddr;  only ipv4  */ 
    struct sockaddr_storage clientaddr; /* both ipv4 and ipv6 */ 
     
    struct sigaction sa; 
     
    int sendsta; 
    char *msg; 
     
    if (argc != 2) { 
        fprintf(stderr, "usage: not found read file"); 
        return 1; 
    } 
     
    memset(&hints, 0, sizeof(hints)); 
    hints.ai_family = af_unspec; 
    hints.ai_socktype = sock_stream; 
    hints.ai_flags = ai_passive; 
     
    status = getaddrinfo(null, servport, &hints, &res); 
     
    if (status != 0) { 
        fprintf(stderr, "getaddrinfo, fail!"); 
        return 2; 
    } 
     
    sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); 
     
    bind(sockfd, res->ai_addr, res->ai_addrlen); 
     
    listen(sockfd, 5); 
     
    printf("======== please wait client =========\n"); 
     
    /* struct sigaction notes from posix:
     *
     *  (1) routines stored in sa_handler should take a single int as
     *      their argument although the posix standard does not require this.
     *  (2) the fields sa_handler and sa_sigaction may overlap, and a conforming
     *      application should not use both simultaneously.
     */ 
    sa.sa_handler = sigchild_handler;    
    sigemptyset(&sa.sa_mask);   /* additional set of signals to be blocked */ 
                                /* during execution of signal-catching function. */ 
    sa.sa_flags = sa_restart;   /* special flags to affect behavior of signal */ 
                                /* sa_restart 0x10000000    // restart syscall on signal return */ 
                                 
    if (sigaction(sigchld, &sa, null) == -1) {  /* sigchld 20   // to parent on child stop or exit */ 
        fprintf(stderr, "sigaction fail!"); 
        exit(3);     
    } 
     
    while(1) { // loop forever!  
        char ipstr[inet_addrstrlen]; 
        void *addr; 
         
        int len = sizeof(clientaddr); 
        connfd = accept(sockfd, (struct sockaddr *)&clientaddr, &len); 
         
        /* view client ip */ 
        struct sockaddr_in *ipv4 = (struct sockaddr_in *)&clientaddr; 
        addr = &(ipv4->sin_addr); 
     
        inet_ntop(af_inet, addr, ipstr, sizeof(ipstr)); 
        printf("client: %s\n", ipstr); 
         
        if (!fork()) { 
            close(sockfd);  /* child doesn't need the listener */ 
            /* copy data */ 
            msg = "the server simply displays a message!"; 
            sendsta = send(connfd, msg, strlen(msg), 0); 
            if (sendsta == -1) 
                fprintf(stderr, "send fail!"); 
            close(connfd); 
            exit(0); 
        } 
         
        close(connfd); 
    } 
     
    close(sockfd); 
    return 0; 

/**
 * version: 0.2
 *
 * description: add signal
 *
 */

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <signal.h>
#include <sys/wait.h>
#include <stdlib.h>  /* exit declare */
#include <unistd.h>  /* fork declare */

#define servport "2349"

void sigchild_handler()
{
 while (waitpid(-1, null, wnohang) > 0); 
}

int main(int argc, char *argv[])
{
 struct addrinfo hints, *res;
 int status;
 int sockfd;
 
 int connfd;
 /* struct sockaddr_in cliaddr;  only ipv4  */
 struct sockaddr_storage clientaddr; /* both ipv4 and ipv6 */
 
 struct sigaction sa;
 
 int sendsta;
 char *msg;
 
 if (argc != 2) {
  fprintf(stderr, "usage: not found read file");
  return 1;
 }
 
 memset(&hints, 0, sizeof(hints));
 hints.ai_family = af_unspec;
 hints.ai_socktype = sock_stream;
 hints.ai_flags = ai_passive;
 
 status = getaddrinfo(null, servport, &hints, &res);
 
 if (status != 0) {
  fprintf(stderr, "getaddrinfo, fail!");
  return 2;
 }
 
 sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
 
 bind(sockfd, res->ai_addr, res->ai_addrlen);
 
 listen(sockfd, 5);
 
 printf("======== please wait client =========\n");
 
 /* struct sigaction notes from posix:
   *
   *  (1) routines stored in sa_handler should take a single int as
   *      their argument although the posix standard does not require this.
   *  (2) the fields sa_handler and sa_sigaction may overlap, and a conforming
   *      application should not use both simultaneously.
   */
 sa.sa_handler = sigchild_handler; 
 sigemptyset(&sa.sa_mask); /* additional set of signals to be blocked */
                               /* during execution of signal-catching function. */
 sa.sa_flags = sa_restart; /* special flags to affect behavior of signal */
        /* sa_restart 0x10000000  // restart syscall on signal return */
        
 if (sigaction(sigchld, &sa, null) == -1) { /* sigchld 20 // to parent on child stop or exit */
  fprintf(stderr, "sigaction fail!");
  exit(3); 
 }
 
 while(1) { // loop forever!
  char ipstr[inet_addrstrlen];
  void *addr;
  
  int len = sizeof(clientaddr);
  connfd = accept(sockfd, (struct sockaddr *)&clientaddr, &len);
  
  /* view client ip */
  struct sockaddr_in *ipv4 = (struct sockaddr_in *)&clientaddr;
  addr = &(ipv4->sin_addr);
 
  inet_ntop(af_inet, addr, ipstr, sizeof(ipstr));
  printf("client: %s\n", ipstr);
  
  if (!fork()) {
   close(sockfd); /* child doesn't need the listener */
   /* copy data */
   msg = "the server simply displays a message!";
   sendsta = send(connfd, msg, strlen(msg), 0);
   if (sendsta == -1)
    fprintf(stderr, "send fail!");
   close(connfd);
   exit(0);
  }
  
  close(connfd);
 }
 
 close(sockfd);
 return 0;
}

 

end.

 摘自 xiaobin_hlj80的专栏