How is the FD_WRITE network event generated when using Event Driven Sockets?

I am working on an event-based event-based application newtwork.

When the client has sent some data, and there is something to read on the socket, the network event FD_READ is generated.

Now, according to my understanding, when the server wants to write through the socket, an event should be created, that is, FD_WRITE. But how will this message be generated?

When there is something readable, FD_READ is automatically generated, but what about FD_WRITE when the server wants to write something?

Anyone who can help me with this confusion please?

The following is a snippet of code:

WSAEVENT hEvent = WSACreateEvent(); WSANETWORKEVENTS events; WSAEventSelect(newSocketIdentifier, hEvent, FD_READ | FD_WRITE); while(1) { //while(1) starts waitRet = WSAWaitForMultipleEvents(1, &hEvent, FALSE, WSA_INFINITE, FALSE); //WSAResetEvent(hEvent); if(WSAEnumNetworkEvents(newSocketIdentifier,hEvent,&events) == SOCKET_ERROR) { //Failure } else { //else event occurred starts if(events.lNetworkEvents & FD_READ) { //recvfrom() } if(events.lNetworkEvents & FD_WRITE) { //sendto() } } } 
+4
source share
1 answer

FD_WRITE means you can write to the socket right now. If the send buffers are full (you send data faster than they can be sent on the network), in the end you can no longer write until you wait a bit.

As soon as you make a write error due to the filling of buffers, this message will be sent to you to inform you that you can resend the sending.

It is also sent when you first open the socket so that you know it there and you can start writing.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms741576(v=vs.85).aspx

The FD_WRITE network event is handled a little differently. The FD_WRITE network event is recorded the first time a socket is connected with a call to connect, ConnectEx, WSAConnect, WSAConnectByList, or WSAConnectByName, or when the socket is accepted with accept, AcceptEx or WSAAccept, and then after the sending fails, WSAEWOULDBLOCK and buffer space appear. Therefore, the application may assume that sending is possible from the first Configuring the FD_WRITE network event and saving until the WSAEWOULDBLOCK transmission is sent. After such a failure, the application will find out that sending is again possible when the FD_WRITE network event and the corresponding event object are set.

So, ideally, you probably hold the flag on whether to write right now. It starts with the truth, but in the end you get WSAEWOULDBLOCK when you call sendto, and you set it to false. After you receive FD_WRITE, you will set the flag to true and resume sending packets.

+3
source

Source: https://habr.com/ru/post/1480084/


All Articles