UDP echo application from console client-server implemenation in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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