자바에서 ArrayList를 사용하는 데 문제가 있습니다. 그 클래스에 값을주고, arrayList에 해당 객체를 추가 한 다음 이미 사용 된 객체에 "new"라는 키워드를 사용하여 새로운 클래스 객체를 만들고 있습니다. 그것은 다른 데이터를 가지고 있지만 변경되지 않은 데이터이지만 이전에 설정되었던 데이터는 여전히 설정되어 있습니까? 여기 새로운 개체가 실제로 새로운 것은 아닙니까?
내 코드입니다private static ArrayList<PongServerThread> games = new ArrayList<PongServerThread>();
private static int players = 0;
private static PongServerThread game;
public static void main(String args[]) throws IOException, InterruptedException
{
while(true)
{
System.out.println("Start of Loop");
if(game.getPlayers() == 0)
{
System.out.println("Waiting for players");
}
if(game.getPlayers() == 2)
{
System.out.println("Game Added");
games.add(game);
games.get((games.size())-1).start();
game.setPlayers(0);
}
game = new PongServerThread();
}
}
이 코드는 잘 작동하지만 두 클라이언트가 연결 한 후 내 PongServerThread에, 메인 스레드를 시작하고 새로운 PongServerThread 객체는 그래서 2 개를 시작해야 클라이언트는 해당 스레드에 연결하지만 일어나고있는 것은,이 클라이언트 한 번 스레드 시작, 여기
game = new PongServerThread();
이 다시 생성됩니다 만, 새로운 클라이언트가 연결되면 첫 번째 인수 할 새로운 스레드 수 클라이언트에 연결하고 다른 클라이언트가 연결되면 아케 등 등 2 클라이언트를 통해, 그래서 무슨 일이 일어나고은
game = new PongServerThread();
정말 새로운 PongServerThread을하지 않는다에 대한 행이 나는이 고정은 어떻게하는 것입니까? 여기 두 클라이언트가 연결 한 콘솔 출력은 --- 다음, 내 서버 생성자
public PongServerThread() throws IOException, InterruptedException
{
//Setup
super("PongServerThread");
setupPong();
boolean listen = false;
boolean playing = false;
System.out.println(rtnInfo());
System.out.println("Server started");
//Create a serverSocket to listen on
ServerSocket serverSocket = null;
try
{
serverSocket = new ServerSocket(port);
listen = true;
System.out.println("Server was setup and will try to create a socket");
}
catch(IOException e)
{
System.err.println("Could not listen on port:" + port);
System.exit(1);
}
while(listen)
{
/*PongPlayerThread player1 = */
players[idPlayer] = new PongPlayerThread(serverSocket.accept(), idPlayer, rtnInfo());
players[idPlayer].start();
System.out.println("Client Connected with ID:" + idPlayer);
players[0].passData(rtnInfo());
idPlayer++;
if(idPlayer > 1)
{
listen = false;
playing = true;
}
}
int timer = 0;
System.out.println("Server Closed");
serverSocket.close();
}
여기
을 ----- 1 개 커넥트 라이언에 대한
Start of Loop
Waiting for players
Pong
400.0:301.0:60.0:300.0:740.0:300.0
Server started
Server was setup and will try to create a socket
Client Connected with ID:0
Checking readLine value
Client Connected with ID:1
Start of Loop
Game Added
Pong
400.0:300.0:60.0:300.0:740.0:300.0
Server started
Server was setup and will try to create a socket
Checking readLine value
Client Connected with ID:0
Checking readLine value
업데이트입니다 업데이트 ---
방금 변수 중 일부를 정적으로 선언했다는 것을 알았습니다. 그리고 이것은 아마도 오류가 발생하는 이유 일 수 있습니다. 그러나 이제는 서버를 실행하려고 할 때가되었습니다.
class Main
{
// Variable
private ArrayList<PongServerThread> games = new ArrayList<PongServerThread>();
private int players = 0;
private PongServerThread game;
public void main(String args[]) throws IOException, InterruptedException
{
//PongServerThread game = new PongServerThread();
/*if(pt.getPlayers() == 2)
{
System.out.println("Started");
pt.start();
}*/
PongServerThread game = new PongServerThread();
while(true)
{
games.add(game);
System.out.println("Game Added");
game.start();
game = new PongServerThread();
}
/*System.out.println("Pong");
PongModel model = new PongModel();
PongView view = new PongView();
new PongController(model, view);
model.addObserver(view); // Add observer to the model
view.setVisible(true); // Display Screen
model.makeActiveObject(); // Start play*/
}
}
코드
가 나는 것을 얻는 것이 왜Could not get Input/Output from server
사람이 알겠습니까
이 오류를 얻을? 필요한 경우 더 많은 코드를 붙여 넣을 수 있습니다. 스레드를 시작하기 위해
그래서 ... PongServerThread의 생성자는 어떻게 생겼습니까? –
는 pong 게임을위한 모든 것을 설정하므로 소켓을 설정하고 두 사람이 연결될 때까지 클라이언트를 청취합니다. 연결되어있을 때 청취는 false가됩니다. – Canvas
'키워드 사용 "new"를 이미 사용 된 객체에 추가합니다. 번역 부탁드립니다. – EJP