8

Click here to load reader

Sockets

Embed Size (px)

Citation preview

Page 1: Sockets

BENEMERITA UNIVERSIDAD

AUTONOMA DE PUEBLA

FACULTAD DE CIENCIASDE LA COMPUTACION

PROGRAMACION CONCURRENTE Y PARALELA

SOCKETS

LEZAMA SANCHEZ ANA LAURAVALENCIA TOXQUI HUMBERTO

1. OBJETIVO DEL PROGRAMA

Page 2: Sockets

Comunicarse con direcciones de Internet y con ellas obtener el ip del servidor. El otro programa obtiene el ip de nuestra computadora. Lee un archivo a traves de internet. Como leer un archivo de texto y uno de imagen(GIF)

2.- ¿QUE CLASES UTILIZA DEL PAQUETE NET DE JAVA?InetAddressURLSocket

3.- ¿CUÁL ES LA RELACIÓN CON LOS PROCESOS CONCURRENTES? Esto hace que se pueda conseguir comunicar computadoras dentro de una red, incluso en Internet permitiendo asi realizar programas concurrentes en red.

PROGRAMASPROGRAMA 1

/*Como obtener la direccion IP local*/

import java.net.*;public class pruebaGetLocalHost { public static void main(String args[])

{ InetAddress miDireccionIP = null; Try

{ miDireccionIP = InetAddress.getLocalHost();

} catch(UnknownHostException e){}

System.out.println(miDireccionIP); }

}

PROGRAMA 2

/*Como leer un archivo a traves d Internet*/

import java.net.*;

Page 3: Sockets

import java.io.*;

public class pruebaLeerURL { public static void main(String args[])

{ Try

{URL javasoft = null;DataInputStream dis = null;javasoft = new URL("http://www.javasoft.com");dis = new DataInputStream(javasoft.openStream());String line = dis.readLine();while (line != null) { System.out.println(line); line = dis.readLine(); }

} catch (IOException e) { System.out.println("Error" + e.getMessage()); } } }

PROGRAMA 3

/*Como obtener la direccion IP de un nombre de dominio*/

import java.net.*;

Page 4: Sockets

public class obtenerIPdeDNS { public static void main(String args[]) {

InetAddress javasoft = null;try

{ javasoft = InetAddress.getByName("www.javasoft.com"); }catch (UnknownHostException e) { System.out.println(javasoft); }System.out.println(javasoft);

}}

PROGRAMA 4

/*Como leer un archivo de texto y uno de imagen(GIF)*/

import java.applet.*;import java.net.*;import java.io.*;import java.awt.*;import java.awt.image.*;public class pruebaGetContent extends Applet { String cadena; Image imagen; public void init() {

try{ //leer un archivo de textoURL arch_texto = new URL(getDocumentBase(),"simple.texto");cadena = (String) arch_texto.getContent();//caargar un archivo graficoURL u = new URL(getDocumentBase(), "simple.gif");

Page 5: Sockets

imagen = this.createImage((ImageProducer)u.getContent());

} catch (MalformedURLException e) {

System.out.println("Error:" + e.getMessage()); } catch (IOException e) {

System.out.println("Error:" + e.getMessage()); } repaint();} public void paint(Graphics g) {

g.drawImage(imagen, 0, 0, this);g.drawString(cadena, 75,75);

}}

PROGRAMA 5

/*Como enviar correo electronico desde una aplicacion en java*/

import java.io.*;import java.net.*;public class pruebaCorreoJava{ static PrintStream ps = null; //envio de mensajes static DataInputStream dis = null; //recepción de mensajes public static void enviar(String str) throws IOException {

ps.println(str); //enviar un texto SMTPps.flush(); //descarga el textoSystem.out.println("Java envió" + str);

} public static void recibir() throws IOException

Page 6: Sockets

{String readstr = dis.readLine(); //obtener la respuesta SMTPSystem.out.println("respuesta SMTP:" + readstr);

} public static void main(String args[]) {

String HELO = "HELO";String MAIL_FROM = "MAIL_FROM:[email protected]";String RCTP_TO = "RCTP_TO: [email protected]";String DATA = "DATA"; //inicio del mensajeString ASUNTO = "Subject: Java es excelente!\n"; //Nota: "r\n.r\n" indica el final del mensajeString MENSAJE = "Java envió esto!r\n.r\n";Socket smtp = null; //Socket de SMTPtry

{ //Nota: 25 es el número de puerto SMTP predeterminado smtp = new Socket("smtp.servidor.com.mx",25); OutputStream os = smtp.getOutputStream(); ps = new PrintStream(os); InputStream is = smtp.getInputStream(); dis = new DataInputStream(is); }catch (IOException e) { System.out.println("Error al conectar:" + e); } try

{ //enviar el HELO String loc = InetAddress.getLocalHost().getHostName(); enviar(HELO + loc); recibir(); //obtener la respuesta SMTP enviar(MAIL_FROM); //enviar el remitente recibir(); //obtener la respuesta SMTP enviar(RCTP_TO); //enviar el receptor recibir(); //obtener la respuesta SMTP enviar(DATA); //enviar el inicio de mensaje recibir(); //obtener la respuesta SMTP enviar(ASUNTO); //enviar el asunto recibir(); //obtener la respuesta SMTP enviar(MENSAJE); //enviar el contenido del mensaje recibir(); //obtener la respuesta SMTP smtp.close(); //cerrar la conexión }

catch (IOException e)

Page 7: Sockets

{ System.out.println("Error al enviar: " + e); } System.out.println("Correo enviado!");

} }

PROGRAMA 6

/*Como obtener el URL de la pagina de HTML de un applet*/

import java.applet.*;import java.net.*;import java.awt.*;public class pruebaGetDocumentBase extends Applet { public void init()

{ URL html = getDocumentBase(); System.out.println(html);;}

}