程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> 關於C >> 網絡編程Server端

網絡編程Server端

編輯:關於C

之前我們已經編寫了一個/kf/201201/116006.html,我們現在增加對此類操作系統特性的一些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的專欄

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved