Commit ef828c75 authored by Administrator's avatar Administrator

Closes unclosed streams in message example

The streams used to read and write the message object in the example
4 were not closed. This commit fixes this error.
parent dc979be4
......@@ -7,7 +7,7 @@
<groupId>es.uvigo.esei.dai</groupId>
<artifactId>sockets</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
<name>Ejemplos de DAI - Sockets</name>
<inceptionYear>2014</inceptionYear>
......
......@@ -37,10 +37,10 @@ public class MessageReceiver {
socket.receive(packet);
try {
try (
final ByteArrayInputStream input = new ByteArrayInputStream(buffer);
final ObjectInputStream dataInput = new ObjectInputStream(input);
final ObjectInputStream dataInput = new ObjectInputStream(input)
) {
final Message message = (Message) dataInput.readObject();
System.out.println("SUBJECT: " + message.getSubject());
......
......@@ -32,19 +32,21 @@ import java.util.Date;
public class MessageSender {
public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket()) {
final ByteArrayOutputStream output = new ByteArrayOutputStream(60000);
final ObjectOutputStream dataOutput = new ObjectOutputStream(output);
final Message message = new Message("Time", "Current time: " + new Date());
System.out.println("SUBJECT: " + message.getSubject());
System.out.println("MESSAGE: " + message.getMessage());
dataOutput.writeObject(message);
final DatagramPacket packet = new DatagramPacket(
output.toByteArray(), output.size(), InetAddress.getLocalHost(), 1234
);
socket.send(packet);
try (
final ByteArrayOutputStream output = new ByteArrayOutputStream(1500);
final ObjectOutputStream dataOutput = new ObjectOutputStream(output)
) {
final Message message = new Message("Time", "Current time: " + new Date());
System.out.println("SUBJECT: " + message.getSubject());
System.out.println("MESSAGE: " + message.getMessage());
dataOutput.writeObject(message);
final DatagramPacket packet = new DatagramPacket(
output.toByteArray(), output.size(), InetAddress.getLocalHost(), 60000
);
socket.send(packet);
}
} catch (final IOException e) {
System.out.print("Connection error: ");
System.out.println(e.getMessage());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment