Results 1 to 8 of 8
- 01-10-2008, 10:40 AM #1
Member
- Join Date
- Jan 2008
- Posts
- 14
- Rep Power
- 0
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 runLast edited by dim_ath; 01-10-2008 at 11:05 AM.
- 01-10-2008, 10:56 AM #2
I could not get this.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.
Clients connects to Server to check .. ?dont worry newbie, we got you covered.
- 01-10-2008, 11:06 AM #3
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.
- 01-10-2008, 11:07 AM #4
Member
- Join Date
- Jan 2008
- Posts
- 14
- Rep Power
- 0
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)
- 01-10-2008, 11:13 AM #5
Then you can go with second option .. add two static field in your server class.
Let me know .. if i completely misunderstood your problem .. :)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 }dont worry newbie, we got you covered.
- 01-10-2008, 11:14 AM #6
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.
- 01-10-2008, 11:16 AM #7
Member
- Join Date
- Jan 2008
- Posts
- 14
- Rep Power
- 0
thank you very much!
- 01-10-2008, 03:59 PM #8
Member
- Join Date
- Jan 2008
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
Server socket - send image to client
By Hinty in forum NetworkingReplies: 2Last Post: 03-14-2009, 07:39 AM -
client/server messages exchange after 5 min
By dim_ath in forum Advanced JavaReplies: 2Last Post: 01-22-2008, 08:46 AM -
[B]Simple Client connected to server but not exchanging messages[/B]
By JavaEmpires in forum NetworkingReplies: 3Last Post: 01-07-2008, 07:01 AM -
send/read int in a client/server app
By dim_ath in forum New To JavaReplies: 2Last Post: 01-03-2008, 01:03 PM -
how to send .jar files client to server
By gobinathm in forum NetworkingReplies: 1Last Post: 12-25-2007, 04:05 AM


LinkBack URL
About LinkBacks

Bookmarks