Further try-with-resources rewrite
This commit is contained in:
parent
e6dd2198de
commit
11610b8270
@ -10,8 +10,6 @@ import java.io.*;
|
||||
import onjava.*;
|
||||
|
||||
public class ChatterClient extends Thread {
|
||||
// Can listen & send on the same socket:
|
||||
private DatagramSocket s;
|
||||
private InetAddress hostAddress;
|
||||
private byte[] buf = new byte[1000];
|
||||
private DatagramPacket dp =
|
||||
@ -21,21 +19,19 @@ public class ChatterClient extends Thread {
|
||||
public ChatterClient(int identifier) {
|
||||
id = identifier;
|
||||
try {
|
||||
// Auto-assign port number:
|
||||
s = new DatagramSocket();
|
||||
hostAddress =
|
||||
InetAddress.getByName("localhost");
|
||||
} catch(UnknownHostException e) {
|
||||
System.err.println("Cannot find host");
|
||||
System.exit(1);
|
||||
} catch(SocketException e) {
|
||||
System.err.println("Can't open socket");
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
System.out.println("ChatterClient starting");
|
||||
}
|
||||
public void sendAndEcho(String msg) {
|
||||
try {
|
||||
try (
|
||||
// Auto-assign port number:
|
||||
DatagramSocket s = new DatagramSocket();
|
||||
) {
|
||||
// Make and send a datagram:
|
||||
s.send(Dgram.toDatagram(
|
||||
msg, hostAddress, ChatterServer.INPORT));
|
||||
@ -48,6 +44,9 @@ public class ChatterClient extends Thread {
|
||||
dp.getPort() + ": " +
|
||||
Dgram.toString(dp);
|
||||
System.out.println(rcvd);
|
||||
} catch(SocketException e) {
|
||||
System.err.println("Can't open socket");
|
||||
throw new RuntimeException(e);
|
||||
} catch(IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
@ -13,12 +13,12 @@ public class ChatterServer {
|
||||
private byte[] buf = new byte[1000];
|
||||
private DatagramPacket dp =
|
||||
new DatagramPacket(buf, buf.length);
|
||||
// Can listen & send on the same socket:
|
||||
private DatagramSocket socket;
|
||||
|
||||
public ChatterServer() {
|
||||
try {
|
||||
socket = new DatagramSocket(INPORT);
|
||||
// Can listen & send on the same socket:
|
||||
try (
|
||||
DatagramSocket socket = new DatagramSocket(INPORT)
|
||||
) {
|
||||
System.out.println("Server started");
|
||||
while(true) {
|
||||
// Block until a datagram appears:
|
||||
@ -37,9 +37,6 @@ public class ChatterServer {
|
||||
dp.getAddress(), dp.getPort());
|
||||
socket.send(echo);
|
||||
}
|
||||
} catch(SocketException e) {
|
||||
System.err.println("Can't open socket");
|
||||
System.exit(1);
|
||||
} catch(IOException e) {
|
||||
System.out.println("Communication error");
|
||||
throw new RuntimeException(e);
|
||||
|
Loading…
x
Reference in New Issue
Block a user