0

Winsock 非ブロッキング オブジェクトを作成したい これは私のコードです

主な機能:

#include <iostream>
#include <cstdlib>
#include "server.h"
#include <windows.h>
using namespace std;

int main()
{
    Object_server a;
    Sleep(100000);

    return 0;
}

サーバー.h:

#ifndef SERVER_H_INCLUDED
#define SERVER_H_INCLUDED

#include <winsock2.h>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <process.h>
#include <windows.h>
#include <stdio.h>
using namespace std;

class Object_server
{
private:

    bool bool_server_flag;
    WSADATA wsaData;
    int socket_counter;
    struct Object_socket
    {
        SOCKET socket_Local;
        SOCKET *socket_Remote;
    };
    struct Object_socketaddr
    {
        struct sockaddr_in sockaddr_Local;
        struct sockaddr_in *sockaddr_Remote;
    };

    struct Object_Data_transmission
    {
        int int_counter;
        int total;
        vector <int> int_target;//-1 server -2NULL
        vector <string> str_Data;
    };

    Object_socket O_socket;
    Object_socketaddr O_sockaddr;
    Object_Data_transmission *O_Data_transmission;
    bool server_on(int int_port=11111)
    {
        int int_flag;

        if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
        {
            return false;
        }

        O_socket.socket_Local=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

        if(O_socket.socket_Local==INVALID_SOCKET)
        {
            return false;
        }

        O_sockaddr.sockaddr_Local.sin_family=AF_INET;
        //O_sockaddr.sockaddr_Local.sin_addr.s_addr=inet_addr(PC_DATA.get_Ip());
        O_sockaddr.sockaddr_Local.sin_addr.s_addr=inet_addr("127.0.0.1");
        O_sockaddr.sockaddr_Local.sin_port=htons(int_port);
        //O_sockaddr.sockaddr_Local.sin_zero=NULL;



        int_flag=bind(O_socket.socket_Local,(struct sockaddr     *)&O_sockaddr.sockaddr_Local,sizeof(O_sockaddr.sockaddr_Local));
        if(int_flag!=0)
        {
            return false;
        }

        int_flag=listen(O_socket.socket_Local,128);
        if(int_flag!=0)
        {
            return false;
        }

        O_socket.socket_Remote=new SOCKET[128];
        O_sockaddr.sockaddr_Remote=new sockaddr_in[128];
        O_Data_transmission=new Object_Data_transmission[128];

        socket_counter=0;

        bool_server_flag=true;

        //_beginthread(Object_server::accept_thread,0,(void *)this);
        unsigned threadID;

        //int_flag=_beginthreadex(NULL,0,Object_server::accept_thread,(void     *)this,CREATE_SUSPENDED,&threadID);
        int_flag=_beginthreadex(NULL,0,Object_server::accept_thread,(void     *)this,0,&threadID);
        //cout<<int_flag;
        //accept_thread((void *)this);
        return true;

    }

    static unsigned __stdcall accept_thread(void *ptr)
    {
        static_cast<Object_server *>(ptr)->accept_f();
        return 0;
    }

    void accept_f()
    {

        int sockaddr_size;
        SOCKET S_tmp;

        while(bool_server_flag)
        {

            sockaddr_size=sizeof(O_sockaddr.sockaddr_Remote[socket_counter]);
            O_socket.socket_Remote[socket_counter]=accept(O_socket.socket_Local,(struct     sockaddr *)&O_sockaddr.sockaddr_Remote[socket_counter],&sockaddr_size);




            if(O_socket.socket_Remote[socket_counter]!=INVALID_SOCKET)
            {
                ++socket_counter;
                //cout<<"connect : "<<socket_counter<<'\n';

            }
            else
            {
                //cout<<"connect failed"<<socket_counter<<'\n';
            }


        }

    }

public:

    Object_server()
    {
        bool_server_flag=false;

        while(server_on())
        {

        }
    }

    Object_server(int int_port)
    {
        bool_server_flag=false;

        while(server_on(int_port))
        {

        }
    }
    ~Object_server()
    {
        delete []O_socket.socket_Remote;
        delete []O_sockaddr.sockaddr_Remote;
        delete []O_Data_transmission;
    }

};

#endif // SERVER_H_INCLUDED

通常、クライアント接続がない場合、関数の受け入れは一時停止しますが、_beginthread または _beginthreadex 関数を使用すると一時停止しません。

int_flag=_beginthreadex(NULL,0,Object_server::accept_thread,(void     *)this,0,&threadID);

accept_thread((void *)this);

受け入れると一時停止します

4

0 に答える 0