-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
80 lines (65 loc) · 2.77 KB
/
Copy pathClient.java
File metadata and controls
80 lines (65 loc) · 2.77 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
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.zip.GZIPOutputStream;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Client implements Runnable {
private InetAddress destinationHost;
private Integer destinationPort;
private Thread t;
private Message message;
Client(String destinationHost, Integer destinationPort, Message message) throws IOException {
this.destinationPort = destinationPort;
this.destinationHost = InetAddress.getByName(destinationHost);
this.message = message;
System.out.printf("Client: Preparing to broadcast to %s:%d.\n", destinationHost.toString(), destinationPort);
start();
}
public void start() {
if (t == null) {
t = new Thread (this, "ClientThread");
t.start();
}
}
@Override
public void run() {
try {
// Convert message to JSON object using Jackson
ObjectMapper mapper = new ObjectMapper();
String message = mapper.writeValueAsString(this.message);
// Convert to a byte array stream. Using an output stream will allow
// us to manipulate the bytes
byte[] messageBytes = message.getBytes();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(messageBytes.length);
// Compress the stream using GZIPOutputStream
System.out.printf("Client: Compressing test message.\n");
GZIPOutputStream compressed = new GZIPOutputStream(outputStream);
// Write the compressed message out to messageBytes, then close stream
compressed.write(messageBytes, 0, messageBytes.length);
compressed.flush();
compressed.close();
// Set up the socket to the server. Datagram is UDP.
DatagramSocket clientSocket = new DatagramSocket();
// Construct a packet using the outputStream
DatagramPacket packet = new DatagramPacket(
outputStream.toByteArray(),
outputStream.toByteArray().length,
destinationHost,
destinationPort
);
// Send away!
System.out.printf(
"Client: Sending compressed message. %d compressed bytes, %d uncompressed.\n",
outputStream.toByteArray().length,
messageBytes.length
);
clientSocket.send(packet);
// The client is now finished.
} catch (IOException e) {
// Catch any exception that might occur above and print a stack trace
e.printStackTrace();
}
}
}