-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSenderThread.java
More file actions
41 lines (36 loc) · 1.03 KB
/
SenderThread.java
File metadata and controls
41 lines (36 loc) · 1.03 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
import java.io.*;
import java.net.*;
class SenderThread extends Thread {
private InetAddress server;
private DatagramSocket socket;
private int port;
private volatile boolean stopped = false;
SenderThread(DatagramSocket socket, InetAddress address, int port) {
this.server = address;
this.port = port;
this.socket = socket;
this.socket.connect(server, port);
}
public void halt() {
this.stopped = true;
}
@Override
public void run() {
try {
BufferedReader userInput
= new BufferedReader(new InputStreamReader(System.in));
while (true) {
if (stopped) return;
String theLine = userInput.readLine();
if (theLine.equals(".")) break;
byte[] data = theLine.getBytes("UTF-8");
DatagramPacket output
= new DatagramPacket(data, data.length, server, port);
socket.send(output);
Thread.yield();
}
} catch (IOException ex) {
System.err.println(ex);
}
}
}