c++ - Unable to accept a new connection properly using IOCP - Invalid socket handle -


i learning iocp , have decided write own wrapper class based on following article:
http://www.codeproject.com/articles/13382/a-simple-application-using-i-o-completion-ports-an

my project c++ tcp server using iocp. client uses send() , recv() send , receive data cannot change (from i've been told shouldn't cause problem, mentioning in case). creates socket using socket() (and not wsasocket()). seems working fine (no error createiocompletionport, can add socket descriptor existing completion port without error. i've checked adding call wsagetlasterror() after each of these functions).

(before anything, please don't mind inconsistent coding style. make stuff work first , clean up.)

socket_ = wsasocket(af_inet, sock_stream, ipproto_ip, null, 0, wsa_flag_overlapped); setsockopt(socket_, ipproto_ip, so_debug | tcp_nodelay, sockopt, 4); ioctlsocket(socket_, fionbio, &ulsockmode_); sin_.sin_family = af_inet; sin_.sin_port = htons((uint16_t)uiport_)); haccept_[0] = wsacreateevent();  //only 1 event, i'm using array convenience  if (haccept_ == wsa_invalid_event) {     //this never executed } wsaeventselect(socket_, haccept_[0], fd_accept); 

after incoming connection detected (i use wsawaitformultipleeevents , wsaenumnetworkevents work don't trigger error), use following code accept client (and problems start):

socket sock_client{ invalid_socket }; int32_t len_si{ sizeof(sockaddr_in) }; //sock_client = accept(socket_, reinterpret_cast<sockaddr*>(psockaddr), &len_si); // works fine //sock_client = sock_client = wsaaccept(socket_, reinterpret_cast<sockaddr*>(psockaddr), &len_si, null, 0);//works fine char buf[2 * (sizeof(sockaddr_in) + 16)]; wsaoverlapped wsaovl; uint32_t bytes{ 0 }; bool b = acceptex(socket_, sock_client, (pvoid)buf, 0, sizeof(sockaddr_in) + 16, sizeof(sockaddr_in) + 16, reinterpret_cast<lpdword>(&bytes), &wsaovl); //this fails, returns 0 int32_t test = wsagetlasterror(); // returns 6 (wsa_invalid_handle) 

i have no idea why works accept() , wsaaccept(), doesn't acceptex().

if use accept() though, after accepting client need call wsarecv() immediately. i'm not sending client yet read needs called before getqueuedcompletionstatus() in worker thread:

wsabuf* buf = new wsabuf; overlapped* ovl = new overlapped; int32_t flags{ 0 }; int32_t bytes{ 0 }; int32_t bytes_recv = wsarecv(client_socket, buf, 1, &flags, &bytes, ovl, null); // returns -1 int32_t err = wsagetlasterror(); // returns 6 (wsa_invalid_handle) 

and since doesn't work, getqueuedcompletionstatus() routine in worker thread keeps on hanging (or @ least, assume reason)

is there wrong i'm doing? i've been trying search around , fix since yesterday night, know it's not lot of time don't see i'm not doing correctly.

update: have changed way initialize socket acceptex().

socket sock_client = wsasocket(af_inet, sock_stream, ipproto_ip, null, 0, wsa_flag_overlapped); 

and

 wsaoverlapped wsaovl = {}; 

acceptex() still returns false, error returned wsagetlasterror() 997 (wsa_io_pending). i'm not sure i/o operation pending , how go fixing it.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -