forked from BruceEckel/OnJava8-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleClient2.java
More file actions
50 lines (49 loc) · 1.38 KB
/
SimpleClient2.java
File metadata and controls
50 lines (49 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// network/SimpleClient2.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
package network;
import java.net.*;
import java.io.*;
public class SimpleClient2 implements Runnable {
private InetAddress address;
private static int counter = 0;
private int id = counter++;
private static int threadcount = 0;
public static int threadCount() {
return threadcount;
}
public SimpleClient2(InetAddress address) {
System.out.println("Making client " + id);
this.address = address;
threadcount++;
}
@Override
public void run() {
try (
Socket socket =
new Socket(address, ServeOne.PORT);
BufferedReader in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
// Enable auto-flush:
socket.getOutputStream())), true)
) {
for (int i = 0; i < 25; i++) {
out.println("Client " + id + ": " + i);
String str = in.readLine();
System.out.println(str);
}
out.println("END");
} catch (IOException ex) {
throw new RuntimeException(ex);
} finally {
threadcount--; // Ending this thread
}
}
}