2014年5月17日星期六

After the Java NIO when the terminal is closed, how do you know which specific server terminals ?

When a plurality of terminals connected to the server , they are registered to a Hashtable server:

Hashtable clientlist = new Hashtable ();
String username equivalent terminal , SocketChannel is the channel for each terminal.

Now, if a terminal is closed, the server catch this exception , how to determine which terminal it?
------ Solution ---------------------------------------- ----
register (Selector sel, int ops, Object att) when
kept inside the terminal at the Object att username
unusual time through SelectionKey.attachment (), the user name taken out of the terminal

------ For reference only ---------------------------------- -----
nobody???
------ For reference only --------------------- great God ------------------
who I Come save it !
------ For reference only -------------------------------------- -
if (key.isReadable ()) {
int count = 0;
SocketChannel channel = (SocketChannel) key.channel ();
buffer.clear ();
while ((count = channel.read (buffer))> 0) {
buffer.flip ();
/ / while (buffer.hasRemaining ()) {
String receiveText = String.valueOf (cs.decode (buffer) array ().);
System.out.println (receiveText);
dispatch (channel, receiveText);
/ /}
channel.register (selector, SelectionKey.OP_READ);
}
if (count <0) {
channel.close ();
System.out.println (" disconnect ..." + PORT_NUMBER);
}
}
}
I write this , to determine which terminal is disconnected
------ For reference only --------------------- ------------------


Thank you first look !
then ask the dispatch function or what role is it?
------ For reference only -------------------------------------- -
that was pulled over on my side code is
------ For reference only to send and receive data ----------------- ----------------------
disconnect and then connect your terminal to send a message appears closedchannelException do such a problem on my side

------ For reference only ---------------------------------- -----


terminal is disconnected, the server needs to catch the exception , it is key to delete (key.cancel () ;), and then shut down the server corresponding socketchannel enough.

My first question you did not seem to understand, I have to know what sets off the terminal , ( I will give each terminal configurations that number or name ) Which then know what is called terminal it closed.
------ For reference only -------------------------------------- - different LocalPort
end I was configured to each terminal , login and disconnection are LocalPort state judge to determine the status of the terminal
------ For reference only ----- ----------------------------------

I would like to ask :
When a terminal is closed, the server socketchannel.read (buffer); How can immediately find abnormal ? Because

now after my terminal is closed, the server will not be read immediately reported abnormalities , but to wait until the next terminal to communicate to appear abnormal .
------ For reference only -------------------------------------- -

I would like to ask :   
When a terminal is closed, the server socketchannel.read (buffer); How can immediately find abnormal ?   Because
  
now after my terminal is closed, the server will not be read immediately reported abnormalities , but to wait until the next terminal to communicate to appear abnormal .  

read (buffer) normal shutdown will return -1.
forced to close the case, the event will also receive a read , reading time will throw an exception.
you look under there is no return -1.
------ For reference only ------------------------ ---------------

  
read (buffer) normal shutdown will return -1.   
forced to close the case, the event will also receive a read , reading time will throw an exception.   
you look under there is no return -1.  

I took your advice :
int n = channel.read (buffer);
if (n == -1)
System.out.println ("-1-1-1-1"); After
client closes , it will immediately return -1 , but it also has kept execute statement I want to perform: Here is kept output -1-1-1-1
how to do it ?
------ For reference only -------------------------------------- -
returns -1 after , usually with SelectionKey.cancel (); put the key written off .
doing this key principle is to be removed from OP_READ inside. ( With interestOps (int ops) can control more detailed, but the scene may not apply to you .)
------ For reference only --------------- ------------------------


I think the rest of it, thank you!

没有评论:

发表评论