Professional Documents
Culture Documents
1
FUNDAMENTOS EN SISTEMAS DISTRIBUIDOS
COMUNICACIÓN ENTRE PROCESOS CON SOCKETS1
1. Se entrega a usted el código fuente de un ejemplo sencillo sobre comunicación entre procesos usando
sockets con el protocolo UDP.
import java.net.*;
import java.io.*;
public class UDPCliente{
public static void main(String args[]){
// Los argumentos dan el contenido del 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 = new DatagramPacket(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();}
}
}
2. 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.
1. Se entrega a usted el código fuente de un ejemplo sencillo sobre comunicación entre procesos
usando sockets con el protocolo TCP.
}
}
Se le solicita:
• Documentar el código línea a línea indicando que se instruye 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 obtener el resultado esperado de acuerdo a lo explicado por el docente
en la sesión de clase. Debe presentar, en la siguiente sesión, la ejecución lograda a su
profesor de la asignatura y contestar a las preguntas que éste le haga sobre 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 el
cliente al servidor y regresa, es un servidor ECO.
Se entrega a usted(es) el código fuente para implementar un proceso muy sencillo que usa un “socket”
servidor instanciable y procesos cliente (hilados) que pueden conectarse a dicho proceso servidor. Compile,
ejecute, documente línea a línea y efectúe el análisis correspondiente de lo que sucede.
import java.awt.*;
import java.net.*;
import java.io.*;
public hiloCliente()
{
try {
s = new Socket("127.0.0.1", 2525);
dis = new DataInputStream(s.getInputStream());
}
catch (IOException e)
{
System.out.println("Error: " + e);
}
}
System.out.println(mensaje);
}
catch (IOException e)
{
System.out.println("Error: " + e);
}
}
}
}
f.show();
try {
servidor = new ServerSocket(2525);
}
catch (IOException e)
{
System.out.println("Error: " + e);
}
while (true)
{
Socket s = null;
try {
s = servidor.accept();
}
catch (IOException e)
{
System.out.println("Error: " + e);
}
try {
PrintStream ps = new PrintStream(s.getOutputStream());
ps.println("ESTA ES UNA CONEXION ENTRE PROCESOS SOBRE LA MISMA MAQUINA USANDO SOCKETS");
ps.flush();
s.close();
}
catch (IOException e)
{
System.out.println("Error: " + e);
}
}
}
}
PARTE 4. Comunicación entre procesos en diferentes máquinas
Usted(es) deben tomar el código fuente de la PARTE 3 y adaptarlo para que el cliente y el servidor se
ejecuten en dos máquinas diferentes en una red. Compile, ejecute, documente el código línea a línea y
efectúe el análisis correspondiente sobre los cambios efectuados.
PARTE 5. Multidifusión
package Multidifusion;
import java.net.*;
import java.io.*;
public class participanteMultidifusion {
public static void main(String args[]){
//Use una dirección multidifusión válida, ejemplo: 224.0.0.7
MulticastSocket s =null;
try {
InetAddress grupo = InetAddress.getByName(args[1]);
s = new MulticastSocket(6700);
s.joinGroup(grupo);
byte [] m = args[0].getBytes();
DatagramPacket mensaje = new DatagramPacket(m, m.length,
grupo, 6700);
s.send(mensaje);
byte[] buffer = new byte[1000];
for(int i=0; i< 3;i++) {
DatagramPacket mensajeEntrada = new
DatagramPacket(buffer, buffer.length);
s.receive(mensajeEntrada);
System.out.println("Recibido el mensaje al grupo:" + new
String(mensajeEntrada.getData()));
}
s.leaveGroup(grupo);
}catch (SocketException e){System.out.println("Socket: " +
e.getMessage());
}catch (IOException e){System.out.println("IO: " + e.getMessage());
}finally {if(s != null) s.close();}
}