| | |
About multi Threads. How can i implements the timeout Threads to Chatting program?
![]() |
•
•
Join Date: May 2009
Posts: 3
Reputation:
Solved Threads: 0
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..
Java Syntax (Toggle Plain Text)
/////////////////////////////////////////////////////////////// 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)); } }
Last edited by ~s.o.s~; May 14th, 2009 at 1:40 pm. Reason: Added code tags, learn to use them.
Re: About multi Threads. How can i implements the timeout Threads to Chatting program?
0
#2 May 14th, 2009
Use BB Code. Please read http://www.daniweb.com/forums/announcement9-3.html
Failure is not fatal, but failure to change might be. - John Wooden
•
•
Join Date: Apr 2008
Posts: 972
Reputation:
Solved Threads: 145
Re: About multi Threads. How can i implements the timeout Threads to Chatting program?
0
#3 May 14th, 2009
![]() |
Similar Threads
- Multi-Tasks & Multi-Threads Concepts Under Windows! Help (C++)
- Multi Thread Help (Java)
- how to create a simple chatting program in java (Java)
- Send data on a serial port (C++)
- Chatting Program - Throws IO Exception (Java)
- threads (Java)
- Multi-user program issues (VB.NET)
- PThreads: question on basic problem (or POSIX Threads) (C)
- IE won't let me access threads. Could someone email me a solution please? (Web Browsers)
Other Threads in the Java Forum
- Previous Thread: "main"
- Next Thread: How to access Two tables in Oracle using JDBC
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character chat class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp game gameprogramming givemetehcodez graphics gui html ide image integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux list loops mac map method methods mobile netbeans newbie notdisplaying number online printf problem program programming project properties qt recursion researchinmotion rotatetext rsa scanner screen server set singleton sms sort sql string swing system textfields threads time title tree tutorial-sample update variablebinding windows working xor






