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?
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
// 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();
}