게임용 소켓을 통해 객체를 보내려고하지만 전송하는 데 오랜 시간이 걸리고 게임이 중단 될 수 있습니다. BufferedOutputStreams 및 BufferedInputStreams를 사용하여 데이터를 보내려고하지만 클라이언트 측에서 BufferedOutputStream을 사용할 때 내 ObjectInputStream이 서버 측에서 초기화되지 않습니다. 이상한 점은 오류가 발생하지 않는다는 것입니다.BufferedOutputStreams를 사용할 때 ObjectInputStream이 초기화되지 않습니다.
코드가 관련된 부분 만 설명하고 있습니다. 다른 부분에 대해 설명하는 데 오랜 시간이 걸리기 때문입니다. 두 명의 클라이언트가 각 게임을 초기화합니다.
/*Server Code*/
ObjectOutputStream toClients;//stream to both players
ObjectInputStream fromClients;//stream from both players
Socket client1;//player one socket
Socket client2;//player two socket
public RunGame(Socket client1, Socket client2)throws IOException//constructor of a new thread
{
this.client1=client1;
this.client2=client2;
}
public void run()//for the thread
{
try{
this.createGame();
/*
rest of code for server when running game
*/
}
catch(IOException e){e.printStackTrace();}
catch(ClassNotFoundException e){e.printStackTrace();}
}
public void createGame()
{
try{
System.out.println("about to create");//this prints out
fromClients=new ObjectInputStream(client1.getInputStream());//first initialization
System.out.println("created");//this doesn't
String s1=(String)fromClients.readObject();
fromClients=new ObjectInputStream(client2.getInputStream());//sets input to player 2
String s2=(String)fromClients.readObject();
}
catch(IOException e){e.printStackTrace();}
catch(ClassNotFoundException e){e.printStackTrace();}
}
/*Client Code*/
Socket sock;//created in the constructor of the thread
ObjectOutputStream toServer;
ObjectInputStream fromServer;
public void run()
{
try{
System.out.println("about to create");//this prints
toServer=new ObjectOutputStream(new BufferedOutputStream(sock.getOutputStream(),8*1024));//bufferedoutputstream is here
toServer.writeObject("String that is to be sent to server");
System.out.println("written");//this also prints
}
catch(IOException e){e.printStackTrace();}
catch(ClassNotFoundException e){e.printStackTrace();}
/*
rest of client code
*/
}
나는 모든 포럼을 훑어 보았지만 제대로 작동하는 것을 찾을 수 없었습니다. 그래서 나는 아주 초보적인 것을하고 있다고 생각합니다. 당신이 줄 수있는 도움을 주셔서 감사합니다!
초심자를 고쳐 주셔서 감사합니다. 너 멋진데 :) – Indeed