OnJava8-Examples/network/SimpleClient.java
Bruce Eckel 13a6e2c5e6 Put necessary files in packages
Also fixed a small compile bug (can't use static in @BeforeEach)
2016-09-03 13:24:33 -06:00

44 lines
1.3 KiB
Java

// network/SimpleClient.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// Sends lines to the server and
// reads lines the server sends.
// {ValidateByHand}
package network;
import java.net.*;
import java.io.*;
public class SimpleClient {
public static void
main(String[] args) throws IOException {
// Produce the "Local Loopback" IP address
// for testing on one machine w/o a network:
InetAddress addr =
InetAddress.getLoopbackAddress();
System.out.println("addr = " + addr);
// TWR will sure that the socket is closed:
try (
Socket socket = new Socket(addr, SimpleServer.PORT);
BufferedReader in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
// Enable auto-flush:
socket.getOutputStream())), true);
) {
System.out.println("socket: " + socket);
for(int i = 0; i < 10; i ++) {
out.println("hello " + i);
String str = in.readLine();
System.out.println(str);
}
out.println("END");
}
}
}