Java 17 Recipes


-4. Creating a Socket Connection and Sending


Download 3.2 Mb.
Pdf ko'rish
bet156/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   152   153   154   155   156   157   158   159   ...   245
Bog'liq
Java 17 Recipes

8-4. Creating a Socket Connection and Sending 
Serializable Objects Across the Wire
 Problem
You need to open a network connection to send/receive objects.
 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 (using 
Future tasks).
public class Ch_8_4_AsyncChannel {
private AsynchronousSocketChannel clientWorker;
InetSocketAddress hostAddress;
public Ch_8_4_AsyncChannel() {
}
ChApTer 8 InpuT And OuTpuT


307
private void start() throws IOException, ExecutionException, 
TimeoutException, InterruptedException {
hostAddress = new InetSocketAddress(InetAddress.
getByName("127.0.0.1"), 2583);
Thread serverThread = new Thread(() -> {
serverStart();
});
serverThread.start();
Thread clientThread = new Thread(() -> {
clientStart();
});
clientThread.start();
}
private void clientStart() {
try {
try (AsynchronousSocketChannel clientSocketChannel = 
AsynchronousSocketChannel.open()) {
Future connectFuture = clientSocketChannel.
connect(hostAddress);
connectFuture.get(); // Wait until connection 
is done.
OutputStream os = Channels.newOutputStream(clientSocke
tChannel);
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");
}
}
} catch (IOException | InterruptedException | 
ExecutionException e) {
ChApTer 8 InpuT And OuTpuT


308
e.printStackTrace();
}
}
private void serverStart() {
try {
AsynchronousServerSocketChannel serverSocketChannel = 
AsynchronousServerSocketChannel.open().bind(hostAddress);
Future serverFuture = 
serverSocketChannel.accept();
final AsynchronousSocketChannel clientSocket = 
serverFuture.get();
System.out.println("Connected!");
if ((clientSocket != null) && (clientSocket.isOpen())) {
try (InputStream connectionInputStream = Channels.
newInputStream(clientSocket)) {
ObjectInputStream ois = null;
ois = new ObjectInputStream(connectionInputStream);
while (true) {
Object object = ois.readObject();
if (object.equals("EOF")) {
clientSocket.close();
break;
}
System.out.println("Received :" + object);
}
ois.close();
}
}
} catch (IOException | InterruptedException | ExecutionException | 
ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException, 
ExecutionException, TimeoutException, InterruptedException {
ChApTer 8 InpuT And OuTpuT


309
Ch_8_4_AsyncChannel example = new Ch_8_4_AsyncChannel();
example.start();
}
}
The output is:
Connected!
Received :Look at me 0
Received :Look at me 1
Received :Look at me 2
Received :Look at me 3
Received :Look at me 4

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   152   153   154   155   156   157   158   159   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling