-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientApp.java
More file actions
92 lines (85 loc) · 2.85 KB
/
ClientApp.java
File metadata and controls
92 lines (85 loc) · 2.85 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
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
81
82
83
84
85
86
87
88
89
90
91
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* Example Client Application that uses the relay client code and
* interacts with the server and other clients
**/
public class ClientApp implements RelayReceiver
{
private RelayClient relayer;
private boolean debug = false;
/**
* Process a message received from the server. This will be called
* in the relayer thread and so it should not do something that takes
* too long, since the relayer thread needs to get back to taking care
* of incoming messages. This thread should extract the message info and
* set up something for the main application thread, or some other thread,
* do use at it runs the app.
* @param msg is the string message that was received
**/
public void receiveMessage(String msg)
{
System.out.println("Received: (" + msg + ")");
}
/**
* Helper method for main, just starts the relay client and connects
* it to the server
* @param serverHost is the hostname or IP address (as a string)
* @param serverPort is the port number the server is using
**/
public boolean startRelayService(String serverHost, int serverPort)
{
relayer = new RelayClient(this); // needs RelayReceiver object
if (!relayer.connect(serverHost,serverPort)) {
System.err.println("Could not connect to server:9001");
return false;
}
System.out.println("Connected...");
// RelayClient object MUST be run in its own thread
(new Thread(relayer)).start();
return true;
}
/**
* Main application method, called from main.
**/
public void doApp()
{
// this "main" application simply reads input from stdin
// and sends it to all other clients
String line;
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println("Enter a string to send:");
try {
line = stdin.readLine();
} catch (Exception e) {
System.err.println("Error reading stdin: " + e);
continue;
}
if (line == null || line.equals(""))
continue;
if (debug) System.out.println("Sending: (" + line + ")");
relayer.send(line);
}
}
/**
* Program entry point.
* @param args is either just hostname/IP or hostname/IP and port#
**/
public static void main(String[] args)
{
int port = 9001;
String host = "localhost";
if (args.length > 2) {
System.out.println("Usage: java ClientApp [machine-name|IP-address] [port]");
return;
} else if (args.length > 0) {
host = args[0];
} else if (args.length > 1) {
port = Integer.parseInt(args[1]);
}
ClientApp app = new ClientApp();
app.startRelayService(host,port);
app.doApp();
}
} // end class