Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 代替windows中的分叉_C++_Winapi_Gcc_Winsock_Fork - Fatal编程技术网

C++ 代替windows中的分叉

C++ 代替windows中的分叉,c++,winapi,gcc,winsock,fork,C++,Winapi,Gcc,Winsock,Fork,我一直在关注,在服务器部分,有一部分代码调用了函数fork() 我在一台windows机器上,不能让那个部件工作。我能做些什么来解决这个问题?。 我的代码如下 /* Server */ #define _WIN32_WINNT 0x501 #include <iostream> #include <windows.h> #include <winsock2.h> #include <ws2tcpip.h> #include <stdio.h&

我一直在关注,在服务器部分,有一部分代码调用了函数fork()

我在一台windows机器上,不能让那个部件工作。我能做些什么来解决这个问题?。 我的代码如下

/* 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;

}
/*服务器*/
#定义_WIN32_WINNT 0x501
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
const int winsockVersion=2;
#定义待办事项10
#定义端口“3000”
内部主(空){
WSADATA WSADATA;
if(WSAStartup(MAKEWORD(winsockVersion,0),&wsadata)==0){

cout
fork
在Windows上不存在。必须使用名为的特定于窗口的API


fork
相反,
CreateProcess
需要EXE的路径。您可以通过使用空参数调用来检索当前EXE的路径。

fork()显然不存在于Windows上。相反,您需要,或者。

与现有答案(来自OJ和Vincent Robert)fork()相反确实存在于高端版本的Windows上。它是基于UNIX的应用程序子系统(SUA)的一部分,早期称为Microsoft Windows Services for UNIX(SFU),早期称为Interix

引用,SUA可在

  • Windows Server 2003 R2(所有版本)-版本5.2
  • Windows Vista(终极版和 企业版)-版本6.0
  • Windows Server 2008(所有版本)- 版本6.0
  • Windows Server 2008 R2和Microsoft Windows 7-版本6.1
使用fork()只需安装免费的SUA SDK。根据您的目标系统,您需要以下选项之一:


你也可以看看

当你为Windows编写代码时,你通常会使用另一种思维方式。我认为你最好看看Winsock教程,而不是你正在使用的本指南。相关问题:,,但实际上你根本没有在Windows上设计那样的网络服务器。fork()Windows上没有这么明显的存在:)请看我的答案。听起来一点也不像粉丝。如果你问我的话,你会给出毫无意义的评论。fork()确实存在于Windows上:)请看我的答案。所以你说的是“fork确实存在,只要你使用正确版本的Windows并安装启用它的SDK来确保它存在”.IMHO,它也可能不存在。
CreateProcess
对于Windows开发人员来说是一个更简单、更好的选择(当然是IMHO)。干杯!没有Windows是一个更简单、更好的选择。遗憾的是,似乎SUA在Windows 8中不受欢迎,并且“将从下一版本中完全删除”:@RichieHindle但现在我们即将在WSL中找到一个替代品:@SørenBoisen:是的!我还没有看过,但它看起来确实很有希望。我们目前正在使用Cygwin,这并不是最稳定的。一个“真正的”Windows Linux命令行子系统应该是一个更好的选择。
/* 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;

}