Ovako dali su nam simple UDP Echo Server, koji echos (vrati) 50% poruka sto mu UDP Echo Client (koji smo isto dobili) posalje.
E sad moramo da promenimo Client tako da bude "Reliable", sto znaci da moramo nekako da ugradimo TimeOut-and-Retransmission tehnologiju putem
setSoTimeout (1000) NAPRIMER
tako da ako Client ne dobije poruku nazad od servera u roku od 1000ms (ili ti 1 sekunde) da opet posalje poruku i tako opet i opet dok ju ne dobije nazad
E sad "receive" method od DatagramSocket daje timeout exception koji ja moram da "handle" koristeci try and catch
Isto tako kad god timeout se desi moram da izadje poruka na ekranu Clienta i da mu kaze da se timeout desio i tek onda da proba ponovo da posalje poruku.
Nadam se da ste razumeli sta trebam uradit, pa se nadam da ce te mi pomoci.
Client u trenutnom stanju ako ne dobije "reply" samo stoji i nista se ne desava, zato moramo da uradimo nesto da Client zna da se timeout desio i da pokusa da posalje poruku ponovo, znaci da pokusava da ju dostavi dok ne uspe i zato se zove "Reliable".
Evo Code Servera
SERVER:
Code:
import java.io.*;
import java.net.*;
public class LossyServer
{
public static void main(String args[]) throws IOException
{
DatagramSocket ds = new DatagramSocket(10007);
while (true)
{
byte[] buf = new byte[8192];
DatagramPacket idp = new DatagramPacket(buf, buf.length);
ds.receive(idp);
if (Math.random() < 0.5)
{
DatagramPacket odp = new DatagramPacket(idp.getData(),
idp.getLength(), idp.getAddress(), idp.getPort());
ds.send(odp);
}
else
{
System.out.println("Packet dropped!");
}
}
}
}
import java.io.*;
import java.net.*;
public class LossyServer
{
public static void main(String args[]) throws IOException
{
DatagramSocket ds = new DatagramSocket(10007);
while (true)
{
byte[] buf = new byte[8192];
DatagramPacket idp = new DatagramPacket(buf, buf.length);
ds.receive(idp);
if (Math.random() < 0.5)
{
DatagramPacket odp = new DatagramPacket(idp.getData(),
idp.getLength(), idp.getAddress(), idp.getPort());
ds.send(odp);
}
else
{
System.out.println("Packet dropped!");
}
}
}
}
A evo i Klijenta
CLIENT
Code:
import java.io.*;
import java.net.*;
public class ReliableClient
{
public static void main(String args[]) throws IOException
{
DatagramSocket ds = new DatagramSocket();
String msg = new String("HELO\r\n");
DatagramPacket req = new DatagramPacket(msg.getBytes(),
msg.length(), InetAddress.getByName("127.0.0.1"), 10007);
ds.send(req);
System.out.println("Packet sent.");
byte[] buf = new byte[8192];
DatagramPacket res = new DatagramPacket(buf, buf.length);
ds.receive(res);
System.out.println("Packet received.");
}
}
import java.io.*;
import java.net.*;
public class ReliableClient
{
public static void main(String args[]) throws IOException
{
DatagramSocket ds = new DatagramSocket();
String msg = new String("HELO\r\n");
DatagramPacket req = new DatagramPacket(msg.getBytes(),
msg.length(), InetAddress.getByName("127.0.0.1"), 10007);
ds.send(req);
System.out.println("Packet sent.");
byte[] buf = new byte[8192];
DatagramPacket res = new DatagramPacket(buf, buf.length);
ds.receive(res);
System.out.println("Packet received.");
}
}
HVALA !!!