-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUDPClient.java
28 lines (25 loc) · 989 Bytes
/
UDPClient.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
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.List;
public class UDPClient implements ISend {
private final String address;
private final int port;
private final List<byte[]> messagePackets;
public UDPClient(String address, int port, List<byte[]> messagePackets) {
this.address = address;
this.port = port;
this.messagePackets = messagePackets;
}
public void send() throws IOException {
try (DatagramSocket socket = new DatagramSocket()) {
InetAddress address = InetAddress.getByName(this.address);
for (byte[] segment : this.messagePackets) {
socket.send(new DatagramPacket(segment, segment.length, address, this.port));
}
} catch (IOException e) {
throw new IOException(STR."Ошибка отправки сообщения: \{e.getMessage()}");
}
}
}