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-21-2008, 06:25 PM
Member
 
Join Date: Jan 2008
Posts: 14
dim_ath is on a distinguished road
client/server messages exchange after 5 min
hello everyone!

in my program, clients choose a number from 1 to 10 and send it to the server.
server produces a number every 5 min and send the client the result (if they
win or lose) immediately.

i 'd like to convert this, in order the result to be sent from the server to client when the time(5min) is finished - just before a second draw occurs.

so the clients must wait to be notified by the server and disconnected when
the server sends the result.

Could anyone help me?
PHP Code:
Server piece of code

// set up and run server
   
public void runServer()
   {
      
// set up server and process connections
      
try {
 
         
// create ServerSocket
         
ServerSocket server = new ServerSocket1000100 );
 
         
clients = new Vector();
 
         
// accept connections and add ClientThreads to Vector
         
while ( true ) {
            
display.append"Waiting for connection...\n" );
            
numberOfClients++;
            
clients.add( new ClientThreadserver.accept(),
               
displaynumberOfClients ) );
            ( ( 
ClientThread clients.lastElement() ).start();
 
            
         }
      }
 
      
// process problems with I/O
      
catch ( IOException ioException ) {
         
ioException.printStackTrace();
      }
 
   } 
// end method runServer
 
 
   
public static void mainString args[] )
   {
      
Server2 application = new Server2();
      
application.setDefaultCloseOperationJFrame.EXIT_ON_CLOSE );
      
application.runServer();
   }
 
   
// private inner class ClientThread manages each Client as a thread
   
private class ClientThread extends Thread {
       private 
int clientNumber;
      private 
Socket connection;
      private 
DataOutputStream output;
      private 
DataInputStream input;
      
Random randGen=new Random(); 
      private 
JTextArea display;
      
 
      
// set up a Client thread
      
public ClientThreadSocket socketJTextArea displayint number
 
)
      {
         
this.display display;
         
clientNumber number;
         
connection socket;
 
         
// obtain streams from Socket
         
try {
            
output = new DataOutputStream
            
connection.getOutputStream() );
            
output.flush();
            
            
input = new DataInputStreamconnection.getInputStream() );
            
 
            
 
           
this.display.append"\nPLAYER " 
               
clientNumber " CONNECTED.""\nConnection received
 from: " 
+
               
connection.getInetAddress().getHostName() + "\n" );
         }
 
         
// process problems with IO
         
catch ( IOException ioException ) {
            
ioException.printStackTrace();
         }
 
      } 
// end constructor ClientThread
 
     
 
     // control thread's execution
      
public void run()
      {
         
         
int message=0;
         
Timer timer = new Timer();
         
TimerTask task = new TimerTask(){
      
           public 
void run() {            
            
rnum=randGen.nextInt(10);
        
display.append"\n""THE MAGIC NUMBER IS: " rnum );
            
        }          
      };
      
     
timer.scheduleAtFixedRate(task10030000) ;
       
         
      
 
// 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.setCaretPositiondisplay.getText().length()
 );
               }
 
               
// process problems reading from client
               
catch ( IOException ioException ) {
                  
display.append"\nUnknown object type received" );
               }
 
            } while ( 
message!=-);
 
            
display.append"\nClient terminated connection" );
            
display null;
         } 
Client piece of code

PHP Code:
 // connect to server, get streams, process connection
   
public void runClient()
   {
      
Socket client;
 
      
// connect to server, get streams, process connection
      
try {
         
displayArea.setText"Attempting connection...\n" );
 
         
// create Socket to make connection to server
         
client = new SocketInetAddress.getByName"127.0.0.1" ),
 
1000 );
 
         
// display connection information
         
displayArea.append"Connected to: " +
            
client.getInetAddress().getHostName() );
 
         
// set up output stream for objects
         
output = new DataOutputStreamclient.getOutputStream() );
          
        
         
// flush output buffer to send header information
         
output.flush();
         
         
 
         
// set up input stream for objects
         
input = new DataInputStreamclient.getInputStream() );
         
         
displayArea.append"\nGot I/O streams\n" );
 
         
// enable enterField so client user can send messages
         
enterField.setEnabledtrue );
 
        
          
// process Intmessages sent from server
         
do {
 
            
// read Intmessage and display it
            
try {
               
               
String messageinput.readUTF();
               
displayArea.append"\n" message );
     
               
displayArea.setCaretPosition
                  
displayArea.getText().length() );
            }
 
            
// catch problems reading from server
            
catch ( IOException ioException ) {
               
displayArea.append"\nUnknown object type received" );
            }
 
         } while ( 
message!=);
 
         
displayArea.append"\nClosing connection.\n" );
 
         
// close streams and socket
         
output.close();
         
         
input.close();
         
         
client.close();
 
         
displayArea.append"Connection closed." );
 
      }  
// end try
 
      // server closed connection
      
catch ( EOFException eofException ) {
         
System.err.println"Server terminated connection" );
      }
 
      
// process problems communicating with server
      
catch ( IOException ioException ) {
         
ioException.printStackTrace();
      }
 
   } 
// end method runClient
 
  
 
// send message to server
   
private void sendIntegerint num )
   {
      
// send object to server
      
try {
         
message num;
         
output.writeIntnum );
         
output.flush();
         
displayArea.append"\nCLIENT>>> Number Selected: " num );
      }
 
      
// process problems sending object
      
catch ( IOException ioException ) {
         
displayArea.append"\nError writing object" );
         
ioException.printStackTrace();
      }
   }
 
   public static 
void mainString args[] )
   {
      final 
Client2 application = new Client2();
      
application.runClient();
   } 
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-22-2008, 07:27 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 750
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Quote:
in my program, clients choose a number from 1 to 10 and send it to the server.
server produces a number every 5 min and send the client the result (if they win or lose) immediately.
Win or lose what?

Quote:
i 'd like to convert this, in order the result to be sent from the server to client when the time(5min) is finished - just before a second draw occurs.
Can you elaborate on this specification a bit? I'm having trouble understanding what exactly you're trying to achieve.

Quote:
Could anyone help me?
I'd like to be able to help you... but your code is a real mess. Also, is it possible to see the whole classes as opposed to seeing only the snippets?
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on July 13, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-22-2008, 09:46 AM
Member
 
Join Date: Jan 2008
Posts: 14
dim_ath is on a distinguished road
In the lotto program, Clients choose a number from 1 to 10 and send it to the server. Server produces a random number every 2 min and checks if the number is the same with the client choice. Then, send the result(if won or lose) to the clients.

At this point, the result is sent immediately to clients.
That i want to achieve, is: the result need to be send at the end of 2min.
So a second draw can occur.
(When clients receive the message need to disconnect)

Thanks
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
how client know what kind of server lemur Networking 3 05-31-2008 08:11 AM
how can server send messages every 5 min? dim_ath Networking 7 01-10-2008 04:59 PM
[B]Simple Client connected to server but not exchanging messages[/B] JavaEmpires Networking 3 01-07-2008 08:01 AM
Identify Client in Socket Client Server Application masadjie Networking 1 12-20-2007 10:18 AM
Problems with client and server Albert Advanced Java 2 07-02-2007 07:07 AM


All times are GMT +3. The time now is 09:20 AM.


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