Using Class Timer  in a client/server app

In my program i d like the server execute draws every 5 min choosing numbers randomly from1 to 10.
Clients, when connected will inform server about the number they chose. (This works correctly)
Next, the clients will wait until notified by the server about the number drawn in the last draw and
about if they won or not.
i ve found that i have to use classes Timer and TimerTask but API does not help me enough
about how to use them. Could anyone help me?
Here is the piece of code i think it need to be changed.
  // control thread's execution
      public void run()
         int message=0;
         int rnum=randGen.nextInt(10);
         display.append( "\n"+ "THE MAGIC NUMBER IS: " + rnum );
// process connection
         try {
            // read message from client
            do {
               try {
                  message =  input.readInt();
                  if (message==rnum){output.writeUTF("SERVER>>> YOU WON: MAGIC NUMBER IS: " +rnum);}
                   else {output.writeUTF("SERVER>>> YOU LOSE: MAGIC NUMBER IS: " +rnum);}
                  display.append( "\n\n" +"PLAYER " + clientNumber + " SELECTED NUMBER " + message );
                  display.setCaretPosition( display.getText().length() );
               // process problems reading from client
               catch ( IOException ioException ) {
                  display.append( "\nUnknown object type received" );
            } while ( message!=-1 );
            display.append( "\nClient terminated connection" );
            display = null;
         // close streams and socket
         finally {
            try {
               output.close();
               input.close();
               connection.close();
            // process problems with I/O
            catch ( IOException ioException ) {
               ioException.printStackTrace();
            clients.remove( this );
      }  // end method run
    

I just implemented something like this so I know what you are trying to do.
Basically TimerTask is an Abstract class, which means you can't make an instance of it. So TimeTask t = new TimeTask() won't work. Instead what you need to do is make your own class that extends TimerTask. You then need to implement the run() method which is abstract in TimerTask.
This sounds complicated at first, but it is very easy.
This is my DelayTimerTask:
import java.util.TimerTask;
*Author: Jon Parise
*email: [email protected]
public class DelayTimerTask extends TimerTask{
    private boolean timeExpired = false;
    /** Creates a new instance of DelayTimerTask */
    public DelayTimerTask() {
        timeExpired =false;
    public void run(){
        timeExpired =true;
    public boolean isExpired(){
        return this.timeExpired;
}All this class does is set a flag when it is run.
The Timer class schedules a TimerTask, in this case a DelayTimer. When the scheduled time comes, everything in run is executed, which in this case sets the flag.
To schedule the task do this:
Timer delayTimer = new Timer();
DelayTimerTask delayTimerTask = new DelayTimerTask();
delayTimer.schedule(delayTimerTask,5000);
while(!delayTimerTask.isExpired()){
     //DoSomething Here
}This will create a new delayTimer, and then schedule it to go off in 5 seconds(5000 millaseconds). When it goes off isExpired will become true and break you out of the loop.
Hope this helps,
Jon

Similar Messages

  • How to design socket client-server app for 2-way communication

    Hi, I am writing a client-server application. I have a single server and many clients. Each client will need the ability to send information to the server at any time. The server will also need the ability to send information to a client at any time. Its this second part that I am not sure how to design. Would I need to create a SocketServer on each client to accept incoming messages, or is there a better way? Thanks

    scranchdaddy wrote:
    Don't my requirements sound a lot like an IM application, where a chat server might need to send a message to a chat client at any time?Not really. If that is what you are designing
    in my opinion one could easily be forgiven for thinking you were deliberately obfuscating your goal...
    How does the server know where the client is? Does it know the IP address of the client?I would imagine the server would contain a directory of IPs? I'm not sure.
    What happens if the client is not running?Then I guess the message would not get delivered.
    What happens if the client is behind a firewall that does not allow incoming connections?How do IM chat clients work? How have people solved this in the past?Typically the server would only care about clients currently connected to the server.
    Maybe you should re-think your design. I don't really have a design, just requirements, that's why I'm writing this post.Your subject says "+How to *design* socket client-server app for 2-way communication+".
    Are you saying you expect someone else to do the design for you?

  • Lotus Notes Client Server Apps

    I'm wondering if anyone has tried to connect to lotus notes client server apps. from portal. I've heard that they have to be "webified" first. Is that true or are there different ways to get to the lotus notes app?

    Hi,
    Please post this on PDK forum.
    Thanks,
    Sharmila

  • Creating chart in java client/server app.

    Currently, I'm developing a client/server java app. under windows. I want to create a chart (pie, bar) for graphically reporting, anybody can help me pls........? Thanks a lot
    TriAK

    There are some solutions...You can use the Java Chart() class or BarChart() from the J2sdk API.
    (http://java.sun.com/applets/jdk/1.4/demo/applets/BarChart/example1.html)
    Besides, you can use some already build class from some companies like ObjectPlanet.(www.objectplanet.com).
    There is also another link(have a look at it) http://www.java4less.com/charts_e.htm
    Another way is to build by your own using java.awt(It will be to most difficult in the manner of time consumed).
    Finally, I will recommend you to create charts as html page integrate this application as Applet or html in general(Javascript,JSP).
    I hope I gave you some ideas.

  • OSX: multi client server app with NSNetService

    hello
    i have a small osx server app
    that uses NSNetService to manage connection and set up streams
    but it seems to stop advertising as soon as a clients connects
    how can i make it multiclient so it keeps advertising and when a new client connects uses the same streams ?
    thanks in advance
    nonnus

    superdeportivo wrote:
    Hello fellahs, as the title of this post says I'm making a client and server application. The server are supposed to handel several clients, and the communication is done true UDP. Out of interest why have you chosen UDP rather than TCP?
    I have got two questions concerning this. The first one is if a exception is throws in the server should I then send the exception back to the client in serialized form. If I choose to do this then I also would need to handel the exception classen to the client. Or should I send back a regular IOException and use it's init cause to define the exception which occured in the server. That is up to you, you can do either. I would do the first option.
    The second question is concerning the multi client support. If this is going to work then I would need to time-out the connection i the server environment, and I have not a clue about how long it should be?It should be long enough that you don't timeout a valid connection. There is no simple answer to this.

  • Help Needed With Basic Client/Server App

    I was wondering if anyone can help with a simple blackjack client/server application i've been writting (basically its a moddified chat application). The problem i'm having seems to lie within the connection management i've written.
    What i'm trying to get is that only 4 players can connect at one time and that each player takes a turn (which i've implemented using threads) to play their hand (as you would if you were playing it for real). The problem is that it will allow the players to connect, but oddly enough, it will not allow a new transaction to be created (i.e. allow a player to take their turn) until 2 players have connected.
    Even when it does create the transaction, after taking input from the client once, the server seems to stop doing anything without any error message of any kind.
    Its really annoyed me now, so you guys are my last hope!
    The code can be found in full here:
    Client Application: http://stuweb3.cmp.uea.ac.uk/~y0241725/WinEchoClient.java
    Server Application: http://stuweb3.cmp.uea.ac.uk/~y0241725/ThreadServer.java
    Card Class: http://stuweb3.cmp.uea.ac.uk/~y0241725/Card.java
    Deck Class: http://stuweb3.cmp.uea.ac.uk/~y0241725/Deck.java
    Please feel free to play around with this code as much as you feel necessary!

    (one last bump back up the forum before i give up on this completely)

  • Lion server doesn't delete open port service once added in time capsule mgmt from server app.

    I was trying to set a specific port to avoid file sharing conflict using time capsule as main router with lion server.
    I added a new public service in Server.app in time capsule section where you can manage Airport setting and open ports for mail cal vpn etc. I  assigned a port and given a name as requested. In my case a named "test"  and gave port number 5678 to try.
    Once I decided to delete this public service I noticed that anytime I make a changes in Server.app in Time capsule settings Server.app perform a refresh and the deleted servirce appear again and again with the same name and same setting in airport utility (ports management)
    The only way to delet it is to go in airport utility and delete that from there. But in the chance you want to make another change in TC managemnet from Server.app you.ll see the service deleted from everywhere magically appearing again in your airport device in my case a TC.
    Callled Apple and they said to investigate the forum.
    Now if I maje a change in Server app. TC section to add or remouve public service I have 10 usefull open ports (not enabled) in Airport ( TC)
    Any Help?
    Thx
    Jo

    I have been having this same problem, except that I cannot open basic ports through either app. I've tried opening the standard web services port on 80, but neither app (Server or Airport Utility) will open it. Apple support has been worthless.

  • Integrating DOS/client server apps through portal

    Is there any partners out their that have developed any portlets that will integrate legacy client server or DOS through the portlet in a Citrix NFuse fashion?
    There seems to be a few competitive portal providers out their who claim that this can be done. OBTree does this via Java wrapped emulation software which can be integrated over the web.
    This would be very useful for legacy integration into one place without rewriting a front end etc.
    Any ideas or recommendations?

    Any one got idea how to resolve this problem. We are facing this problem with applet based applications. Pleae let me know if any one know how to write netlet rules to make this app. work through portal.

  • Using KeyFilter Java Bean in Client Server Environment - Mr. Grover hope u r here !

    Hello,
    I will be grateful if some one can tell me how to use Keyfilter
    java bean in Forms 6.0 client server environment.
    Our requirement is to restrict entry in text field to just upper
    case characters at key punch rather than after navigating out of
    field.
    Thanx in advance.

    Are you using
    http://otn.oracle.com/sample_code/products/forms/content.html#jbp
    jc
    Since the readme file shows exactly what you have to do.
    Basically you set the implementation class for a text field to
    point to this class and make sure this class is accessable.
    Regards
    Grant Ronald
    Forms Product Management

  • Can i use my time capsule as ftp server

    Hello to everyone,
    resently I have read in the net that it is possible to set the time capsule as ftp server and also as a local server.
    Can enyone explain me how to do it pracally?
    Thank to people hwo will help in advance.

    You heard wrong.. there is no ftp server in the TC..
    It can do SMB to windows and AFP to apple.. and that my friend is that.
    If you load files onto the TC you can access them remotely only via AFP.. google search remote access time capsule.

  • Authetication problem in client/server app

    I am presently developing a client/server program, and I'm wondering what will be the best form of authentication. I plan to develop a protocol for the programs, any deviation leading to socket closure. I thought about signatures, but I don't think any body's ready to vouch for me(ie I don't know how to go about signatures). Is there any help?

    Learn about JAAS

  • Client/Server apps using custom objects

    I have wrote an ip transaction server and I am trying to pass objects across to it that don't have to reside on the server. I have created an interface that implements serializable and then the class I am trying to pass implements the interface. I read that as long as the interface is on both the client and the server that the object doesn't need to be using this approach.
    I, however, keep getting a classdefnotfound error for the custom object when I do a readObject().
    Can this really be done?
    Thanks in advance.
    Relevant code snippets:
    interface Workable implements java.io.Serializable
         public abstract int getToWork();
    public class test1 implements Workable
         public int getToWork()
              System.out.println("Test 11 value = ");
              return 1;
    Server code:
    Workable ipdata;
    ipdata = (Workable)in.readObject();//Blows up trying to read object here
    ipdata.getToWork();

    Take a look at the thread on this newsgroup named:
    deserialization loads classes more eagerly than construction?
    That seems similar to your complaint (Java's object serialization not conforming to its own specs about "classes that don't need to be present", in that thread it is a non-serializable member object that blocks things). I get the feeling you hit a bug in Java's serialization mechanism and that it is caused by a faulty heuristic in Java serialization.

  • Tns time out windows client , server on linux oracle 11g R1

    I have Windows 7 as the host machine and Oracle 11g R1 client installed on it.
    I installed VMWare V5.5 (downloaded from oracle site) and then installed Linux on it as guest OS. I have Oracle database 11g R1 intalled on Linux.
    I can ping the Linux server from Windows.
    I can also ftp to the Linux server from windows and get the files locally on Windows OS.
    The listener and tns files are configured correctly (i assume) on both Linux and Windows.
    Listener.ora on Linux
    SID_LIST_LISTENER =
    (SID_LIST =
    (SID_DESC =
    (SID_NAME =CLRExtProc)
    (ORACLE_HOME = /u01/app/oracle/product/11.1.0/db_2)
    (PROGRAM = extproc)
    (SID_DESC=
    (GLOBAL_DBNAME=orcl)
    (ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_2)
    (SID_NAME=orcl)
    SUBSCRIBE_FOR_NODE_DOWN_EVENT_LISTENER=OFF
    LISTENER =
    (DESCRIPTION_LIST =
    (DESCRIPTION =
    (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    (ADDRESS = (PROTOCOL = TCP)(HOST =192.168.159.132)(PORT = 1521)) ) )
    Tnsnames.ora on linux
    ORCL =
    (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.159.132)(PORT = 1521))
    (CONNECT_DATA =
    (SERVER = DEDICATED)
    (SERVICE_NAME = orcl)
    Sqlnet.ora on Linux
    NAMES.DIRECTORY_PATH= (TNSNAMES, EZCONNECT)
    SQLNET.INBOUND_CONNECT_TIMEOUT = 300
    SQLNET.SEND_TIMEOUT = 300
    SQLNET.RECV_TIMEOUT = 300
    Windows files
    Tnsnames.ora on Windows
    ORCL =
    (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST =192.168.159.132 )(PORT = 1521))
    (CONNECT_DATA =
    (SERVER = DEDICATED)
    (SERVICE_NAME = orcl) )
    Sqlnet.ora on Windows
    SQLNET.AUTHENTICATION_SERVICES= (NTS)
    NAMES.DIRECTORY_PATH= (TNSNAMES, EZCONNECT)
    SQLNET.INBOUND_CONNECT_TIMEOUT = 300
    SQLNET.SEND_TIMEOUT = 300
    SQLNET.RECV_TIMEOUT = 300
    The error on windows
    ping results on windows
    C:\Users\sri>ping 192.168.159.132
    Pinging 192.168.159.132 with 32 bytes of data:
    Reply from 192.168.159.132: bytes=32 time<1ms TTL=64
    Reply from 192.168.159.132: bytes=32 time<1ms TTL=64
    Reply from 192.168.159.132: bytes=32 time<1ms TTL=64
    Reply from 192.168.159.132: bytes=32 time<1ms TTL=64
    Ping statistics for 192.168.159.132:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
    Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms
    C:\Users\sri>tnsping orcl
    TNS Ping Utility for 64-bit Windows: Version 11.2.0.1.0 - Production on 23-JUL-2010 19:55:56
    Copyright (c) 1997, 2010, Oracle. All rights reserved.
    Used parameter files:
    C:\or11g\product\11.2.0\dbhome_1\network\admin\sqlnet.ora
    **Used TNSNAMES adapter to resolve the alias**
    **Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST =192.168.159.132)(PORT = 1**
    **521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = orcl)))**
    **TNS-12535: TNS:operation timed out**

    # sqlnet.ora Network Configuration File: C:\or11g\product\11.2.0\dbhome_1\network\admin\sqlnet.ora
    # Generated by Oracle configuration tools.
    # This file is actually generated by netca. But if customers choose to
    # install "Software Only", this file wont exist and without the native
    # authentication, they will not be able to connect to the database on NT.
    SQLNET.AUTHENTICATION_SERVICES= (NTS)
    NAMES.DIRECTORY_PATH= (TNSNAMES, EZCONNECT)
    SQLNET.INBOUND_CONNECT_TIMEOUT = 300
    SQLNET.SEND_TIMEOUT = 300
    SQLNET.RECV_TIMEOUT = 300

  • Making a Client & Server app with UDP

    Hello fellahs, as the title of this post says I'm making a client and server application. The server are supposed to handel several clients, and the communication is done true UDP.
    I have got two questions concerning this. The first one is if a exception is throws in the server should I then send the exception back to the client in serialized form. If I choose to do this then I also would need to handel the exception classen to the client. Or should I send back a regular IOException and use it's init cause to define the exception which occured in the server.
    The second question is concerning the multi client support. If this is going to work then I would need to time-out the connection i the server environment, and I have not a clue about how long it should be?
    /thanks in regards Ali.

    superdeportivo wrote:
    Hello fellahs, as the title of this post says I'm making a client and server application. The server are supposed to handel several clients, and the communication is done true UDP. Out of interest why have you chosen UDP rather than TCP?
    I have got two questions concerning this. The first one is if a exception is throws in the server should I then send the exception back to the client in serialized form. If I choose to do this then I also would need to handel the exception classen to the client. Or should I send back a regular IOException and use it's init cause to define the exception which occured in the server. That is up to you, you can do either. I would do the first option.
    The second question is concerning the multi client support. If this is going to work then I would need to time-out the connection i the server environment, and I have not a clue about how long it should be?It should be long enough that you don't timeout a valid connection. There is no simple answer to this.

  • Advice on client server apps

    Hi
    i would like to discuss some ways to help improve the connection between a mobile device and a remote server using sockets and http connection.
    on my N95 device and on T mobile network, i sometimes struggle to connect to the remove server and was wondering if there is a way to improve this.
    would using UDP be ideal has i have heard from someone that TCP is generely not good for wireless connections. what wil be the best approach to improve the perfiormance of wireless devices communicating with a remote server? thanks

    hi. first and foremost, both tcp & udp need internet connection irrespective of there modes of operations. tcp is connection oriented since the server has to be in constant communication with the client due to acknowledgement issues while udp does not need acknowledgement. now according to my experience the whole issue is with the cellular service provider companies; they need to make their networks efficient. in my country the internet just stops being there for some several minutes before it resumes and under such circumstances i think both tcp and udp are bound to affected.

Maybe you are looking for