-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelayClient.java
More file actions
111 lines (103 loc) · 3.29 KB
/
RelayClient.java
File metadata and controls
111 lines (103 loc) · 3.29 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.net.Socket;
/**
* Example Relay client code. This is a simple program that demonstrates
* what is expected of a relay client. It allows the user to type in messages
* and then it sends these to the server, which relays them to all other
* clients. A thread is used to asynchronously receive messages from the
* server while waiting for user input, but other read mechanisms are
* possible (e.g., polling).
**/
public class RelayClient implements Runnable
{
/** connection to server **/
private Socket mySocket;
/** incoming network message reader **/
private BufferedReader nwin;
/** outgoing network message sender **/
private BufferedWriter nwout;
/** callback object for app for when a message is received **/
private RelayReceiver app;
private boolean debug = false;
/**
* Constructor
* @param rx is the application's RelayReceiver object
**/
public RelayClient(RelayReceiver rx)
{
app = rx;
}
/**
* Connect to a relay server.
* @param serverName is the hostname or IP address of the server
* @param serverPort is the port number of the server
* @return true if connection succeeded, false otherwise
**/
public boolean connect(String serverName, int serverPort)
{
try {
mySocket = new Socket(serverName, serverPort);
} catch (IOException e) {
System.err.println("Error connecting: " + e);
return false;
}
try {
nwout = new BufferedWriter(new OutputStreamWriter(
mySocket.getOutputStream()));
nwin = new BufferedReader(new InputStreamReader(
mySocket.getInputStream()));
} catch (IOException e) {
System.err.println("Error creating reader or writer: " + e);
try {
mySocket.close();
} catch (Exception ce) {}
return false;
}
return true;
}
/**
* Send a message to the relay server. Called by application.
* @param message is the message to send
**/
public void send(String message)
{
try {
nwout.write(message + "\n"); // protocol is newline-based
nwout.flush(); // important to flush buffers!
} catch (IOException e) {
System.err.println("Error sending: " + e);
}
}
/**
* Incoming message listening thread entry point. Will receive
* any incoming messages, and invoke the receiveMessage() handler
* on the application object (if available)
**/
public void run()
{
String inputLine;
while (!mySocket.isInputShutdown()) {
try {
inputLine = nwin.readLine();
} catch (IOException e) {
System.err.println("Error receiving: " + e);
continue;
}
if (inputLine != null) {
if (app != null)
app.receiveMessage(inputLine);
else
System.out.println("No App to Receive: (" + inputLine + ")");
}
}
if (debug) System.out.println("Connection closed...");
try {
mySocket.close();
} catch (Exception e) {}
mySocket = null;
}
} // end class