User Datagram Protocol UDP Juan Pablo Araneda Danilo Araya Z.
La Idea... Proveer a las aplicaciones de un servicio de mensajes simple con un protocolo no confiable.
Para establecer una comunicación confiable se requieren MUCHOS mensajes de control. Esto hace que una comunicación confiable se justifique solo cuando tenemos una alta relación: Por que usar un protocolo no confiable?
Aplicaciones mas comunes sobre UDP ECHO Hola!! Cual es la dirección IP de lucas.elo.utfsm.cl? Domain Name Service (DNS)
Características de UDP No orientado a la conexión. Control de errores opcional. Trabaja en base a puertos. Puertos “bien conocidos” para servicios estándar (0-1023). Puertos para asignación dinámica ( ).
Puertos. Host lucas ECHO 7 DNS 53 TIME 37 Que numero IP tiene lucas? Hola!!Que hora es? Hola!! Son las 14:
Estructura del Header. Puerto de origen Puerto de destino Tamaño del datagrama Suma de control Datos Incluye el header minimo 8 máximo Opcional: Si no se usa es Cero
Pseudo Header Se usa para calcular el Checksum. No se transmite!! Se debe generar en el origen para calcular el Checksum y luego en el destino para verificarlo. Tiene la ventaja agregada de verificar el origen y el destino.
Pseudo Header Dirección IP de origen Dirección IP de destino Protocolo =17 Tamaño de la UDP
Procedimiento de envío Header Pseudo Header Checksum Data Pseudo Header Checksum ORIGENDESTINO Header Checksum Data
Bibliografía Internetworking with TCP/IP Douglas Comer Volume I: Principles, Protocols and Architecture. 3era Edición. TCP/IP Arquitectura, protocolos e Implementación con IPv6 y seguridad de IP Dr Sidnie Feit. RFC 768 “User Datagram Protocol”
Ejemplo de aplicación UDP Servidor de eco Hola!!
Listado del Servidor class echoServerThread extends Thread { private DatagramSocket socket = null; echoServerThread() { super("echoServer"); try { socket = new DatagramSocket(); System.out.println("echoServer listening on port: " + socket.getLocalPort()); } catch (java.net.SocketException e) { System.err.println("Could not create datagram socket."); }
Listado del Servidor public void run() { if (socket == null) return; while (true) { try { byte[] buf = new byte[256]; DatagramPacket packet; InetAddress address; int port; String dString = null;
// receive request packet = new DatagramPacket(buf, 256); socket.receive(packet); address = packet.getAddress(); port = packet.getPort(); buf = packet.getData(); // send response packet = new DatagramPacket(buf, buf.length, address, port); socket.send(packet); } catch (IOException e) { System.err.println("IOException: " + e); e.printStackTrace(); } protected void finalize() { if (socket != null) { socket.close(); socket = null; System.out.println("Closing datagram socket."); }
Listado del Cliente class echoUDP { public static void main(String[] args) { int port; InetAddress address; DatagramSocket socket = null; DatagramPacket packet; byte[] sendBuf = new byte[256]; byte[] recvBuf = new byte[256]; if (args.length != 3) { System.out.println("Use: java echoUDP "); return; }
try { // bind to the socket socket = new DatagramSocket(); } catch (java.net.SocketException e) { System.err.println("Could not create datagram socket."); } if (socket != null) { try { // send request port = Integer.parseInt(args[1]); address = InetAddress.getByName(args[0]); sendBuf = args[2].getBytes(); packet = new DatagramPacket(sendBuf, sendBuf.length, address, port); socket.send(packet);
// get response packet = new DatagramPacket(recvBuf, 256); socket.receive(packet); String received = new String(packet.getData()); System.out.println("Respuesta: " + received); System.out.println("Chao!!"); socket.close(); } catch (IOException e) { System.err.println("IOException: " + e); e.printStackTrace(); }