JavaUDPServer |
JavaUDPClient |
JavaUDPServer.java
package javaudpserver; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.util.logging.Level; import java.util.logging.Logger; /** * * @web http://java-buddy.blogspot.com/ */ public class JavaUDPServer { private static DatagramSocket datagramSocket; private static final int BUFFER_SIZE = 1024; private static byte[] buffer; public static void main(String[] args) { if (args.length != 1) { System.out.println("usage: java -jar JavaUDPServer.jar <port>"); System.exit(1); } int port = Integer.parseInt(args[0]); System.out.println("Port: " + port); try { datagramSocket = new DatagramSocket(port); buffer = new byte[BUFFER_SIZE]; DatagramPacket in_datagramPacket = new DatagramPacket( buffer, BUFFER_SIZE); datagramSocket.receive(in_datagramPacket); InetAddress clientAddress = in_datagramPacket.getAddress(); int clientPort = in_datagramPacket.getPort(); String in_message = new String( in_datagramPacket.getData(), 0, in_datagramPacket.getLength()); System.out.println("received: " + in_message); String out_messagae = "echo: " + in_message; DatagramPacket out_datagramPacket= new DatagramPacket( out_messagae.getBytes(), out_messagae.length(), clientAddress, clientPort); datagramSocket.send(out_datagramPacket); } catch (SocketException ex) { Logger.getLogger(JavaUDPServer.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(JavaUDPServer.class.getName()).log(Level.SEVERE, null, ex); } finally { datagramSocket.close(); } } }
JavaUDPClient.java
package javaudpclient; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; /** * * @web http://java-buddy.blogspot.com/ */ public class JavaUDPClient { private static DatagramSocket datagramSocket; private static final int BUFFER_SIZE = 1024; private static byte[] buffer; public static void main(String[] args) { if (args.length != 2) { System.out.println("usage: java -jar JavaUDPClient.jar <IP address> <port>"); System.exit(1); } try { InetAddress inetAddress = InetAddress.getByName(args[0]); int port = Integer.parseInt(args[1]); Scanner userScanner = new Scanner(System.in); String userInput = userScanner.nextLine(); datagramSocket = new DatagramSocket(); DatagramPacket out_datagramPacket = new DatagramPacket( userInput.getBytes(), userInput.length(), inetAddress, port); datagramSocket.send(out_datagramPacket); buffer = new byte[BUFFER_SIZE]; DatagramPacket in_datagramPacket = new DatagramPacket(buffer, BUFFER_SIZE); datagramSocket.receive(in_datagramPacket); String serverEcho = new String( in_datagramPacket.getData(), 0, in_datagramPacket.getLength()); System.out.println(serverEcho); } catch (UnknownHostException ex) { Logger.getLogger(JavaUDPClient.class.getName()).log(Level.SEVERE, null, ex); } catch (SocketException ex) { Logger.getLogger(JavaUDPClient.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(JavaUDPClient.class.getName()).log(Level.SEVERE, null, ex); } finally { datagramSocket.close(); } } }
Compare with: Simple example of TCP Client/Server communication
No comments:
Post a Comment