java - NIO: Send message and then disconnect immediately -


Under some circumstances, I want to send an error message from a server using non-blocked I / O () SocketChannel.write (ByteBuffer) ) and then disconnect the client I will write the entire contents and then disconnect immediately that I believe the customer can not be to receive this message because I think the OS has not actually sent data at this point .

Is that correct, and if there is a recommended approach to dealing with this situation?

I was thinking of using a clock so that I want to disconnect a customer, then close their connection after 1-2 seconds.

Socket Chinal. Writing non-blocking mode will be the number of bytes, which were sent to the network immediately without blocking can go. Your question thinks me that you expect to use full buffer with the method of writing and try asynchronous attempts to send additional data to the network, but this is not how it is working.

If you really need to make sure that the error message is sent to the client before disconnecting the socket, I only block the method before calling it. To use non-intercept mode, you have to type in a loop, calculate the number of bytes sent by each orientation, and when you are able to pass the entire message in the socket (bad solution, let me know Is unnecessary code, wait and so on).


Comments