すべてのスレッドは待機することができますが、1スレッドのみ(最後のスレッド)に通知します。すべてのスレッドに通知するには?notifyAll()not working、notifyAllどのようにソケットプログラミングのすべてのスレッド
public class Server {
static Socket clientSocket;
static int count = 0 ;
static boolean listeningSocket = true;
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(2343);
} catch (IOException e) {
System.err.println("Could not listen on port: 2343");
}
while(listeningSocket){
clientSocket = serverSocket.accept();
count++ ;
Serverrun myThread[] = new Serverrun[3];
myThread[count-1] = new Serverrun(clientSocket);
myThread[count-1].start();
if(count>=3){
listeningSocket = false;
}
}
serverSocket.close();
}
}
public class Serverrun extends Thread{
Socket clientSocket;
public Serverrun(Socket clientSocket) {
this.clientSocket = clientSocket;
}
public void run(){
System.out.println("abc");
String clientSentence;
String cap_Sentence;
String rd1,rd2;
try {
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
DataOutputStream outToClient2 = new DataOutputStream(clientSocket.getOutputStream());
outToClient2.writeBytes("User Login:"+'\n');
clientSentence = inFromClient.readLine();
System.out.println(" " +clientSentence+ " : login");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("error");
}
Server s = new Server();
receivebj(s.listeningSocket);
Game g = new Game();
rd1=g.randomNum();
rd2=g.randomNum();
DataOutputStream outToClient;
try {
outToClient = new DataOutputStream(clientSocket.getOutputStream());
outToClient.writeBytes(rd1+" "+rd2+'\n');
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
synchronized void receivebj(boolean listeningSocket){
if(listeningSocket!=false){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("work receivebj");
notifyAll();
}
}
私は3クライアント 結果CLIENT1を持って終了していないクライアント2:
ユーザーログイン:アリ
ユーザーログイン:鳥を
しかし、クライアント3終了:
ユーザーログイン:猫カードの
数:9 Q
追加 クラスのゲーム
public class Game {
String randomNum() {
// TODO Auto-generated method stub
String rd1,rd2;
String[] names;
names = new String[12];
names[0] = "A";
names[1] = "2";
names[2] = "3";
names[3] = "4";
names[4] = "5";
names[5] = "6";
names[6] = "7";
names[7] = "8";
names[8] = "9";
names[9] = "J";
names[10] = "Q";
names[11] = "K";
int num = (int) (Math.random()*12);
//System.out.println("Number:"+names[num]);
return names[num];
}
}
クラスクライアント
class Client {
public static void main(String argv[]) throws Exception {
String sentence;
String modifiedSentence1,modifiedSentence2;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("localhost",2343);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
modifiedSentence1 = inFromServer.readLine();
System.out.println(modifiedSentence1);
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence2 = inFromServer.readLine();
System.out.println("Number of card:"+ modifiedSentence2);
clientSocket.close();
}
}
結果サーバー:
abc
ant : login
abc
bird : login
abc
cat : login
work receivebj method
このようなコードを書かないでください。例外が発生したときは、例外をログに記録します。以前の 'try'ブロック内のコードの成功に依存するコードは、同じ' try'ブロックの中になければなりません。 – EJP