About multi Threads. How can i implements the timeout Threads to Chatting program?

Reply

Join Date: May 2009
Posts: 3
Reputation: lkw8888 is an unknown quantity at this point 
Solved Threads: 0
lkw8888 lkw8888 is offline Offline
Newbie Poster

About multi Threads. How can i implements the timeout Threads to Chatting program?

 
0
  #1
May 13th, 2009
Timeout doesn't work properly.. is there any problem? with my source.. These are relevant source for the TimeOut Thread..
  1. ///////////////////////////////////////////////////////////////
  2. public ChatJin() {
  3. //Find Local IP Address & Name
  4. try {
  5. InetAddress myInet=InetAddress.getLocalHost();
  6. System.out.println("My InetAddress "+myInet.getHostAddress()+" name "+myInet.getHostName());
  7. } catch(UnknownHostException e) {
  8. System.out.println("InetAddress Error on Port "+8000+" Error code "+e);
  9. e.printStackTrace();
  10. }
  11. try {
  12. //Listening
  13.  
  14. server = new DatagramSocket(8000,InetAddress.getLocalHost());
  15. System.out.println("Datagram Server on "+server.getLocalAddress().getHostAddress()+" port "+server.getLocalPort());
  16.  
  17. } catch(UnknownHostException e) {
  18. System.out.println("Server Unknown Exception error"+e);
  19. } catch(SocketException e) {
  20. System.out.println("Server Socket Error on Port "+dport+" Error code "+e);
  21. e.printStackTrace();
  22. }
  23. try {
  24. //Sending
  25. client = new DatagramSocket(dport,InetAddress.getLocalHost());
  26. System.out.println("Datagram Client on "+client.getLocalAddress().getHostAddress()+" port "+client.getLocalPort());
  27. } catch(IOException e) {
  28. System.out.println("Client Socket Error on Port "+dport+" Error code "+e);
  29. e.printStackTrace();
  30. }
  31.  
  32. }
  33.  
  34. ///////////////////////////////////////////////////////////
  35.  
  36. // Thread main
  37. public static void main(String args[]) {
  38. ChatJin m = new ChatJin();
  39. m.go();
  40. Thread t = new Thread(m);
  41. t.start();
  42. Thread t1= new TimeOut();
  43. t1.start();
  44. }
  45. /////////////////////////////////////////////////////////////////
  46.  
  47. // This is Timeout thread
  48. class TimeOut extends Thread{
  49. private static final int TIMEOUT = 3000; // Resend timeout (milliseconds)
  50. private static final int MAXTRIES = 5; // Maximum retransmissions
  51.  
  52.  
  53.  
  54.  
  55. public void run(){
  56. ChatJin a = new ChatJin();
  57. int tries = 0;
  58. try{
  59. a.server.setSoTimeout(TIMEOUT);
  60. }
  61. catch(SocketException e){
  62. e.printStackTrace();
  63. }
  64. boolean receivedResponse;
  65. do {
  66. receivedResponse = false;
  67. try{
  68. a.client.send(a.soutData);
  69. a.server.receive(a.sinData);
  70. if(!a.sinData.getAddress().getHostName().equals(a.server.getLocalAddress().getHostAddress()))
  71. throw new IOException("Received packet nono");
  72. receivedResponse = true;
  73. }
  74.  
  75. catch(InterruptedIOException e) {
  76. tries += 1;
  77. System.out.println("Time out "+(MAXTRIES-tries)+" more tries...");
  78. e.printStackTrace();
  79. }
  80. catch(IOException ioe)
  81. {
  82. System.out.println ("I/O Error occured - " + ioe);
  83. }
  84. } while ((!receivedResponse) && (tries < MAXTRIES));
  85.  
  86.  
  87. }
  88. }
Last edited by ~s.o.s~; May 14th, 2009 at 1:40 pm. Reason: Added code tags, learn to use them.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 464
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: About multi Threads. How can i implements the timeout Threads to Chatting program?

 
0
  #2
May 14th, 2009
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: About multi Threads. How can i implements the timeout Threads to Chatting program?

 
0
  #3
May 14th, 2009
Java Threads are fundamental to the whole Java API. They really do work, properly.
What's the problem with your code? You say "don't work properly" but that tells us nothing - what exactly your program do/not do that you didn't expect?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC