<tfoot id='HBHyI'></tfoot>

  • <i id='HBHyI'><tr id='HBHyI'><dt id='HBHyI'><q id='HBHyI'><span id='HBHyI'><b id='HBHyI'><form id='HBHyI'><ins id='HBHyI'></ins><ul id='HBHyI'></ul><sub id='HBHyI'></sub></form><legend id='HBHyI'></legend><bdo id='HBHyI'><pre id='HBHyI'><center id='HBHyI'></center></pre></bdo></b><th id='HBHyI'></th></span></q></dt></tr></i><div id='HBHyI'><tfoot id='HBHyI'></tfoot><dl id='HBHyI'><fieldset id='HBHyI'></fieldset></dl></div>

      1. <legend id='HBHyI'><style id='HBHyI'><dir id='HBHyI'><q id='HBHyI'></q></dir></style></legend>

        <small id='HBHyI'></small><noframes id='HBHyI'>

        • <bdo id='HBHyI'></bdo><ul id='HBHyI'></ul>

      2. 替代在 Windows 中分叉

        时间:2023-08-01
          • <bdo id='VsItY'></bdo><ul id='VsItY'></ul>
            <i id='VsItY'><tr id='VsItY'><dt id='VsItY'><q id='VsItY'><span id='VsItY'><b id='VsItY'><form id='VsItY'><ins id='VsItY'></ins><ul id='VsItY'></ul><sub id='VsItY'></sub></form><legend id='VsItY'></legend><bdo id='VsItY'><pre id='VsItY'><center id='VsItY'></center></pre></bdo></b><th id='VsItY'></th></span></q></dt></tr></i><div id='VsItY'><tfoot id='VsItY'></tfoot><dl id='VsItY'><fieldset id='VsItY'></fieldset></dl></div>
          • <small id='VsItY'></small><noframes id='VsItY'>

            <legend id='VsItY'><style id='VsItY'><dir id='VsItY'><q id='VsItY'></q></dir></style></legend>

              <tfoot id='VsItY'></tfoot>
                <tbody id='VsItY'></tbody>

                  本文介绍了替代在 Windows 中分叉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我一直在关注 Beej Networking guide 并在服务器部分有一部分代码在其中调用了函数 fork().

                  I've been following Beej Networking guide and in the server section there is portion of code where it has called a function fork().

                  if (!fork()) { // this is the child process
                              close(sockfd); // child doesn't need the listener
                              if (send(new_fd, "Hello, world!", 13, 0) == -1)
                                  perror("send");
                              close(new_fd);
                              exit(0);
                  

                  我在 Windows 机器上,无法让那部分工作.我能做些什么来解决这个问题?我的代码如下.

                  I'm on a windows machine and cant get that part working. What can I do to solve this?. My code is as follows.

                  /* Server */
                  #define _WIN32_WINNT 0x501
                  #include <iostream>
                  #include <windows.h>
                  #include <winsock2.h>
                  #include <ws2tcpip.h>
                  #include <stdio.h>
                  #include  <sys/types.h>
                  
                  
                  using namespace std;
                  
                  const int winsockVersion = 2;
                  #define BACKLOG 10
                  #define PORT "3000"
                  
                  
                  int main(void){
                  
                      WSADATA wsadata;
                      if (WSAStartup(MAKEWORD(winsockVersion,0),&wsadata) == 0){
                          cout<<"-WSAStartup initialized..." << endl;
                  
                          int status;
                          int sockfd, new_fd;
                          const char yes = '1';
                          struct addrinfo hints, *res,*loop_find;
                          struct sockaddr_storage their_addr;
                          socklen_t addr_size;
                  
                  
                  
                          memset(&hints,0,sizeof hints);
                          hints.ai_family = AF_INET;
                          hints.ai_socktype = SOCK_STREAM;
                          hints.ai_flags = AI_PASSIVE;
                  
                          if ( (status = getaddrinfo(NULL,PORT,&hints,&res)) == 0 ){
                              cout<<"-Call to get addrinfo successful!." << endl;
                          }
                  
                          for (loop_find = res; loop_find!=NULL; loop_find = loop_find->ai_next){
                              if ( (sockfd = socket(loop_find->ai_family,loop_find->ai_socktype,loop_find->ai_protocol) ) == -1 ){
                                  cout<<"-Could not create socket." << endl;
                                  continue;
                              }else{
                                  cout<<"-Socket Created." << endl;
                              }
                  
                              //clearing in use ports.
                              if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
                                  cout<<"-Couldnt clear blocked port." << endl;
                                  perror("setsockopt");
                                  exit(1);
                              }
                  
                              if( bind(sockfd,loop_find->ai_addr,loop_find->ai_addrlen) == -1 ){
                                  closesocket(sockfd);
                                  perror("server: bind");
                                  continue;
                              }
                  
                              break;
                          }
                  
                          if (listen(sockfd,BACKLOG) != -1){
                              cout<<"-Listening for incoming connections.";
                          }
                  
                          //accept loop.
                          while(true){
                  
                              socklen_t addr_size = sizeof their_addr;
                              new_fd = accept(sockfd,(sockaddr*)&their_addr,&addr_size);
                  
                              if ( new_fd == -1 ){
                                  perror("accept");
                                  continue;
                              }
                  
                              struct sockaddr new_addr;
                              int len = sizeof new_addr;
                              getpeername(new_fd,&new_addr,&len);
                              cout<<"-Connected to " << new_addr.sa_data << endl;
                  
                              if(!fork()){ //this is a child process
                                  closesocket(sockfd);
                                  if (send(new_fd,"hello world!!",13,0) == -1){
                                      perror("send");
                                      closesocket(new_fd);
                                      exit(0);
                                  }
                              }
                              closesocket(new_fd);
                  
                          }
                      }
                  
                  
                      //clear stuff
                      if( WSACleanup() != 0){
                          cout<<"-WSACleanup unsuccessful" << endl;
                      }else{
                          cout<<"-WSACleanup successful" << endl;
                      }
                  
                  
                      return 0;
                  
                  }
                  

                  推荐答案

                  fork() 在 Windows 上显然不存在.相反,您需要创建一个新线程,或全新流程.

                  fork() obviously doesn't exist on Windows. Instead you'll need to create a new thread, or a whole new process.

                  这篇关于替代在 Windows 中分叉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:目录大小 下一篇:使用 ShellExecuteEx 并捕获标准输入/输出/错误

                  相关文章

                  最新文章

                  • <bdo id='11aXP'></bdo><ul id='11aXP'></ul>
                • <tfoot id='11aXP'></tfoot>

                  <small id='11aXP'></small><noframes id='11aXP'>

                  1. <legend id='11aXP'><style id='11aXP'><dir id='11aXP'><q id='11aXP'></q></dir></style></legend>
                    1. <i id='11aXP'><tr id='11aXP'><dt id='11aXP'><q id='11aXP'><span id='11aXP'><b id='11aXP'><form id='11aXP'><ins id='11aXP'></ins><ul id='11aXP'></ul><sub id='11aXP'></sub></form><legend id='11aXP'></legend><bdo id='11aXP'><pre id='11aXP'><center id='11aXP'></center></pre></bdo></b><th id='11aXP'></th></span></q></dt></tr></i><div id='11aXP'><tfoot id='11aXP'></tfoot><dl id='11aXP'><fieldset id='11aXP'></fieldset></dl></div>