Sunday 15 August 2010

java - The write() method of a SocketChannel do not throw an exception when it should do -


I am writing a server to exchange messages between customers, left to solve an issue How to shut down a customer during closing a customer What do I do to start a monitor thread in which all-client maps are monitored, and if I try to delete a channel, if there is an exception Tried to write it Be. However, after closing a client, the write () method in the monitor thread does not make an exception, so the useless channel will never be released. Anyone know why?

  Public Class Server Monitor Thread {Private map & lt; String, socket channel & gt; AllClients; Private set set; Private Iterator; Private entry entry; Private socket channel channel; Private Byte Buffer Buff; Public server monitors (map & lt; string, socket channel & gt; all cranes) {this.allClients = allClients; Buf = Byte Buffer.Allocate Direct (10); Byte b = 0; Buf.put (b); Buf.flip (); } Public run zero () {while (true) {if (! AllClients.isEmpty ()) {set = allClients.entrySet (); This = set.iterator (); While (this hasNext ()) {entry = (entry) it.next (); Channel = (SocketChannel) entry .getValue (); Try {Channel.write (buf); } Hold (exception e) {allClients.remove (entry.getKey ()); //set.remove(entry); }}} Try {Thread. Sleep (1000 * 5); } Grip (Interrupted e) e.printStackTrace (); }}}   

}

local form of writing in TCP socket From buffered and asynchronously put on the wire then you can not trust the peer to fail after the first written down. You can rely on failing to write later, but you can take the opportunity to write many to get it.

No comments:

Post a Comment