gpt4 book ai didi

java - 多线程服务器,循环只工作一次

转载 作者:行者123 更新时间:2023-11-30 09:06:56 26 4
gpt4 key购买 nike

我的桥牌拍卖多线程服务器出现问题。它的主题不太重要,到目前为止我需要做的就是让 run 方法内的循环工作不止一“圈”。我的意思是我的循环只为每个客户端工作一次然后它停止了,但我无法解决这个问题。它应该一直工作,并且在玩家 N-> E-> S-> W-> 的顺序之后它从玩家 N 开始另一圈,但现在它只是静止不动......

检查我的代码:

package serwer;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.logging.*;

public class Serwer {

public static void main(String[] args) throws Exception {
System.out.println("Started server to the bridge auction");
int conCount = 0;
ServerSocket serwer = new ServerSocket(9898);
ArrayList<connection> connections = new ArrayList<connection>(){};

try {
//only 4 players are allowed to play bridge in one table
while (connections.size() < 4) {
connection p = new connection(serwer.accept(), conCount++);
connections.add(p);
connections.get(conCount-1).start();
}
} finally {
serwer.close();
}
}

/**
* static class responsible for the connection in multithreaded server
*/
static class connection extends Thread {

private Socket socket;
private int conCount;
private static int counter = 0;
private final String[] Players;
private String stringOnServer = "0";
private int stake = 1;

public connection(Socket socket, int conCount) {
this.Players = new String[]{"N", "E", "S", "W"};
this.socket = socket;
this.conCount = conCount;
System.out.println("New connection id: " + Players[conCount]);
}

public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

//here is info what player are you
out.println("You are player on position " + Players[conCount]);

while (true) {
synchronized (this) {
if (counter % 4 == conCount) {
while (true) {
out.println("Your turn " + Players[conCount] + ", please input the text: ");
String input = in.readLine();
System.out.println("\t" + Players[conCount] + " : " + input);
counter += 1;
//for now only the stringOnServer is simply echo
stringOnServer = input;

System.out.println("\tCurrent string on server = " + stringOnServer);
try {
this.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Serwer.class.getName()).log(Level.SEVERE, null, ex);
}
break;
}
} else {
this.notify();
}
}

}

} catch (IOException e) {
System.out.println("error with player: " + Players[conCount] + ": " + e);
} finally {
try {
socket.close();
} catch (IOException e) {
System.out.println("can't closed");
}
System.out.println("connection with player" + Players[conCount] + " terminated");
}
}

}
}

客户端代码非常简单,但如果有人有时间和耐心测试它,我将其添加到:

package klient;

import java.net.*;
import java.io.*;


public class Klient {

public static void main(String[] args) throws IOException {
try {
Socket s = new Socket("127.0.0.1", 9898);
String answer;
System.out.println("Welcome on the server of auction.");

//Here is displayed info from server what player are you
BufferedReader fromServer = new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println(fromServer.readLine());

while (true) {
System.out.println(fromServer.readLine());
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
answer = input.readLine();
out.println(answer);
}
} catch (ConnectException ex) {
System.out.println("There are 4 players on server or server is closed, try again later");
}
}
}

问题出在与计数器连接类的这个循环中。预先感谢您的帮助:)

最佳答案

您必须从另一个线程调用 notify()。您当前的线程正在等待,无法通知自己。

关于java - 多线程服务器,循环只工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24241960/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com