Java Reference
In-Depth Information
= Channels.newInputStream(clientSocket);
ObjectInputStream ois = null;
ois = new
ObjectInputStream(connectionInputStream);
while (true) {
Object object = ois.readObject();
if (object.equals("EOF")) {
connectionCount.decrementAndGet();
clientSocket.close();
break;
}
System.out.println("Received :" + object);
}
ois.close();
connectionInputStream.close();
}
// Client Side
try (AsynchronousSocketChannel clientSocketChannel
= AsynchronousSocketChannel.open()) {
Future<Void> connectFuture
= clientSocketChannel.connect(hostAddress);
connectFuture.get();
// Wait until
connection is done.
OutputStream os
= Channels.newOutputStream(clientSocketChannel);
try (ObjectOutputStream oos = new
ObjectOutputStream(os)) {
for (int i = 0; i < 5; i++) {
oos.writeObject("Look at me " + i);
Thread.sleep(1000);
}
oos.writeObject("EOF");
}
}
How It Works
Search WWH ::




Custom Search