0

我有一个 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 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 {
    //
}

在发送套接字数据(从服务器)之后,当我 GetQueuedCompletionStatus for ServerCompletionPort 时,这似乎不会触发套接字。我想知道如何为套接字设置 IO!感谢您的帮助!:-)

4

0 回答 0