c - Making udp socket non-blocking -
i had been working on non-blocking udp socket. code had developed generates window message whenever there data read on socket. below code snippet:
void createsocket(hwnd hwnd) { ///socket binding/// wsadata wsa; ///initialise winsock/// if (wsastartup(makeword(2,2),&wsa) != 0) { exit(exit_failure); } ///create socket/// if((socketidentifier = socket(af_inet , sock_dgram , 0 )) == invalid_socket) { //socket creation failed } ///socket created/// ///prepare sockaddr_in structure/// serversocket.sin_family = af_inet; serversocket.sin_addr.s_addr = inaddr_any; serversocket.sin_port = htons( port ); ///bind/// if( bind(socketidentifier ,(struct sockaddr *)&serversocket , sizeof(serversocket)) == socket_error) { //bind failed } wsaasyncselect (socketidentifier, hwnd, my_message_notification, fd_read | fd_connect | fd_close | fd_accept); //set ///bind done/// } lresult callback wndproc(hwnd hwnd, uint msg, wparam wparam, lparam lparam); int winapi winmain( hinstance hinstance, hinstance hprevinstance, lpstr lpcmdline, int nshowcmd { //window created createsocket() //socket created while(getmessage(&msg, null, 0, 0) > 0) //check on window messages { translatemessage(&msg); dispatchmessage(&msg); } return msg.wparam; } lresult callback wndproc(hwnd hwnd, uint msg, wparam wparam, lparam lparam) { switch(msg) { case my_message_notification: //is message being sent? { switch (lparam) //if so, 1 it? { case fd_accept: //connection request made break; case fd_connect: //connection made break; case fd_read: receiveatsocket(); break; case fd_close: //lost connection break; } } break; } }
this code working fine, , socket not have wait in call snedto() or recvfrom(). instead, window message generated whenever data ready read or written on socket.
now, want find out other way of informing data data ready rather window message. i.e. don't want window message generated whenever there data read or written on socket.
is there other way of implementing above mentioned functionality without using window messages> please me.
waiting :(
another way use waitforsingleobject or waitformultipleobjects or brethren. can see code doing here: asynchronous winsock server wrapper, cpu lagging - c++
Comments
Post a Comment