Я пытаюсь отправить объекты через сокет для игры, но они требуют много времени для отправки и могут привести к зависанию игры. Я хочу использовать BufferedOutputStreams и BufferedInputStreams для отправки данных, но мой ObjectInputStream не будет инициализироваться на стороне сервера, когда я использую BufferedOutputStream на стороне клиента. Странно то, что ошибок не было.ObjectInputStream не будет инициализироваться при использовании BufferedOutputStreams
Я только предоставляю код, потому что это займет много времени, чтобы объяснить, что происходит в противном случае. В каждую игру инициализируются два клиента.
/*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