List of usage examples for java.net DatagramSocket DatagramSocket
public DatagramSocket(int port) throws SocketException
From source file:Main.java
public static void main(String args[]) throws IOException { DatagramSocket socket = new DatagramSocket(DAYTIME_PORT); while (true) { byte buffer[] = new byte[256]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); socket.receive(packet);/*from w ww. j a v a 2 s. c om*/ String date = new Date().toString(); buffer = date.getBytes(); // Get response address/port for client from packet InetAddress address = packet.getAddress(); int port = packet.getPort(); packet = new DatagramPacket(buffer, buffer.length, address, port); socket.send(packet); } }
From source file:Main.java
public static void main(String args[]) { try {//from w w w . j a va 2 s. c o m InetAddress ia = InetAddress.getByName("www.java2s.com"); DatagramSocket ds = new DatagramSocket(InetSocketAddress.createUnresolved("google.com", 8080)); byte buffer[] = "hello".getBytes(); DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80); ds.connect(InetSocketAddress.createUnresolved("google.com", 8080)); ds.send(dp); ds.close(); } catch (Exception e) { e.printStackTrace(); } }
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/*from w w w . j a va 2 s .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(); // 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: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);/*ww w . ja va2s .c om*/ 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 {// w w w. ja va2 s . co m int port = 80; DatagramSocket ds = new DatagramSocket(port); byte buffer[] = new byte[BUFSIZE]; while (true) { DatagramPacket dp = new DatagramPacket(buffer, buffer.length); // Receive data ds.receive(dp); // Display address from the datagram packet InetAddress ia = dp.getAddress(); System.out.println(ia); dp.setData(buffer); } } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(String args[]) { try {//from ww w . ja va 2s. c o m int port = 80; DatagramSocket ds = new DatagramSocket(port); byte buffer[] = new byte[BUFSIZE]; while (true) { DatagramPacket dp = new DatagramPacket(buffer, buffer.length); // Receive data ds.receive(dp); // Display address from the datagram packet InetAddress ia = dp.getAddress(); System.out.println(ia); dp.setSocketAddress(InetSocketAddress.createUnresolved("google.com", 8080)); } } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { try {// ww w . j av a 2 s . com 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:WriteServer.java
public static void main(String args[]) throws Exception { int serverPort = 998; int buffer_size = 1024; byte buffer[] = new byte[buffer_size]; DatagramSocket ds = new DatagramSocket(serverPort); int pos = 0;//from w ww. j a va 2 s. co m while (true) { int c = System.in.read(); switch (c) { case -1: System.out.println("Server Quits."); return; case '\r': break; case '\n': ds.send(new DatagramPacket(buffer, pos, InetAddress.getLocalHost(), 999)); pos = 0; break; default: buffer[pos++] = (byte) c; } } }
From source file:MainClass.java
public static void main(String[] args) { for (int port = 1024; port <= 65535; port++) { try {/* w ww .ja v a 2 s.c o m*/ // the next line will fail and drop into the catch block if // there is already a server running on port i DatagramSocket server = new DatagramSocket(port); server.close(); } catch (SocketException ex) { System.out.println("There is a server on port " + port + "."); } } }
From source file:UdpEchoServer.java
public static void main(String[] args) { DatagramSocket sock;/*from w ww .j a v a 2s . co m*/ DatagramPacket pack = new DatagramPacket(new byte[BUFFERSIZE], BUFFERSIZE); try { sock = new DatagramSocket(7); } catch (SocketException e) { System.out.println(e); return; } // echo back everything while (true) { try { sock.receive(pack); sock.send(pack); } catch (IOException ioe) { System.out.println(ioe); } } }