In my current situation, the asynchronous event registration in onaccept () is unsuccessful. The error code obtained with wsagetlasterror () is 0, and the switch cannot get the error message. The following is the original code, my code is well-developed based on it, and the framework is roughly the same,
In fact, what I want to know is whether the socket parameter in the accept parameter uses the server (the parameter being monitored) or the socket psock = (socket) wparam parameter obtained from Windows using the message ing function, I cannot re-register an asynchronous event when I use the server, but I can use psock, but I do not know whether the socket object obtained with this psock is legal. Although the asynchronous event is successfully registered, however, when the client is disconnected, the server still does not respond. I have re-registered the ip_event event.
In message ing, use another message ing function ipevent () to associate with it. Message ing is normal and valid.
# Define network_event user_message + 100 // defines network events
Sockaddr_in clientaddr; file: // temporarily store the Client IP Address
// Customize the message ing function to map the network events defined above to the processing function
// Onnetevent is a network event handler, which is defined below
On_message (network_event, onnetevent );
In the initialization function in your dialog box, call the following sub-function for network initialization.
Bool initnetwork () // initialize the network
{
// Initialize the TCP protocol
Bool ret = wsastartup (makeword (2, 2), & wsadata );
If (Ret! = 0)
{
MessageBox ("failed to initialize socket! ");
Return false;
}
// Create a server socket
Socket serversocket = socket (af_inet, sock_stream, ipproto_tcp );
If (serversocket = invalid_socket)
{
MessageBox ("failed to create socket! ");
Closesocket (m_socket );
Wsacleanup ();
Return false;
}
// Bind to a local port
Sockaddr_in localaddr;
Localaddr. sin_family = af_inet;
Localaddr. sin_port = htons (1688 );
Localaddr. sin_addr.s_addr = 0;
If (BIND (serversocket, (const struct sockaddr *) & localaddr,
Sizeof (sockaddr) = socket_error)
{
MessageBox ("binding address failed! ");
Closesocket (m_socket );
Wsacleanup ();
Return false;
}
// Register an Asynchronous Network event. m_hwnd is the main dialog box of the application or the handle of the main window.
Wsaasyncselect (serversocket, m_hwnd, network_event,
Fd_accept | fd_close | fd_read | fd_write );
Listen (serversocket, 5); // sets the listening mode
Return true;
}
// Define the network event Response Function
Void onnetevent (wparam, lparam)
{
// Call the API function to obtain the network event type
Int ievent = wsagetselectevent (lparam );
// Obtain the client socket that sends this event
Socket psock = (socket) wparam;
Switch (ievent)
{
Case fd_accept: // Client Connection Request
{
Onaccept ();
Break;
}
Case fd_close: // client disconnection event:
{
Onclose (psock );
Break;
}
Case fd_read: // network packet arrival event
{
Onreceive (psock );
Break;
}
Case fd_write: // send Network Data Events
{
Onsend (psock );
Break;
}
Default: break;
}
}
Void onaccept (socet psock) // responds to the Client Connection Request function
{
Int Len = sizeof (sockaddr );
// Call the API function, accept the connection, and return a new socket.
// You can also obtain the Client IP address.
Socket clientsocket = accept (serversocket,
(Struct sockaddr *) & clientaddr, & Len );
// Register an asynchronous event for the new socket. Note that no accept event exists.
If (wsaasyncselect (clientsocket, m_hwnd, ip_event,
Fd_close | fd_read | fd_write) = socket_error)
{
MessageBox ("An error occurred while registering the asynchronous event! ");
Return;
}
// Compile the function to save the client-related information: socket,
// Ip address and logon time
Saveclientsocket (clientsocket, clientaddr, currenttimer );
}
Void onclose (socet psock)
{
// Compile the function, end the communication with the corresponding client, release the corresponding resources, and perform corresponding processing
Endclientsocket (psock );
}
Void onsend (socet psock)
{
// A self-compiled function that performs preprocessing when sending data to the client
Handleonsend (psock );
}
Void onreceive (socet psock)
{
Recv (); // call the API function to read data packets in the network buffer.
// Compile the function to send the data packet and the client that sends the data
// Clientsocket is encapsulated into a network message
Buildnetmsg ();
// A self-compiled function that puts the network message into a message queue and is processed by the worker thread.
Savenetmsg ();
Setevent (); // trigger a working thread with an event object
}