Reporte de Analisis Protocolo UDP
Enviado por klimbo3445 • 23 de Mayo de 2018 • 840 Palabras (4 Páginas) • 312 Visitas
...
int port;//un entero llamado puerto
try {
port = Integer.parseInt(args[0]);//parsea el puerto a un entero
} catch (Exception e) {
port = discardPort;//se le asigna al puerto el DiscartPort
}
try {
DatagramSocket ds = new DatagramSocket(port);//nuevo Datagrama ds
while (true) {//escucha todo el tiempo
try {
//Se envia al DatagramPacket lo que esta en el buffer
DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
ds.receive(dp);//pone el mensaje en el Datagrama ds
String s = new String(dp.getData(), 0, dp.getLength());//Recupera el msj que mando con el tamaño con la variable s
System.out.println(dp.getAddress() + " at port " + dp.getPort() + " says " + s);//Imprime el msj que envio con la direccion y puerto
} catch (IOException e) {
System.err.println(e);
}
} // end while
} // end try
catch (SocketException se) {
System.err.println(se);
} // end catch
} // end main
}
Conversor °C a °F
- Cliente
package updclient;
/**
*
* @author jatar_000
*/
import java.net.*;
import java.io.*;
public class UPDClient_Conversor {
/**
* @param args the command line arguments
* @throws java.lang.Exception
*/
public static void main(String args[]) throws Exception {
byte[] sendData;
byte[] receiveData;
BufferedReader inFromKeyboard = new BufferedReader(new InputStreamReader(System.in));
try {
DatagramSocket clientSocket = new DatagramSocket(8888);
InetAddress IPAddress = InetAddress.getByName("127.0.0.1");
while (true) {
/**
* *******************************
* create a new buffer each time
*/
sendData = new byte[1024];
receiveData = new byte[1024];
/**
* ******************************
*/
System.out.println("Introducir TEMP y el valor de la temperatura en Celsius[-200,200]: ");
String sentence = inFromKeyboard.readLine();
//sending TEMP command
if (sentence.startsWith("TEMP")) {
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9999);
clientSocket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
System.out.println(
"Temperatura enviada al servidor : " + sentence + "°C" + "\n Esperando al servidor....");
clientSocket.receive(receivePacket);
String fromServer = new String(receivePacket.getData());
System.out.println("Respuesta del servidor : " + fromServer + "°F");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
- Servidor
package udpserver;
/**
*
* @author jatar_000
*/
import java.net.*;
import java.io.*;
class UDPServer_Conversor{
public static void main(String arg[]) throws Exception {
DatagramSocket serversocket = new DatagramSocket(9999);
UDPServer udpserver = new UDPServer();
byte[] receivedBuffer; // = new byte[1024];
byte[] sentBuffer; //= new byte[1024];
while (true) {
receivedBuffer = new byte[1024];
sentBuffer = new byte[1024];
...