この質問が何度も聞かれたことをお詫びしますが、何時間ものグーグル/実験の後、私は問題を解決していません。
サーバーからUDPソケットを介してオブジェクト(ServerPlayer)をクライアントに送信しようとしていますが、クライアントがパケットを受信すると、無効なストリームヘッダーを取得します。何がデータを変更して、破損した/存在しないヘッダーを引き起こす可能性があるのかわかりません。
送信者スレッド:
//Thread loop
@Override
public void run()
{
System.out.println("Thread " + threadCount + " running");
//Test object transmission code.
data = new byte[2048];
// -----------------------------------------------------------------------------------
// TRANSMISSION SETUP/OBJECT TRANSFER
try
{
//In case of delay
socket.setSoTimeout(1000);
//Object Transmission setup
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bStream);
oo.writeObject(p);
oo.close();
//Transmit object
data = bStream.toByteArray();
packet = new DatagramPacket(data, data.length, address, 4415);
socket.send(packet);
// -----------------------------------------------------------------------------------
// GAME LOOP
while(socket.getSoTimeout() > 0)
{
}
// -----------------------------------------------------------------------------------
// GET TO THE CHOPPA CODE BELOW
}
catch (SocketException e)
{
System.out.println("Player " + username + " timed out");
}
catch (IOException e)
{
System.out.println("Server: ");
e.printStackTrace();
}
}
クライアント/レシーバー:
public MultiplayerGameStart() throws IOException
{
address = InetAddress.getByName("127.0.0.1");
serverip = InetAddress.getByName(NetSettingsManager.ipAdd);
}
@Override
public void run()
{
//Packet byte array
data = new byte[2048];
try
{
//Send connection REQ
socket = new DatagramSocket(4415, address);
data = PacketUtil.intTobyte(NetSettingsManager.NET_FUNCTION.REQ.getFunctionCode(), data.length);
packet = new DatagramPacket(data, data.length, serverip, serverport);
socket.send(packet);
//Wait for ACK
packet = new DatagramPacket(data, data.length);
socket.receive(packet);
//Converts ACK bytes to int to be read. If ACK code is correct, continue.
if(PacketUtil.byteToint(packet.getData()) == NetSettingsManager.NET_FUNCTION.ACK.getFunctionCode())
{
//Returns username to be checked on playerlist.
System.out.println("Logging into server..."); //DEBUG output
data = NetSettingsManager.username.getBytes();
packet = new DatagramPacket(data, data.length, serverip, serverport);
socket.send(packet);
//Stub code below. Will receive player object generated by server.
packet = new DatagramPacket(data, data.length);
socket.receive(packet);
ObjectInputStream iStream = new ObjectInputStream(new ByteArrayInputStream(packet.getData(), packet.getOffset(), packet.getLength()));
//iStream.reset();
ServerPlayer messageClass = (ServerPlayer) iStream.readObject();
iStream.close();
space = new PlayState();
space.addEntity(new ClientPlayer(received));
}
}
catch (IOException e)
{
System.out.println("Client: ");
e.printStackTrace();
}
catch(ClassNotFoundException e)
{
System.out.println("Client: ");
e.printStackTrace();
}
}
ServerPlayer(注:このサンプルから関数を省略しましたが、必要に応じて提供できます)。
public class ServerPlayer extends ServerEntity implements Serializable
{
int[] material = new int[4];
Chunk pChunk;
public ServerPlayer(String username, float posX, float posY)
{
super(username, posX, posY);
//Material table.
material[0] = 25; //metal
material[1] = 33; //energy
material[2] = 43; //ion
material[3] = 131; //gas
}
}
スタックトレース:
java.io.StreamCorruptedException: invalid stream header: 4C756E69
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at client.multiplayer.MultiplayerGameStart.run(MultiplayerGameStart.java:80)
at java.lang.Thread.run(Unknown Source)
お時間をいただきありがとうございます:)。
アップデート
したがって、スレッドで送信者コードを実行すると問題が発生することがわかりました。今私のパケット出力は
server:sent 243 bytes: -84,-19,0,5 | client: received 8 bytes -84, -19, 0, 5
しかし悲しいことに私は今得ています
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readUTFBody(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readUTF(Unknown Source)
at java.io.ObjectInputStream.readUTF(Unknown Source)
at java.io.ObjectStreamClass.readNonProxy(Unknown Source)
at java.io.ObjectInputStream.readClassDescriptor(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at client.multiplayer.MultiplayerGameStart.run(MultiplayerGameStart.java:80)
at java.lang.Thread.run(Unknown Source)`