-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalcClient.java
More file actions
38 lines (31 loc) · 880 Bytes
/
Copy pathCalcClient.java
File metadata and controls
38 lines (31 loc) · 880 Bytes
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
package threadbook.ch15;
import java.io.*;
import java.net.*;
public class CalcClient extends Object {
public static void main(String[] args) {
String hostname = "localhost";
int port = 2001;
try {
Socket sock = new Socket(hostname, port);
DataInputStream in = new DataInputStream(
new BufferedInputStream(sock.getInputStream()));
DataOutputStream out = new DataOutputStream(
new BufferedOutputStream(sock.getOutputStream()));
double val = 4.0;
out.writeDouble(val);
out.flush();
double sqrt = in.readDouble();
System.out.println("sent up " + val + ", got back " + sqrt);
// Don't ever send another request, but stay alive in
// this eternally blocked state.
Object lock = new Object();
while ( true ) {
synchronized ( lock ) {
lock.wait();
}
}
} catch ( Exception x ) {
x.printStackTrace();
}
}
}