Results 1 to 8 of 8
  1. #1
    dim_ath is offline Member
    Join Date
    Jan 2008
    Posts
    14
    Rep Power
    0

    Default how can server send messages every 5 min?

    hello everyone!

    i have implemented a simple lotto system where clients send the number they chose,
    and server answers if they win or lose. However i want the server to produce a magic number
    every 5 minutes and sends the result to the CLIENTS when the time is over.
    Can you help me please? what method need i use?

    this is the piece of code:

    // 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
    Last edited by dim_ath; 01-10-2008 at 11:05 AM.

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    However i want the server to produce a magic number
    every 5 minutes and sends the result to the server when the time is over.
    I could not get this.

    Clients connects to Server to check .. ?
    dont worry newbie, we got you covered.

  3. #3
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    If Server is pushing the lotto number connecting to each client then you can use Java Timer Timer (Java 2 Platform SE v1.4.2).

    If Clients connect to the server and check their number then you can have some static fields for 1. Timestamp of last generation , 2. The generated number and check timestamp for 5 minutes against the current time to decide whether generate new or not...
    dont worry newbie, we got you covered.

  4. #4
    dim_ath is offline Member
    Join Date
    Jan 2008
    Posts
    14
    Rep Power
    0

    Default

    sorry, i corrected the mistake. clients connect and send to the server the number they chose. server replies if the number is that it produced (if the client won)

  5. #5
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Then you can go with second option .. add two static field in your server class.
    Java Code:
    public static long timestamp ;
    public static int lottoNumber ;
    
    // You can pass or make these fields public..
    ....
    if(System.currentTimeMillis() + (convert 5 min to milliseconds) > timestamp ){
      //generate new lotto number and update timestamp ..
    }else{
     // Just check lottoNumber with client's input
    }
    Let me know .. if i completely misunderstood your problem .. :)
    dont worry newbie, we got you covered.

  6. #6
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Oops !!
    Java Code:
    ....
    if(System.currentTimeMillis() + (convert 5 min to milliseconds) > timestamp ){
      //generate new lotto number and update timestamp ..
    
    Check the client input with generated number as well ..
    dont worry newbie, we got you covered.

  7. #7
    dim_ath is offline Member
    Join Date
    Jan 2008
    Posts
    14
    Rep Power
    0

    Default

    thank you very much!

  8. #8
    dim_ath is offline Member
    Join Date
    Jan 2008
    Posts
    14
    Rep Power
    0

Similar Threads

  1. Server socket - send image to client
    By Hinty in forum Networking
    Replies: 2
    Last Post: 03-14-2009, 07:39 AM
  2. client/server messages exchange after 5 min
    By dim_ath in forum Advanced Java
    Replies: 2
    Last Post: 01-22-2008, 08:46 AM
  3. Replies: 3
    Last Post: 01-07-2008, 07:01 AM
  4. send/read int in a client/server app
    By dim_ath in forum New To Java
    Replies: 2
    Last Post: 01-03-2008, 01:03 PM
  5. how to send .jar files client to server
    By gobinathm in forum Networking
    Replies: 1
    Last Post: 12-25-2007, 04:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •