![]() |
The Java Developers Almanac 1.4Order this book from Amazon. |
e175. Writing to a SocketChannelSee also e173 Creating a Non-Blocking Socket.// Create a direct buffer to get bytes from socket. // Direct buffers should be long-lived and be reused as much as possible. ByteBuffer buf = ByteBuffer.allocateDirect(1024); try { // Fill the buffer with the bytes to write; // see e160 Putting Bytes into a ByteBuffer buf.put((byte)0xFF); // Prepare the buffer for reading by the socket buf.flip(); // Write bytes int numBytesWritten = socketChannel.write(buf); } catch (IOException e) { // Connection may have been closed }
e174. Reading from a SocketChannel e176. Using a Selector to Manage Non-Blocking Sockets e177. Creating a Non-Blocking Server Socket e178. Accepting a Connection on a ServerSocketChannel e179. Using a Selector to Manage Non-Blocking Server Sockets e180. Detecting When a Non-Blocking Socket Is Closed by the Remote Host
© 2002 Addison-Wesley. |