-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMessageSendTask.java
43 lines (38 loc) · 1.2 KB
/
MessageSendTask.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
import java.io.*;
import java.net.Socket;
/**
* A Task class - for communication. Executes requests delivery.
*/
public class MessageSendTask implements Runnable {
private int port;
private String host;
private Message message;
private Widget widget;
/**
* A class constructor.
*/
public MessageSendTask(String host, int port, Message m, Widget w) {
this.port = port;
this.host = host;
this.message = m;
this.widget = w;
}
/**
* Runnable interface method - Open a connection and send the given request.
*/
public void run() {
try {
Socket clientSocket = new Socket(host, port);
InputStream input = clientSocket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
clientSocket.getOutputStream().write(this.message.toString().getBytes());
String reply = reader.readLine();
reader.close();
clientSocket.close();
widget.handleReply(reply);
clientSocket.close();
} catch (IOException e) {
throw new RuntimeException("Failed to open port.", e);
}
}
}