我有一个C ++程序,我用套接字连接到我的服务器,我需要为套接字设置重叠。执行以下操作无效:
功能
int set_wsa_proxy_client ( proxy_client *node ) {
WSABUF wbuf;
DWORD bytes, flags;
int BufLen = 1024;
wbuf.buf = node->buf;
wbuf.len = node->len;
flags = 0;
int rr = WSARecv ( node->s , &wbuf , 1 , &bytes , &flags , &node->ov , NULL );
if (rr == FALSE) {
if (WSAGetLastError() != WSA_IO_PENDING) {
printf("PostRecv: WSARecv* failed: %d\n", WSAGetLastError());
if ( WSAGetLastError() == ERROR_SUCCESS ) { // this means it completed right away ...
//cout << endl << "ERROR_SUCCESS - set_wsa_lobby_client completed" << endl;
//cout << endl << "BYTES: " << node->len << endl;
return 0;
}
return WSAGetLastError();
}
}
return 0;
}
扩展
typedef struct OverlappedEx : OVERLAPPED {
int id;
} OverlappedEx;
proxy_client struct
struct proxy_client {
// ... blah blah blah
SOCKET s;
OverlappedEx ov;
// ... blah blah blah
}
主要
HANDLE ServerCompletionPort = CreateIoCompletionPort ( INVALID_HANDLE_VALUE , NULL , (ULONG_PTR)NULL , 0 );
if ( ServerCompletionPort == NULL ) { fprintf( stderr , "CreateIoCompletionPort failed: %d\n" , GetLastError() ); return -1; }
proxy_client *new_c = new proxy_client;
memset(&new_c->ov , 0 , sizeof(new_c->ov));
new_c->ov.hEvent = ServerCompletionPort;
new_c->s = (make socket)
// ... Connect and other stuff ...
HANDLE hrc = CreateIoCompletionPort( (HANDLE)new_c->s, new_c->ov.hEvent, (ULONG_PTR)pc, 0 ); // pc is the global struct of proxy_client
if (hrc == NULL)
fprintf(stderr, "CompletionThread: CreateIoCompletionPort failed: %d\n", GetLastError());
int r = 0;
if ( ( r = set_wsa_proxy_client ( new_c ) ) != 0 ) {
//
} else {
//
}
在发送套接字数据(来自服务器)之后,当我为ServerCompletionPort获取GetQueuedCompletionStatus时,这似乎没有触发套接字。我想知道如何为套接字设置IO!谢谢你的帮助! : - )