View Single Post
  #1 (permalink)  
Old 01-21-2008, 07:25 PM
dim_ath dim_ath is offline
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();
   } 
Reply With Quote
Sponsored Links