forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatagramSocketEx.java
More file actions
35 lines (24 loc) · 970 Bytes
/
DatagramSocketEx.java
File metadata and controls
35 lines (24 loc) · 970 Bytes
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
package com.zetcode;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
// DatagramSocket provides network communication via UDP protocol
// The Quote of the Day (QOTD) service is a member of the Internet protocol
// suite, defined in RFC 865
public class DatagramSocketEx {
public static void main(String[] args) throws IOException {
var hostname = "djxmmx.net";
int port = 17;
var address = InetAddress.getByName(hostname);
try (var socket = new DatagramSocket()) {
var request = new DatagramPacket(new byte[1], 1, address, port);
socket.send(request);
var buffer = new byte[512];
var response = new DatagramPacket(buffer, buffer.length);
socket.receive(response);
var quote = new String(buffer, 0, response.getLength());
System.out.println(quote);
}
}
}