Further try-with-resources rewrite

This commit is contained in:
Bruce Eckel 2016-09-03 12:17:25 -06:00
parent e6dd2198de
commit 11610b8270
2 changed files with 11 additions and 15 deletions

View File

@ -10,8 +10,6 @@ import java.io.*;
import onjava.*; import onjava.*;
public class ChatterClient extends Thread { public class ChatterClient extends Thread {
// Can listen & send on the same socket:
private DatagramSocket s;
private InetAddress hostAddress; private InetAddress hostAddress;
private byte[] buf = new byte[1000]; private byte[] buf = new byte[1000];
private DatagramPacket dp = private DatagramPacket dp =
@ -21,21 +19,19 @@ public class ChatterClient extends Thread {
public ChatterClient(int identifier) { public ChatterClient(int identifier) {
id = identifier; id = identifier;
try { try {
// Auto-assign port number:
s = new DatagramSocket();
hostAddress = hostAddress =
InetAddress.getByName("localhost"); InetAddress.getByName("localhost");
} catch(UnknownHostException e) { } catch(UnknownHostException e) {
System.err.println("Cannot find host"); System.err.println("Cannot find host");
System.exit(1); System.exit(1);
} catch(SocketException e) {
System.err.println("Can't open socket");
throw new RuntimeException(e);
} }
System.out.println("ChatterClient starting"); System.out.println("ChatterClient starting");
} }
public void sendAndEcho(String msg) { public void sendAndEcho(String msg) {
try { try (
// Auto-assign port number:
DatagramSocket s = new DatagramSocket();
) {
// Make and send a datagram: // Make and send a datagram:
s.send(Dgram.toDatagram( s.send(Dgram.toDatagram(
msg, hostAddress, ChatterServer.INPORT)); msg, hostAddress, ChatterServer.INPORT));
@ -48,6 +44,9 @@ public class ChatterClient extends Thread {
dp.getPort() + ": " + dp.getPort() + ": " +
Dgram.toString(dp); Dgram.toString(dp);
System.out.println(rcvd); System.out.println(rcvd);
} catch(SocketException e) {
System.err.println("Can't open socket");
throw new RuntimeException(e);
} catch(IOException e) { } catch(IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@ -13,12 +13,12 @@ public class ChatterServer {
private byte[] buf = new byte[1000]; private byte[] buf = new byte[1000];
private DatagramPacket dp = private DatagramPacket dp =
new DatagramPacket(buf, buf.length); new DatagramPacket(buf, buf.length);
// Can listen & send on the same socket:
private DatagramSocket socket;
public ChatterServer() { public ChatterServer() {
try { // Can listen & send on the same socket:
socket = new DatagramSocket(INPORT); try (
DatagramSocket socket = new DatagramSocket(INPORT)
) {
System.out.println("Server started"); System.out.println("Server started");
while(true) { while(true) {
// Block until a datagram appears: // Block until a datagram appears:
@ -37,9 +37,6 @@ public class ChatterServer {
dp.getAddress(), dp.getPort()); dp.getAddress(), dp.getPort());
socket.send(echo); socket.send(echo);
} }
} catch(SocketException e) {
System.err.println("Can't open socket");
System.exit(1);
} catch(IOException e) { } catch(IOException e) {
System.out.println("Communication error"); System.out.println("Communication error");
throw new RuntimeException(e); throw new RuntimeException(e);