入力/出力ストリームを使用して、クライアントとサーバーの間でオブジェクトを渡します。サーバーでオブジェクトを送受信できますが、現在は送信のみが可能なクライアントにも同じことが必要です。だから私はクライアントに ObjectInputStream を与えました。ただし、初期化するとブロックされます!周りを検索して答えを見つけましたが、解決策はありません。
助けてください!
public GameConnection(String strPort, TextArea chat)
{
this.port = Integer.parseInt(strPort);
System.out.println("GameConnection::Constructor(): Connecting on port " + port);
this.chat = chat;
connect = new Connection();
sendObject();
}
public void sendObject()
{
try
{
obj_stream.writeObject(new String("GameServer received a message!"));
}
catch(Exception e){System.out.println("GameConnection::sendObject(): " + e);}
}
protected class Connection extends Thread
{
private boolean alive = true;
public Connection()
{
try
{
socket = new Socket(host, port);
System.out.println("Connected to GAMESERVER" + host + " on port + " + port);
obj_stream = new ObjectOutputStream(socket.getOutputStream());
// Next line BLOCKS!!!
//ObjectInputStream stream = new ObjectInputStream(socket.getInputStream());
}
catch (IOException ioe)
{
System.out.println("Connection::constructor() " + ioe);
Terminate();
}
catch (NullPointerException npe)
{
System.out.println("Connection::constructor() " + npe);;
Terminate();
}
}
私はそれらを別のスレッドで使用しようとしましたが、少なくとも私にとっては同じ問題がありました:(