Sockets

Páginas: 8 (1895 palabras) Publicado: 8 de noviembre de 2010
Sistemas Distribuidos

COMUNICACIÓN ENTRE PROCESOS CON SOCKETS
Sockets usando UDP
Se entrega a usted el código fuente de un ejemplo sencillo sobre comunicación entre procesos usando sockets con el protocolo UDP.

Código del lado cliente
package Cliente; import java.net.*; import java.io.*; public class UDPCliente{ public static void main(String args[]){ // Los argumentos dan el contenidodel mensaje y dirección Servidor DatagramSocket unSocket = null; try { unSocket = new DatagramSocket(); byte [] m = args[0].getBytes(); InetAddress dirServ = InetAddress.getByName(args[1]); int puertoServ = 7001; DatagramPacket peticion = new DatagramPacket(m, args[0].length(), dirServ, puertoServ); unSocket.send(peticion); byte[] buffer = new byte[1000]; DatagramPacket respuesta = newDatagramPacket(buffer, buffer.length); unSocket.receive(respuesta); System.out.println("Le devuelvo en este ejercicio de sockets: " + new String(respuesta.getData())); }catch (SocketException e){System.out.println("Socket: " + e.getMessage()); }catch (IOException e){System.out.println("IO: " + e.getMessage()); }finally {if(unSocket != null) unSocket.close();} } }

Código del lado servidor
package Servidor;import java.net.*; import java.io.*; public class UDPServidor{ public static void main(String args[]){ DatagramSocket unSocket = null; try{ unSocket = new DatagramSocket(7001); // crea un socket en el puerto acordado byte[] buffer = new byte[10000];

while(true){ DatagramPacket peticion = new DatagramPacket(buffer, buffer.length); unSocket.receive(peticion); DatagramPacket respuesta = newDatagramPacket(peticion.getData(), peticion.getLength(), peticion.getAddress(), peticion.getPort()); unSocket.send(respuesta); }} catch ( SocketException e){System.out.println("Socket: " + e.getMessage()); }catch (IOException e) {System.out.println("IO: " + e.getMessage()); }finally {if(unSocket != null) unSocket.close();} } }

Se le solicita:  Documentar el código línea a línea indicando que seinstruye a la máquina en cada una de ellas.  Compilar y ejecutar el proceso servidor en una máquina y el proceso cliente en otra máquina; logrando comunicar los dos procesos (puede usar maquinas virtuales)  El documento de informe debe mostrar el proceso seguido por usted(es) para lograr el resultado.  La frase a utilizar para que el servidor responda repitiéndola será: Esta frase va desde elcliente al servidor y regresa, es un servidor ECO. Varíe el proceso servidor de tal manera que solo acepte mensajes de tamaño veinte “bytes”; pruebe a enviar el mensaje anterior y explique lo que sucede.

Sockets usando TCP (Streams)
Se entrega a usted el código fuente de un ejemplo sencillo sobre comunicación entre procesos usando sockets con el protocolo TCP. Código del lado cliente
packageCliente; import java.net.*; import java.io.*; public class TCPCliente { public static void main (String args[]) { Socket s = null; try{ int puertoServicio = 7800; s = new Socket(args[1], puertoServicio); DataInputStream entrada = new DataInputStream( s.getInputStream()); DataOutputStream salida =new DataOutputStream( s.getOutputStream()); salida.writeUTF(args[0]); String datos = entrada.readUTF();System.out.println("El servidor hace eco de: "+ datos) ; }catch (UnknownHostException e) {System.out.println("Socket:"+e.getMessage()); }catch (EOFException e){System.out.println("EOF:"+e.getMessage()); }catch (IOException e){System.out.println("Linea leida:"+e.getMessage()); }finally {if(s!=null) try {s.close();}catch (IOExceptione) {System.out.println("cerrar:"+e.getMessage());}} } }

Código dellado servidor
package Servidor; import java.net.*; import java.io.*;

public class TCPServidor { public static void main (String args[]) { try{ int puertoServ = 7800; ServerSocket escuchaSocket = new ServerSocket(puertoServ); while(true) { Socket clienteSocket = escuchaSocket.accept(); Conexion c = new Conexion(clienteSocket); } } catch(IOException e) {System.out.println("Socket a la...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • sockets
  • sockets
  • Sockets
  • Sockets
  • Sockets
  • Sockets
  • Comunicacion entre sOCKETS
  • Sockets de windows

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS