Java Reference
In-Depth Information
moved/renamed, the decoding will decode “as much as it can” while skipping the prop-
erties that it couldn't decode.
The recommendation is to not persist your main classes (even though the XMLEn-
coder is more forgiving), but to create special objects that are simple, hold the basic
information, and do not perform many tasks by themselves.
8-4. Creating a Socket Connection and
Sending Serializable Objects Across the
Wire
Problem
You need to open a network connection, and send/receive objects from it.
Solution
Use Java's New Input Output API version 2 (NIO.2) to send and receive objects. The
following solution utilizes the NIO.2 features of nonblocking sockets (by using Fu-
ture tasks):
// Server Side
hostAddress = new
InetSocketAddress(InetAddress.getByName("127.0.0.1"),
2583);
Future<AsynchronousSocketChannel> serverFuture = null;
AsynchronousServerSocketChannel serverSocketChannel
= AsynchronousServerSocketChannel.open().bind(hostAddress);
serverFuture = serverSocketChannel.accept();
final AsynchronousSocketChannel clientSocket
= serverFuture.get(2000, TimeUnit.MILLISECONDS);
System.out.println("Connected!");
if ((clientSocket != null) && (clientSocket.isOpen())) {
InputStream connectionInputStream
Search WWH ::




Custom Search