Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. GetQueuedCompletionStatus blocks, though GetLastError which is called after AcceptEx returns ERROR_IO_PENDING [modified]

GetQueuedCompletionStatus blocks, though GetLastError which is called after AcceptEx returns ERROR_IO_PENDING [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionsysadmin
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    followait
    wrote on last edited by
    #1

    But the client can connect. some of the test codes server

    void post_io_request_of_accept(SOCKET so_listen, PerIoData * per_io_data)
    {
    DWORD bytes_received;
    BOOL ok = AcceptEx(so_listen,
    per_io_data->so_local,
    per_io_data->buf,
    0,
    sizeof(SOCKADDR_STORAGE)+16,
    sizeof(SOCKADDR_STORAGE)+16,
    &bytes_received,
    per_io_data);
    DWORD err = GetLastError();
    if (ok)
    {
    assert(false);
    }
    else if (err==ERROR_IO_PENDING)
    {
    }
    else
    assert(false);
    }

    DWORD WINAPI ServingThreadProc(LPVOID pParam)
    {
    DWORD bytes_tranfered;
    ULONG_PTR completion_key;
    PerIoData * per_io_data;

    while (true)
    {
    	BOOL ok = GetQueuedCompletionStatus(HANDLE(pParam),
    		&bytes\_tranfered,
    		&completion\_key,
    		(OVERLAPPED\*\*)&per\_io\_data,
    		INFINITE);
    	assert(ok);		
    	handle\_io\_completion(per\_io\_data);
    }
    return 0;
    

    }

    client

    int main()
    {
    WSADATA wsa;
    int res = WSAStartup(MAKEWORD(2,2), &wsa);
    assert(res==0);

    SOCKADDR\_IN addr\_in;
    addr\_in.sin\_family = AF\_INET;
    addr\_in.sin\_addr.s\_addr = inet\_addr("127.0.0.1");
    addr\_in.sin\_port = htons(5150);
    ZeroMemory(&addr\_in.sin\_zero, sizeof(addr\_in.sin\_zero));
    
    SOCKET s = WSASocket(AF\_INET, SOCK\_STREAM, IPPROTO\_TCP, NULL, 0, 0);
    assert(s!=INVALID\_SOCKET);
    int ok = connect(s, (sockaddr\*)&addr\_in, sizeof(addr\_in));
    assert(ok==0);
    
    for (int i=0; i<10; ++i)
    {
    	char buf\[256\];
    	sprintf\_s(buf, sizeof(buf), "message %d", i);
    	WSABUF wsa\_buf;
    	wsa\_buf.buf = buf;
    	wsa\_buf.len = strlen(buf);
    	DWORD bytes\_sent;
    	int res = WSASend(s, &wsa\_buf, 1, &bytes\_sent, 0, NULL, NULL);
    	assert(res==0 && bytes\_sent==wsa\_buf.len);
    	printf\_s("sent: %s\\n", buf);
    	Sleep(1000);
    }
    
    WSACleanup();
    
    return 0;
    

    }

    If use PostQueuedCompletionStatus, GetQueuedCompletionStatus will be unblocked. What is the problem? Thanks.

    modified on Friday, August 6, 2010 11:38 AM

    M 1 Reply Last reply
    0
    • F followait

      But the client can connect. some of the test codes server

      void post_io_request_of_accept(SOCKET so_listen, PerIoData * per_io_data)
      {
      DWORD bytes_received;
      BOOL ok = AcceptEx(so_listen,
      per_io_data->so_local,
      per_io_data->buf,
      0,
      sizeof(SOCKADDR_STORAGE)+16,
      sizeof(SOCKADDR_STORAGE)+16,
      &bytes_received,
      per_io_data);
      DWORD err = GetLastError();
      if (ok)
      {
      assert(false);
      }
      else if (err==ERROR_IO_PENDING)
      {
      }
      else
      assert(false);
      }

      DWORD WINAPI ServingThreadProc(LPVOID pParam)
      {
      DWORD bytes_tranfered;
      ULONG_PTR completion_key;
      PerIoData * per_io_data;

      while (true)
      {
      	BOOL ok = GetQueuedCompletionStatus(HANDLE(pParam),
      		&bytes\_tranfered,
      		&completion\_key,
      		(OVERLAPPED\*\*)&per\_io\_data,
      		INFINITE);
      	assert(ok);		
      	handle\_io\_completion(per\_io\_data);
      }
      return 0;
      

      }

      client

      int main()
      {
      WSADATA wsa;
      int res = WSAStartup(MAKEWORD(2,2), &wsa);
      assert(res==0);

      SOCKADDR\_IN addr\_in;
      addr\_in.sin\_family = AF\_INET;
      addr\_in.sin\_addr.s\_addr = inet\_addr("127.0.0.1");
      addr\_in.sin\_port = htons(5150);
      ZeroMemory(&addr\_in.sin\_zero, sizeof(addr\_in.sin\_zero));
      
      SOCKET s = WSASocket(AF\_INET, SOCK\_STREAM, IPPROTO\_TCP, NULL, 0, 0);
      assert(s!=INVALID\_SOCKET);
      int ok = connect(s, (sockaddr\*)&addr\_in, sizeof(addr\_in));
      assert(ok==0);
      
      for (int i=0; i<10; ++i)
      {
      	char buf\[256\];
      	sprintf\_s(buf, sizeof(buf), "message %d", i);
      	WSABUF wsa\_buf;
      	wsa\_buf.buf = buf;
      	wsa\_buf.len = strlen(buf);
      	DWORD bytes\_sent;
      	int res = WSASend(s, &wsa\_buf, 1, &bytes\_sent, 0, NULL, NULL);
      	assert(res==0 && bytes\_sent==wsa\_buf.len);
      	printf\_s("sent: %s\\n", buf);
      	Sleep(1000);
      }
      
      WSACleanup();
      
      return 0;
      

      }

      If use PostQueuedCompletionStatus, GetQueuedCompletionStatus will be unblocked. What is the problem? Thanks.

      modified on Friday, August 6, 2010 11:38 AM

      M Offline
      M Offline
      Moak
      wrote on last edited by
      #2

      Perhaps you can use a IOCP networking class from CodeProject (or somewhere else) as a basis for your code? See if it helps: Internet & Network - Client/Server Development. :)

      Chat in Europe :java: Now with 24% more Twitter

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups