对象流 I/O 问题,客户端对象未更新

发布于 2024-12-23 02:49:27 字数 2192 浏览 0 评论 0原文

我正在开发一个基于客户端服务器的游戏。该游戏的主要特征是必须在客户端之间同步的网格对象。为此,我使用基于套接字的对象 I/O 流。

但是我在同步过程中遇到了问题。网格由所有客户端发送和接收,但在第一次上传到每个客户端后其状态不会修改。

我的意思是,客户端在连接时确实收到了处于当前状态的对象,但后续接收(由另一个客户端连接或先前的客户端移动发起)不会对初始发送状态进行任何修改...

这里是(已删除) -down)服务器端代码片段:

while(true) //continuously accept new connections
     {
     //wait incoming connection, and accept it   
     Socket newSocket = serverListener.accept(); 

     //create player details and save it in hashtable
     Player newPlayer = new Player(newSocket); //streams saved here
     Players.put(newPlayer); 

     //update all clients
     sendGridToAll(); 
     }

Player 类构造函数:

public Player(Socket s) throws IOException
    {
    this.Tx         = new ObjectOutputStream(socket.getOutputStream());
    this.Rx         = new DataInputStream(socket.getInputStream());
    }

SendToAll 方法:

public void sendGridToAll()
    {
    synchronized(Players) //do nothing while players HT is being modified
        {
        for(Enumeration e = Players.elements(); e.hasMoreElements(); )
            {
            Player tmpPlayer = (Player)e.nextElement();
            ObjectOutputStream tmpTx = tmpPlayer.getTx();

            try {
                tmpTx.writeObject(grid);
                }
            catch(IOException ex) 
                {
                ex.printStackTrace();
                }                
            }
        print.log("Grid update sent");
        }
    }

这是处理对象接收的客户端片段(在线程中运行):

public void run()
    {
    ObjectInputStream RX = new ObjectInputStream(socket.getInputStream());   

    while(true)
        {
        try 
            {
            RX_grid = (Grid)RX.readObject();
            }

        catch (IOException ex) 
            {
            print.log("IO Error");
            ex.printStackTrace();
            }

        catch (ClassNotFoundException ex) 
            {
            print.log("Bad grid class UID");
            }

        finally
            {
            print.log("grid recieved");
            c.updateGui(RX_grid);
            }
        }
    }

谢谢您的帮助

i'm working a client-server based game. The game is mainly caracterized by a Grid object which must be synchronized between clients. To do that I use the Object I/O Streams over Sockets.

However i encounter an issue during the synchronization process. The Grid is sent and recieved by all clients but its state is not modified after the first upload to each client.

I mean by that that clients do recieve the object in its present state when they connect but subsequent receiptions (initiated by either another client connection or previous client moves) don't present any modification over the initial sent state...

here are (stripped-down) snippets of the server side code:

while(true) //continuously accept new connections
     {
     //wait incoming connection, and accept it   
     Socket newSocket = serverListener.accept(); 

     //create player details and save it in hashtable
     Player newPlayer = new Player(newSocket); //streams saved here
     Players.put(newPlayer); 

     //update all clients
     sendGridToAll(); 
     }

The Player class constructor:

public Player(Socket s) throws IOException
    {
    this.Tx         = new ObjectOutputStream(socket.getOutputStream());
    this.Rx         = new DataInputStream(socket.getInputStream());
    }

The SendToAll method:

public void sendGridToAll()
    {
    synchronized(Players) //do nothing while players HT is being modified
        {
        for(Enumeration e = Players.elements(); e.hasMoreElements(); )
            {
            Player tmpPlayer = (Player)e.nextElement();
            ObjectOutputStream tmpTx = tmpPlayer.getTx();

            try {
                tmpTx.writeObject(grid);
                }
            catch(IOException ex) 
                {
                ex.printStackTrace();
                }                
            }
        print.log("Grid update sent");
        }
    }

and here is the client side snippet handling the object reception (ran in a thread):

public void run()
    {
    ObjectInputStream RX = new ObjectInputStream(socket.getInputStream());   

    while(true)
        {
        try 
            {
            RX_grid = (Grid)RX.readObject();
            }

        catch (IOException ex) 
            {
            print.log("IO Error");
            ex.printStackTrace();
            }

        catch (ClassNotFoundException ex) 
            {
            print.log("Bad grid class UID");
            }

        finally
            {
            print.log("grid recieved");
            c.updateGui(RX_grid);
            }
        }
    }

Thank you for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文