ClubEnsayos.com - Ensayos de Calidad, Tareas y Monografias
Buscar

Reporte de Analisis Protocolo UDP


Enviado por   •  30 de Enero de 2017  •  Prácticas o problemas  •  3.099 Palabras (13 Páginas)  •  190 Visitas

Página 1 de 13

[pic 1]

Sistemas Distribuidos

Enero - Mayo 2017

Practica de Programación 1

Reporte de protocolo UDP

Jatar Atala Moreno Lacalle

  • UDPClient

package udpclient;

import java.net.*;

import java.io.*;

/**

 *

 * @author jatar_000

 */

public class UDPClient {

    public static void main(String args[]) {

// args = mensaje y nombre del servidor

        DatagramSocket aSocket = null;// Se crea una conexion de tipo DatagramPacket para inicar la comunicacion

        try {

            aSocket = new DatagramSocket();//Se inicia la conexion

            byte[] m = args[0].getBytes();//una veriable m para el tamaño en bytes del mensaje

            InetAddress aHost = InetAddress.getByName(args[1]);//Recibe la direccion en aHost

            int serverPort = 6789;//el puerto de salida es 6789

            DatagramPacket request = new DatagramPacket(m, args[0].length(), aHost, serverPort);

            //empaqueta los datos anteriores en un datagrama

            aSocket.send(request);//envia al aSocket la peticion

            byte[] buffer = new byte[1000];//crea un buffer para poner ahi el mensaje

            DatagramPacket reply = new DatagramPacket(buffer, buffer.length);//datagrama de respuesta

            aSocket.receive(reply);//recibe la respuesta

            System.out.println("Reply: " + new String(reply.getData()));//imprime la respuesta

        } catch (SocketException e) {

            System.out.println("Socket: " + e.getMessage());//excepcion de socket

        } catch (IOException e) {

            System.out.println("IOS: " + e.getMessage());

        }//excepcion de entrada/salida

    }

}

  • UDPServer

package udpserver;

import java.net.*;

import java.io.*;

/**

 *

 * @author jatar_000

 */

public class UDPServer {

    public static void main(String args[]) {

        DatagramSocket aSocket = null;// Se crea una conexion de tipo DatagramPacket para inicar la comunicacion

        try {

            aSocket = new DatagramSocket(6789);//recibe el mensaje por el puerto 6789

            byte[] buffer = new byte[1000];//buffer para recepcion y respuesta

            while (true) {//eschucha mientras llegan peticiones

                //variable de peticion de tipo DatagramPacket

                DatagramPacket request = new DatagramPacket(buffer, buffer.length);

                aSocket.receive(request);//recibe mensaje

                DatagramPacket reply = new DatagramPacket(request.getData(),//crea la la respuesta del resvidor

                        request.getLength(), request.getAddress(), request.getPort());//obtiene datos de la peticion: tamaño, direccion, puerto

                aSocket.send(reply);//envia respuesta

            }

        } catch (SocketException e) {

            System.out.println("Socket: " + e.getMessage());

        } catch (IOException e) {

            System.out.println("IO: " + e.getMessage());

        }

    }

}

  • UDPDiscardClient

import java.net.*;

import java.io.*;

public class UDPDiscardServer {

    public final static int port = 9;//declara variable int DiscartPort

    public static void main(String[] args) {

        String hostname;//variable para el nombre del host

        if (args.length > 0) {

            hostname = args[0];//pasa el nombre del host a hostname

        } else {

            hostname = "localhost";//Si no se recibe el nombre del hosto, le pondra localhost

        }

        try {

            String theLine;//Variable linea

            DatagramPacket theOutput;//Datagrama de salida

            InetAddress server = InetAddress.getByName(hostname);//direccion del servidor

            BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));//toma lo que se ingresa por consola y lo coloca en el buffer

            DatagramSocket theSocket = new DatagramSocket();//Datagrama de conexion

            while (true) {//siempre escucha

                theLine = userInput.readLine();//obtiene el valor escrito en consola

                if (theLine.equals(".")) {

                    break;

                }

                termina el mensaje al recibir un .

byte[] data = new byte[theLine.length()];//obtiene el tamaño del msj

                data = theLine.getBytes();//;envia el msj

                theOutput = new DatagramPacket(data, data.length, server, port);

                theSocket.send(theOutput);//Envia el datagrama

            } // end while

        } // end try

        catch (UnknownHostException e) {

            System.err.println(e);

        } catch (SocketException se) {

            System.err.println(se);

        } catch (IOException e) {

            System.err.println(e);

        }

    } // end main

}

  • UDPDiscradServer

import java.net.*;

import java.io.*;

public class UDPDiscardServer {

    public final static int discardPort = 9;//declara variable int DiscartPort

    static byte[] buffer = new byte[65507];//crea un buffer

    public static void main(String[] args) {

...

Descargar como (para miembros actualizados)  txt (11 Kb)   pdf (188.7 Kb)   docx (767.5 Kb)  
Leer 12 páginas más »
Disponible sólo en Clubensayos.com