En esta práctica se realizará un ensayo con diferentes protocolos de acceso a servicios de amplia difusión en Internet: localtime, http, pop3.
[fdiaz@infodep03 fdiaz]$ telnet 192.168.1.149 13 Trying 192.168.1.149... Connected to infodep03 (192.168.1.149). Escape character is '^]'. 14 MAR 2005 11:39:13 CET Connection closed by foreign host. [fdiaz@infodep03 fdiaz]$
[fdiaz@infodep03 fdiaz]$ telnet 192.168.1.149 80 Trying 192.168.1.149... Connected to infodep03 (192.168.1.149). Escape character is '^]'. GET /~fdiaz/sd/index.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD><TITLE>Pagina de la asignatura SD</TITLE> <META content=document name=resource-type> <META content=global name=distribution> <META http-equiv=Content-Type content="text/html; charset=iso-8859-1"> <META content="Fernando Diaz" name=author> <META content="Microsoft FrontPage 4.0" name=GENERATOR> <META http-equiv="refresh" content="0; url=http://infodep03/~fdiaz/sd/index20050119.html"> </HEAD> <BODY> </BODY></HTML> Connection closed by foreign host. [fdiaz@infodep03 fdiaz]$Ejemplo de solicitud de una página inexistente:
[fdiaz@infodep03 fdiaz]$ telnet 192.168.1.149 80 Trying 192.168.1.149... Connected to infodep03 (192.168.1.149). Escape character is '^]'. GET /indice.html <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>404 Not Found</title> </head><body> <h1>Not Found</h1> <p>The requested URL /indice.html was not found on this server.</p> <hr /> <address>Apache-AdvancedExtranetServer/2.0.47 (Mandrake Linux/6mdk) mod_perl/1.99_09 Perl/v5.8.1 mod_ssl/2.0.47 OpenSSL/0.9.7b PHP/4.3.2 Server at 192.168.1.149 Port 80</address> </body></html> Connection closed by foreign host. [fdiaz@infodep03 fdiaz]$
Nos conectamos como el usuario klinton:
[fdiaz@infodep03 fdiaz]$ telnet 192.168.1.149 110 Trying 192.168.1.149... Connected to infodep03 (192.168.1.149). Escape character is '^]'. +OK POP3 infodep03 v2003.83mdk server ready USER pop3 +OK User name accepted, password please PASS .pop3. +OK Mailbox open, 3 messages
Solicitamos varios servicios: el estado del buzón, el tamaño del segundo mensaje, recuperamos el mensaje número 21, a continuación lo borramos del buzón, y, por último, terminamos la conexión con el servidor.
STAT +OK 3 1539 LIST +OK Mailbox scan listing follows 1 485 2 498 3 556 . RETR 2 +OK 498 octets Return-Path: <fdiaz@infodep03.euisg.uva.es> X-Original-To: pop3@infodep03 Delivered-To: pop3@infodep03.euisg.uva.es Received: by infodep03.euisg.uva.es (Postfix, from userid 500) id E3342583A5; Mon, 14 Mar 2005 11:56:46 +0100 (CET) To: pop3@infodep03.euisg.uva.es Subject: otro mail de prueba Message-Id: <20050314105646.E3342583A5@infodep03.euisg.uva.es> Date: Mon, 14 Mar 2005 11:56:46 +0100 (CET) From: fdiaz@infodep03.euisg.uva.es (Fernando Diaz Gomez) Status: Hola, que tal? . QUIT +OK Sayonara Connection closed by foreign host. [fdiaz@infodep03 fdiaz]$
Recrearemos los ejemplos anteriores construyendo pequeños programas usando el lenguaje Java. El siguiente ejemplo usa el servicio localtime mediante sockets TCP.
import java.net.*; import java.io.*; public class GetTime { public static void main(String[] args) { Socket client = null; try { client = new Socket("infodep03", 13); } catch (UnknownHostException hostError) { System.err.println(hostError.getMessage()); System.exit(1); } catch (IOException genericError) { System.err.println(genericError.getMessage()); System.exit(1); } try { DataInputStream in = new DataInputStream(client.getInputStream()); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println("Recibido: " + inputLine); } } catch (IOException IOError) { System.err.println(IOError.getMessage()); System.exit(1); } } }Lo mismo se puede realizar mediante sockets UDP.
import java.net.*; public class UDPTest { public static void main(String[] args) throws Exception { DatagramSocket socket; DatagramPacket packet; InetAddress address; byte[] message = new byte[256]; // // Send empty request // socket = new DatagramSocket(); address=InetAddress.getByName("infodep03"); packet = new DatagramPacket(message, message.length, address, 13); socket.send(packet); // // Receive reply and display on screen // packet = new DatagramPacket(message, message.length); socket.receive(packet); String received =new String(packet.getData(), 0); System.out.println("Received: " + received); socket.close(); } }
Finalmente, se propone la compilación y ejecución de los siguientes programas que utilizan sockets:
Ejemplos en C | Ejemplos en Java | |
Protocolo UDP | cliente: socketudpcli.c servidor: socketudpser.c |
cliente: socketudpcli.java servidor: socketudpser.java |
Protocolo TCP | cliente: sockettcpcli.c servidor: sockettcpser.c |
cliente: sockettcpcli.java servidor: sockettcpser.java |
Entre otras cosas se pueden probar: