import java.net.*;
//import java.awt.Dimension;
import java.io.*;
//import javax.swing.JLabel;
public class ServerThreadedRandomNumbers extends Thread {//Thread Technology!
String randomNumbers ;
InputStreamReader in = null;
BufferedReader buf = null;
PrintWriter out = null;
Socket theConnection;
int Num = 25;//5025;//25; // Largest random number issued by server
boolean isMatrixSent = false;
boolean isGameStarted = false;
int [][]myMatrixGeneratedRandomNumbers=new int [5][5];
//constructor: used to pass new connection (thread)
public ServerThreadedRandomNumbers(Socket s , int ClientNo, int[][] MatrixGeneratedRandomNumbers) {
theConnection = s;
myMatrixGeneratedRandomNumbers=MatrixGeneratedRandomNumbers;
}
public void setGameStarted(boolean started) {
isGameStarted = started;
}
public void run() { // implements the processes of the thread
try {
// the buffer used for writing/reading strings from/to the socket
out = new PrintWriter(theConnection.getOutputStream(), true);
buf = new BufferedReader(new InputStreamReader(theConnection. getInputStream()));
if (isMatrixSent==false){
while (true) {
String as = buf.readLine(); // read client's requests
if (as==null) break;
// Issue random numbers to client
if (as.equals("2")){
for (int i=0;i<5;i++) {
for (int j=0;j<5;j++){
//myMatrixGeneratedRandomNumbers[i][j]=(int)(Math.random()*NUM);
out.println((int)myMatrixGeneratedRandomNumbers[i][j]);
}
}
out.println(randomNumbers+" "); // Send random number to client
}
}
isMatrixSent=true;
// isGameStarted=true;
}
if (isGameStarted==true){
out.println("GO");
while (true) {
String as = buf.readLine(); // read client's requests
if (as==null) break;
// Issue random numbers to client
if (as.equals("1")){
int a=(int)(Math.random() * (Num));
randomNumbers=String.valueOf(a);
//randomNumbers=randomNumbers.concat(" ");
// System.out.print("Pack send to client: "+theConnection.getInetAddress().toString()
// +":"+theConnection.getPort() );
// System.out.println("// randomNumbers issued: "+randomNumbers);
out.println(randomNumbers+" "); // Send random number to client
}
}
System.out.println("Finished client:" + theConnection.getInetAddress().toString()+":"+theConnection.getPort());
}
// text.append("OK MPHKE");
//StatusBar2.setMessage("Finished client:" + theConnection.getInetAddress().toString()+":"+theConnection.getPort());
//}
}
catch (IOException ex) {
}
}
}