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

    Unhappy 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 ServerSocket( 1000, 100 );
     
             clients = new Vector();
     
             // accept connections and add ClientThreads to Vector
             while ( true ) {
                display.append( "Waiting for connection...\n" );
                numberOfClients++;
                clients.add( new ClientThread( server.accept(),
                   display, numberOfClients ) );
                ( ( ClientThread ) clients.lastElement() ).start();
     
                
             }
          }
     
          // process problems with I/O
          catch ( IOException ioException ) {
             ioException.printStackTrace();
          }
     
       } // end method runServer
     
     
       public static void main( String args[] )
       {
          Server2 application = new Server2();
          application.setDefaultCloseOperation( JFrame.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 ClientThread( Socket socket, JTextArea display, int number
     )
          {
             this.display = display;
             clientNumber = number;
             connection = socket;
     
             // obtain streams from Socket
             try {
                output = new DataOutputStream( 
                connection.getOutputStream() );
                output.flush();
                
                input = new DataInputStream( connection.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(task, 100, 30000) ;
           
             
          
     
    // 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;
             }
    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 Socket( InetAddress.getByName( "127.0.0.1" ),
     1000 );
     
             // display connection information
             displayArea.append( "Connected to: " +
                client.getInetAddress().getHostName() );
     
             // set up output stream for objects
             output = new DataOutputStream( client.getOutputStream() );
              
            
             // flush output buffer to send header information
             output.flush();
             
             
     
             // set up input stream for objects
             input = new DataInputStream( client.getInputStream() );
             
             displayArea.append( "\nGot I/O streams\n" );
     
             // enable enterField so client user can send messages
             enterField.setEnabled( true );
     
            
              // process Intmessages sent from server
             do {
     
                // read Intmessage and display it
                try {
                   
                   String message= input.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!=1 );
     
             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 sendInteger( int num )
       {
          // send object to server
          try {
             message = num;
             output.writeInt( num );
             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 main( String args[] )
       {
          final Client2 application = new Client2();
          application.runClient();
       }

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    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?

    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.

    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?
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

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

    Default

    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

Similar Threads

  1. how client know what kind of server
    By lemur in forum Networking
    Replies: 3
    Last Post: 05-31-2008, 07:11 AM
  2. how can server send messages every 5 min?
    By dim_ath in forum Networking
    Replies: 7
    Last Post: 01-10-2008, 03:59 PM
  3. Replies: 3
    Last Post: 01-07-2008, 07:01 AM
  4. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 09:18 AM
  5. Problems with client and server
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 06:07 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
  •