Timeout doesn't work properly.. is there any problem? with my source.. These are relevant source for the TimeOut Thread..
///////////////////////////////////////////////////////////////
public ChatJin() {
//Find Local IP Address & Name
try {
InetAddress myInet=InetAddress.getLocalHost();
System.out.println("My InetAddress "+myInet.getHostAddress()+" name "+myInet.getHostName());
} catch(UnknownHostException e) {
System.out.println("InetAddress Error on Port "+8000+" Error code "+e);
e.printStackTrace();
}
try {
//Listening
server = new DatagramSocket(8000,InetAddress.getLocalHost());
System.out.println("Datagram Server on "+server.getLocalAddress().getHostAddress()+" port "+server.getLocalPort());
} catch(UnknownHostException e) {
System.out.println("Server Unknown Exception error"+e);
} catch(SocketException e) {
System.out.println("Server Socket Error on Port "+dport+" Error code "+e);
e.printStackTrace();
}
try {
//Sending
client = new DatagramSocket(dport,InetAddress.getLocalHost());
System.out.println("Datagram Client on "+client.getLocalAddress().getHostAddress()+" port "+client.getLocalPort());
} catch(IOException e) {
System.out.println("Client Socket Error on Port "+dport+" Error code "+e);
e.printStackTrace();
}
}
///////////////////////////////////////////////////////////
// Thread main
public static void main(String args[]) {
ChatJin m = new ChatJin();
m.go();
Thread t = new Thread(m);
t.start();
Thread t1= new TimeOut();
t1.start();
}
/////////////////////////////////////////////////////////////////
// This is Timeout thread
class TimeOut extends Thread{
private static final int TIMEOUT = 3000; // Resend timeout (milliseconds)
private static final int MAXTRIES = 5; // Maximum retransmissions
public void run(){
ChatJin a = new ChatJin();
int tries = 0;
try{
a.server.setSoTimeout(TIMEOUT);
}
catch(SocketException e){
e.printStackTrace();
}
boolean receivedResponse;
do {
receivedResponse = false;
try{
a.client.send(a.soutData);
a.server.receive(a.sinData);
if(!a.sinData.getAddress().getHostName().equals(a.server.getLocalAddress().getHostAddress()))
throw new IOException("Received packet nono");
receivedResponse = true;
}
catch(InterruptedIOException e) {
tries += 1;
System.out.println("Time out "+(MAXTRIES-tries)+" more tries...");
e.printStackTrace();
}
catch(IOException ioe)
{
System.out.println ("I/O Error occured - " + ioe);
}
} while ((!receivedResponse) && (tries < MAXTRIES));
}
}