Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@absalomhr
Last active March 17, 2019 05:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save absalomhr/0ee2c68942225dbf1d82a3d9b49c33c8 to your computer and use it in GitHub Desktop.
Save absalomhr/0ee2c68942225dbf1d82a3d9b49c33c8 to your computer and use it in GitHub Desktop.
UDP echo application from console client-server implemenation in Java
import java.net.*;
import java.io.*;
public class EchoReceive {
// Server
public static void main(String[] args) {
try {
DatagramSocket s = new DatagramSocket(1234); // port 1234
System.out.println("Waiting for datagrams");
for (;;) {
DatagramPacket p = new DatagramPacket(new byte[65535], 65535); // 65535: Max. space for a datagram
s.receive(p); // Receiving the datagram
System.out.println("Datagram received from: " + p.getAddress() + " with port: " + p.getPort());
byte[] b = p.getData(); // Storing received bytes in an byte array for later convertion to String
// b: Where are we taking the bytes from
// 0: From which position are we starting to read the bytes
// p.getLength(): How many bytes are we reading
String data = new String(b, 0, p.getLength());
// We send back the datagram we just received to the sender
DatagramPacket p1 = new DatagramPacket(b, p.getLength(), p.getAddress(), p.getPort());
s.send(p1);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.net.*;
import java.io.*;
public class EchoSend {
//Client
public static void main(String[] args) {
String host = "127.0.0.1";
int port = 1234;
InetAddress dst = null; // Where are we sending the datagrams to
try {
dst = InetAddress.getByName(host);
} catch (UnknownHostException e) {
System.err.println("Non valid address");
System.exit(1);
}
try {
for (;;) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // System.in = console
System.out.println("Write your message: ");
String msj = br.readLine(); // Getting message from console
DatagramSocket cl = new DatagramSocket();
byte[] b = msj.getBytes(); // String to bytes for sending it in a DatagramPacket
DatagramPacket p = new DatagramPacket(b, b.length, dst, port); // Creating the datagram to send
cl.send(p); // Sending the datagram
// We are going to receive back the datagram we just sent
DatagramPacket p1 = new DatagramPacket(new byte[65535], 65535); // Creating a new datagram where we receive the echo (the above sent datagram)
cl.receive(p1);
System.out.println("Datagram received from: " + p.getAddress() + " with port: " + p.getPort());
byte[] b2 = p1.getData(); // Storing received bytes in an byte array for later convertion to String
// b2: Where are we taking the bytes from
// 0: From which position are we starting to read the bytes
// p1.getLength(): How many bytes are we reading
String data = new String(b2, 0, p1.getLength());
// Printing the received echo
System.out.println(data);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment