windows 套接字子进程继承

网络编程模式中,有一种是多进程模式,主进程侦听,收到连接后,启动子进程处理请求,每个连接一个子进程。

在linux中通过fork很容易实现,因为fork时子进程继承父进程的文件描述符,套接字也是一种文件描述符。

在windows句柄也能够继承,但是必须通过外部方法告诉子进程句柄值,可以通过命令行参数或者环境变量的方式传递。

怎么获取套接字的句柄呢?其实套接字本身就是一个句柄,只是平常使用recv、send来操作套接字,没觉得它是一个句柄。

 需要注意的是,在win 9x中,套接字句柄默认是不继承的,需要使用复制句柄(DuplicateHandle)函数复制一个可继承的句柄。

主进程代码片段:

   // This is a Winsock server that is listening on a port.

   // When a client connects, the server spawns a child process and
   // passes the socket handle to the child.
   // The child can use this socket handle to interact with the
   // client and the parent is free to go back to waiting for
   // other clients to connect.

   OrigSock=accept(listen_socket,(struct sockaddr *)&from,&fromlen);

   if (OrigSock == INVALID_SOCKET)  {
      fprintf(stderr,"accept failed %d\n",GetLastError());
      return -1;
   }
   {
      STARTUPINFO si;
      PROCESS_INFORMATION pi;
      char argbuf[256];

      memset(&si,0,sizeof(si));

      // 
      // Duplicate the socket OrigSock to create an inheritable copy.
      // 
      if (!DuplicateHandle(GetCurrentProcess(),
            (HANDLE)OrigSock,
            GetCurrentProcess(),
            (HANDLE*)&DuplicateSock,
            0,
            TRUE, // Inheritable
            DUPLICATE_SAME_ACCESS)) {

         fprintf(stderr,"dup error %d\n",GetLastError());
         return -1;
      }
      // 
      // Spawn the child process.
      // The first command line argument (argv[1]) is the socket handle.
      // 

      wsprintf(argbuf,"child.exe %d",DuplicateSock);
      if (!CreateProcess(NULL,argbuf,NULL,NULL,
               TRUE, // inherit handles
               0,NULL,NULL,&si,&pi) ){
         fprintf(stderr,"createprocess failed %d\n",GetLastError());
         return -1;
      }

      // 
      // On Windows 95, the parent needs to wait until the child
      // is done with the duplicated handle before closing it.
      // 
      WaitForSingleObject(pi.hProcess, INFINITE);
   }
   // 

   // The duplicated socket handle must be closed by the owner
   // process--the parent. Otherwise, socket handle leakage
   // occurs. On the other hand, closing the handle prematurely
   // would make the duplicated handle invalid in the child. In this
   // sample, we use WaitForSingleObject(pi.hProcess, INFINITE) to
   // wait for the child.
   // 
   closesocket(OrigSock);
   closesocket(DuplicateSock);

子进程代码片段: 

   main(int argc, char *argv[]){
      SOCKET Sock;

      /* WSAStartup etc. */ 
      if (2 == argc){
         Sock = atoi(argv[1]);   // use Sock
      }
   }


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fighting Horse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值