List of usage examples for java.net DatagramPacket getData
public synchronized byte[] getData()
From source file:Main.java
public static void main(String args[]) { try {/*from w w w .jav a2 s. c om*/ int port = 80; DatagramSocket ds = new DatagramSocket(port); while (true) { byte buffer[] = new byte[BUFSIZE]; DatagramPacket dp = new DatagramPacket(buffer, buffer.length, InetAddress.getByName("google.com"), 8080); ds.receive(dp); String str = new String(dp.getData()); System.out.println(str); } } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(String args[]) { try {/*from w w w . j a v a 2s . c o m*/ int port = 80; DatagramSocket ds = new DatagramSocket(port); while (true) { byte buffer[] = new byte[BUFSIZE]; DatagramPacket dp = new DatagramPacket(buffer, 0, buffer.length, InetAddress.getByName("google.com"), 8080); ds.receive(dp); String str = new String(dp.getData()); System.out.println(str); } } catch (Exception e) { e.printStackTrace(); } }
From source file:PacketReceiver.java
public static void main(String[] args) throws Exception { byte[] buffer = new byte[1024]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); DatagramSocket socket = new DatagramSocket(5002); socket.receive(packet);/*w ww. jav a2 s.co m*/ System.out.println(packet.getSocketAddress()); buffer = packet.getData(); System.out.println(new String(buffer)); }
From source file:Main.java
public static void main(String args[]) { try {/*from w ww .ja v a2 s. c o m*/ int port = 80; DatagramSocket ds = new DatagramSocket(port); while (true) { byte buffer[] = new byte[BUFSIZE]; DatagramPacket dp = new DatagramPacket(buffer, buffer.length, InetSocketAddress.createUnresolved("google.com", 8080)); ds.receive(dp); String str = new String(dp.getData()); System.out.println(str); } } catch (Exception e) { e.printStackTrace(); } }
From source file:GetDate.java
public static void main(String args[]) throws Exception { byte msg[] = new byte[256]; DatagramSocket dgSocket = new DatagramSocket(); InetAddress destination = InetAddress.getByName("web.mit.edu"); DatagramPacket datagram = new DatagramPacket(msg, msg.length, destination, PortDayTime); dgSocket.send(datagram);/*from w w w. j a va2s . com*/ datagram = new DatagramPacket(msg, msg.length); dgSocket.receive(datagram); String received = new String(datagram.getData()); System.out.println("The time in Cambridge is now: " + received); dgSocket.close(); }
From source file:UDP0.java
public static void main(String[] args) throws Exception { byte[] ary = new byte[128]; DatagramPacket pack = new DatagramPacket(ary, 128); if (args[0].charAt(0) == 'r') { // read//from w w w . j a v a 2s .c o m DatagramSocket sock = new DatagramSocket(1111); sock.receive(pack); String word = new String(pack.getData()); System.out.println("From: " + pack.getAddress() + " Port: " + pack.getPort()); System.out.println(word); sock.close(); } else { // write DatagramSocket sock = new DatagramSocket(); pack.setAddress(InetAddress.getByName(args[1])); pack.setData(args[2].getBytes()); pack.setPort(1111); sock.send(pack); sock.close(); } }
From source file:Main.java
public static void main(String args[]) throws IOException { String host = args[0];//from w w w . j ava 2 s . c o m byte message[] = new byte[256]; InetAddress address = InetAddress.getByName(host); System.out.println("Checking at: " + address); DatagramPacket packet = new DatagramPacket(message, message.length, address, DAYTIME_PORT); DatagramSocket socket = new DatagramSocket(); socket.send(packet); packet = new DatagramPacket(message, message.length); socket.receive(packet); String time = new String(packet.getData()); System.out.println(time); socket.close(); }
From source file:ChatClient.java
public static void main(String[] args) throws Exception { int PORT = 4000; byte[] buf = new byte[1000]; DatagramPacket dgp = new DatagramPacket(buf, buf.length); DatagramSocket sk;// w w w . j ava 2 s. c om sk = new DatagramSocket(PORT); System.out.println("Server started"); while (true) { sk.receive(dgp); String rcvd = new String(dgp.getData(), 0, dgp.getLength()) + ", from address: " + dgp.getAddress() + ", port: " + dgp.getPort(); System.out.println(rcvd); BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); String outMessage = stdin.readLine(); buf = ("Server say: " + outMessage).getBytes(); DatagramPacket out = new DatagramPacket(buf, buf.length, dgp.getAddress(), dgp.getPort()); sk.send(out); } }
From source file:Main.java
public static void main(String[] args) { try {/*from w w w . j a v a 2 s . co m*/ byte[] buffer = new byte[1024]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); DatagramSocket socket = new DatagramSocket(5002); System.out.println("Waiting for a packet..."); socket.receive(packet); System.out.println("Just received packet from " + packet.getSocketAddress()); buffer = packet.getData(); System.out.println(new String(buffer)); } catch (IOException e) { e.printStackTrace(); } }
From source file:UdpEchoClient.java
public static void main(String[] args) { InetAddress address;/* w ww .ja va 2 s . c o m*/ try { address = InetAddress.getByName(args[0]); } catch (UnknownHostException host) { System.out.println(host); return; } DatagramPacket pack = new DatagramPacket(testString.getBytes(), testString.length(), address, 7); DatagramPacket incoming = new DatagramPacket(new byte[256], 256); DatagramSocket sock = null; try { Calendar start, end; sock = new DatagramSocket(); start = Calendar.getInstance(); sock.send(pack); sock.setSoTimeout(5000); sock.receive(incoming); end = Calendar.getInstance(); String reply = new String(incoming.getData()); reply = reply.substring(0, testString.length()); if (reply.equals(testString)) { System.out.println("Success"); System.out.println("Time = " + (end.getTime().getTime() - start.getTime().getTime()) + "mS"); } else System.out.println("Reply data did not match"); } catch (SocketException socke) { System.out.println(socke); } catch (IOException ioe) { System.out.println(ioe); } finally { sock.close(); } }