我想做一个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);
它会在接受时暂停