2

如果这个问题被问了很多次,但经过数小时的谷歌搜索/实验后,我还没有解决我的问题,我深表歉意。

我正在尝试通过 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)`
4

1 回答 1

1

摆脱对reset(). 他们没有添加任何东西,我相信服务器端调用bStream.reset();正在删除创建ObjectOutputStream.

此外,当您构造 时ByteArrayInputStream,您应该使用三参数变体,从数据报包中传递getOffset()和传递。getLength()

将来,当问这样的问题时,请发布异常的实际堆栈跟踪,以及足够的代码来验证异常是否发生在您认为的位置(在这种情况下,完整的接收方法)。


编辑:似乎客户端和服务器都在同一个端口(4415)发送和接收。根据您提供的其他调试信息,我相信客户端正在获取它尝试发送到服务器的初始数据报。尝试为每个端口使用不同的端口(并使用常量,例如CLIENT_PORTandSERVER_PORT来指定它们)。

于 2013-03-18T19:06:55.477 回答