How to let selector that socketchannel key change in java nio?

If you have a consumer which is too slow, the only option may be to disconnect them to protect your server. You don't want one bad consumer impacting your other clients I usually increase the send buffer size to the point where if it fills, I close the connection. This avoid the complexity of handing unwritten data in Java code because all you are really doing is extending the buffer a little bit more.

If you increase the send buffer size, you are doing this transparently. It is possible you don't even need to play with the send buffer size, the default is usually about 64 KB.

If you have a consumer which is too slow, the only option may be to disconnect them to protect your server. You don't want one bad consumer impacting your other clients. I usually increase the send buffer size to the point where if it fills, I close the connection.

This avoid the complexity of handing unwritten data in Java code because all you are really doing is extending the buffer a little bit more. If you increase the send buffer size, you are doing this transparently. It is possible you don't even need to play with the send buffer size, the default is usually about 64 KB.

I use the followings to change the socketchannel buffersize. But why serverSocket only can set receivebuffer size. Socket.socket().

SetReceiveBufferSize(256 * 1024); socket.socket(). SetSendBufferSize(256 * 1024); – susan Nov 12 at 22:56 1 @susan ServerSocket lets you change the receive buffer size, which is inherited by accepted sockets, so you can set them to > 64k. If you try to do that to the accepted socket it will fail because > 64k needs the TCP 'window scaling' option which is negotiated during the connect handshake.

For the same reason if you want to set a receive buffer > 64k on a client socket you have to do it before connecting it. You can set the send buffer any time as it doesn't require help from the protocol. – EJP Nov 13 at 1:57 @susan correction: it won't 'fail' but the part above 64k won't be used unless the size is set before the connection.

– EJP Nov 13 at 3:34 Thank you very much. – susan Nov 13 at 3:38 Is there any solution to solve my problem presented above? I find in my code many queued data cannot be written out.

When the key is writable, the generator may write data again, which cause the queued data has less change to be written out. How to make this part right? Thanks.

– susan Nov 13 at 3:45.

You have to make sure that new data gets enqueued after data that is already pending to be written. If the behaviour persists, you really only have two choices: either disconnect the client on the grounds of misbehaviour, or stop producing output for it until the backlog clears. Probably both.

You can implement the first via adroit use of a longish select() timeout. If select() returns zero, it means either there are no registered channels or nothing whatsoever has happened to any of them during the timeout period, in which case you might want to think about disconnecting from all clients. If you have a lot of concurrent clients that gets too hamfisted to work, so you would have to keep track of the last time each channel got selected, and disconnect any channel whose last activity time gets too long ago.

Within that timeout period you may also want to stop producing output for that guy while he is slow reading. The precise definition of 'longish' is left as an exercise for the reader, but ten minutes comes to mind as a first approximation.

I appreciate yor help very much. It works by letting the message written out before generating next message at the generator side. But for the other process, if the buffer is full, wait unitl there is room to write again.

– susan Nov 13 at 20:35.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions