The client creates a DatagramSocket
and
uses it to connect()
to the remote server.
Data is sent in a DatagramPacket
, with a payload
in bytes. Lines read from stdin are converted to bytes
(more detail in the next chapter), and are then
send()
to the server. A reply
is read by receive()
, its payload
extracted and converted to a string.
If a read fails because a packet is lost, a timeout exception is triggered, and here just reports an error message.
The code is EchoClient.java
// adapted from "https://www.baeldung.com/udp-in-java
import java.net.*;
import java.io.IOException;
import java.io.*;
import java.nio.charset.StandardCharsets;
public class EchoClient {
public static void main(String[] args){
DatagramSocket socket = null;
InetAddress address = null;
int ONE_SECOND = 1000;
int SERVER_PORT = 2000;
byte[] buf;
if (args.length != 1) {
System.err.println("Usage: Client address");
System.exit(1);
}
try {
address = InetAddress.getByName(args[0]);
} catch(UnknownHostException e) {
e.printStackTrace();
System.exit(2);
}
try {
//socket = new DatagramSocket(SE
socket = new DatagramSocket();
socket.connect(address, SERVER_PORT);
} catch(IOException e) {
e.printStackTrace();
System.exit(3);
}
BufferedReader consoleReader =
new BufferedReader(new InputStreamReader(System.in));
String line = null;
while (true) {
line = null;
try {
System.out.print("Enter line:");
line = consoleReader.readLine();
System.out.println("Read '" + line + "'");
} catch(IOException e) {
e.printStackTrace();
System.exit(6);
}
if (line.equals("BYE"))
break;
buf = line.getBytes(StandardCharsets.UTF_8);
DatagramPacket packet
= new DatagramPacket(buf, buf.length, address, 2000);
try {
socket.send(packet);
} catch(IOException e) {
System.err.println("Can't send to server");
continue;
}
try {
socket.receive(packet);
} catch(SocketTimeoutException e) {
System.err.println("Timeout read from server");
continue;
} catch(IOException e) {
System.err.println("Failed read from server");
continue;
}
System.out.println(new String(packet.getData(), StandardCharsets.UTF_8));
}
}
}
The following server is adapted from A Guide To UDP In Java
// adapted from "https://www.baeldung.com/udp-in-java
import java.net.*;
import java.io.IOException;
public class EchoServer {
private DatagramSocket socket;
private byte[] buf = new byte[2048];
public EchoServer() {
try {
socket = new DatagramSocket(2000);
} catch(SocketException e) {
System.err.println("can't start server");
System.exit(1);
}
while (true) {
DatagramPacket packet
= new DatagramPacket(buf, buf.length);
try {
socket.receive(packet);
} catch (IOException e) {
// ignore
continue;
}
InetAddress address = packet.getAddress();
int port = packet.getPort();
// send using the length of data received
packet = new DatagramPacket(buf, packet.getLength(), address, port);
try {
socket.send(packet);
} catch (IOException e) {
// ignore
continue;
}
}
}
public static void main(String[] args) {
new EchoServer();
}
}
Copyright © Jan Newmarch, jan@newmarch.name
" Network Programming using Java, Go, Python, Rust, JavaScript and Julia"
by
Jan Newmarch
is licensed under a
Creative Commons Attribution-ShareAlike 4.0 International License
.
Based on a work at
https://jan.newmarch.name/NetworkProgramming/
.