Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-10-2008, 11:40 AM
Member
 
Join Date: Jan 2008
Posts: 14
dim_ath is on a distinguished road
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 12:05 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-10-2008, 11:56 AM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-10-2008, 12:06 PM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-10-2008, 12:07 PM
Member
 
Join Date: Jan 2008
Posts: 14
dim_ath is on a distinguished road
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)
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-10-2008, 12:13 PM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
Then you can go with second option .. add two static field in your server class.
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-10-2008, 12:14 PM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
Oops !!
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-10-2008, 12:16 PM
Member
 
Join Date: Jan 2008
Posts: 14
dim_ath is on a distinguished road
thank you very much!
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 01-10-2008, 04:59 PM
Member
 
Join Date: Jan 2008
Posts: 14
dim_ath is on a distinguished road
could you explain me more the Timestamp?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
client/server messages exchange after 5 min dim_ath Advanced Java 2 01-22-2008 09:46 AM
[B]Simple Client connected to server but not exchanging messages[/B] JavaEmpires Networking 3 01-07-2008 08:01 AM
send/read int in a client/server app dim_ath New To Java 2 01-03-2008 02:03 PM
how to send .jar files client to server gobinathm Networking 1 12-25-2007 05:05 AM
Server socket - send image to client Hinty Networking 0 11-30-2007 09:15 PM


All times are GMT +3. The time now is 02:24 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org