-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleTalkClient.java
82 lines (52 loc) · 2.36 KB
/
SimpleTalkClient.java
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* Simple client to pass strings to the server.
Modeled after the simpleserver client code in the text. */
/* Paul Gray
Networking F10
Comments stripped for this sample code...your code should
describe what sections play what role. */
import java.io.*;
import java.net.Socket;
import java.net.InetAddress;
public class SimpleTalkClient {
static final int SERVER_PORT = 8888;
static final int MAX_LINE = 256;
private static PrintStream ps;
private static Socket sock;
private static String hostname = "";
public static void main(String args []) {
if(args.length == 1) {
hostname = args[0];
}
else {
System.err.println("Usage: java SimpleTalkClient [host]");
System.exit(1);
}//endif
try{
sock = new Socket(InetAddress.getByName(hostname), SERVER_PORT);
BufferedReader bufftype = new BufferedReader(
new InputStreamReader(System.in));
ps = new PrintStream(
sock.getOutputStream());
try{
String teletype = "";
while (teletype != null) {
teletype = bufftype.readLine();
if(teletype != null){
ps.println(teletype);
}
}//endwhile
}catch(Exception e) {System.out.println(e);
closeConnections();
}
finally { closeConnections(); }
}catch(IOException ioe) { System.out.println("SimpleTalk: unknown host: "
+ hostname);}
}//endmain
public static void closeConnections() {
try {
ps.close();
sock.close();
System.exit(0);
}catch(Exception e) {System.out.println(e);}//endtry
}//endcloseConnections
}//endSimpleTalkClient