List of usage examples for java.net DatagramSocket receive
public synchronized void receive(DatagramPacket p) throws IOException
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); System.out.println(packet.getSocketAddress()); buffer = packet.getData();//ww w . jav a 2 s . co m System.out.println(new String(buffer)); }
From source file:Main.java
public static void main(String[] args) throws Exception { byte[] ary = new byte[128]; DatagramPacket pack = new DatagramPacket(ary, 128); // read// w w w . j a va 2 s .com 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(); // write 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: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/* www .j av a 2 s .co 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 Exception { DatagramPacket pack = new DatagramPacket(new byte[BUFFERSIZE], BUFFERSIZE); DatagramSocket sock = new DatagramSocket(7); // echo back everything while (true) { sock.receive(pack); sock.send(pack);/*from www .j ava 2s .c o m*/ } }
From source file:Main.java
public static void main(String[] argv) throws Exception { byte[] inbuf = new byte[256]; // default size DatagramSocket socket = new DatagramSocket(); DatagramPacket packet = new DatagramPacket(inbuf, inbuf.length); socket.receive(packet); int numBytesReceived = packet.getLength(); System.out.println(numBytesReceived); }
From source file:Main.java
public static void main(String[] args) { try {/* www. ja v a 2s .c om*/ 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:Main.java
public static void main(String[] args) throws Exception { DatagramSocket ds = new DatagramSocket(3000); byte[] buf = new byte[1024]; DatagramPacket dp = new DatagramPacket(buf, 1024); ds.receive(dp); String strRecv = new String(dp.getData(), 0, dp.getLength()) + " from " + dp.getAddress().getHostAddress() + ":" + dp.getPort(); System.out.println(strRecv);/*from ww w . ja v a 2s. c o m*/ ds.close(); }
From source file:Main.java
public static void main(String args[]) throws IOException { String host = args[0];//from w w w . j a v a 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:WriteServer.java
public static void main(String args[]) throws Exception { int clientPort = 999; int buffer_size = 1024; byte buffer[] = new byte[buffer_size]; DatagramSocket ds = new DatagramSocket(clientPort); while (true) { DatagramPacket p = new DatagramPacket(buffer, buffer.length); ds.receive(p); System.out.println(new String(p.getData(), 0, p.getLength())); }/*from www . j a v a 2 s .co m*/ }
From source file:DatagramReceiver.java
public static void main(String args[]) throws Exception { int port = Integer.parseInt(args[0]); DatagramSocket ds = new DatagramSocket(port); byte buffer[] = new byte[BUFSIZE]; while (true) { DatagramPacket dp = new DatagramPacket(buffer, buffer.length); ds.receive(dp); System.out.println(new String(dp.getData())); }/*w w w . j a va 2 s . c o m*/ }