Skip to content
Snippets Groups Projects
Commit 2a3b4567 authored by ik1g19's avatar ik1g19
Browse files

add UDP apps

parent 0341d8ee
Branches
No related tags found
No related merge requests found
File added
import java.io.*;
import java.net.*;
class UDPReceiver {
public static void main(String[] args) {
try {
DatagramSocket socket = new DatagramSocket(4321);
byte[] buf = new byte[256];
for (int i = 0; i < 10; i++) {
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
System.out.println("receive DatagramPacket " + (new String(packet.getData())).trim() + " " + packet.getAddress() + ":" + packet.getPort());
}
} catch (Exception e) {
System.out.println("error " + e);
}
}
}
\ No newline at end of file
File added
import java.io.*;
import java.net.*;
class UDPSender{
public static void main(String [] args){
try{
InetAddress address =InetAddress.getByName("isaac-VirtualBox");
DatagramSocket socket = new DatagramSocket();
for (int i=0;i<10;i++) {
byte[] buf = String.valueOf(i).getBytes();
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4321);
socket.send(packet);
System.out.println("send DatagramPacket "
+ new String(packet.getData())
+ " "
+ packet.getAddress()
+ ":"
+ packet.getPort());
Thread.sleep(2000);
}
} catch(Exception e) {
System.out.println("error");
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment