Download UDP - UTFSM

Document related concepts
no text concepts found
Transcript
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.
Por que usar 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:
# datos
# mensajes _ de _ control
Aplicaciones mas comunes sobre
UDP
Hola!!
Hola!!
• ECHO
• Domain Name
Service (DNS)
Cual es la dirección
200.1.17.130
IP de lucas.elo.utfsm.cl?
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
(1024-65535).
Hola!!
Puertos.
Que numero IP tiene
QueHola!!
hora es?
lucas?
7
ECHO
Host
lucas
DNS 53
TIME
37
Son las 09:47
200.1.17.130
Estructura del Header.
0
16
31
Puerto de
origen
Puerto de
destino
Tamaño del
datagrama
Suma de
control
Datos
Incluye el
header
minimo 8
máximo 65535
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
0
16
31
Dirección IP de origen
Dirección IP de destino
00000000
Protocolo
Tamaño
=17
de la UDP
Procedimiento de envío
Pseudo Header
Pseudo Header
Header
Header
Checksum
Data
Checksum
Data
Checksum
ORIGEN
DESTINO
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 <hostname> <port#> <mensaje>");
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();
}
}
}
}