0

I have a C++ program where I connect to my server with a socket and I need to set the overlapped for the socket. Doing the following does not work:

Function

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;
}

Extension

typedef struct OverlappedEx : OVERLAPPED {
int id;
} OverlappedEx;

proxy_client struct

struct proxy_client {
    // ... blah blah blah
    SOCKET s;
    OverlappedEx ov;
    // ... blah blah blah
}

Main

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 {
    //
}

This does not seem to trigger the socket when I GetQueuedCompletionStatus for ServerCompletionPort, after sending the socket data (from the server). I was wondering how I can set an IO for a socket! Thank for the help! :-)

User
  • 659
  • 2
  • 12
  • 29
  • Take a look at this question: http://stackoverflow.com/questions/754068/win32-overlapped-i-o-completion-routines-or-waitformultipleobjects – Nikolai Fetissov Sep 19 '12 at 23:23
  • My curiosity as to the definition of server_client is just eating at me. Further, any particular reason you're setting an IOCP handle it what appears to be an OVERLAPPED struct's hEvent member? I'd start with that. – WhozCraig Sep 20 '12 at 02:24
  • @CraigNelson I am really sorry about that. It was a typo. :( Darn. – User Sep 20 '12 at 02:31

0 Answers0