创建一种通过网络发送整数的有效方法。 TCP [英] Creating an efficient way of sending integers over a network. TCP

查看:149
本文介绍了创建一种通过网络发送整数的有效方法。 TCP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将整数值转换为字节数组,然后通过字节流将它们发送到客户端程序,该程序将字节数组转换回整数?

How can I convert integer values to byte arrays and then send them over a byte stream to the client program which converts the byte array back to an integer?

My程序是一个乒乓球游戏。一旦运行,它就会创建一个服务器,客户端使用对象流立即通过互联网连接到。一切都运作良好,但似乎效率不高。我的意思是,当球试图通过更新循环保持同步时,球来回摆动。我可能已经松散地编程了,但这是我能想到的最好的。我希望有更多了解这种事情如何运作的人可以帮我澄清一些事情。

My program is a pingpong game. Once run it creates a server which a client connects to over the internet using an object stream right now. All is working well, but it doesn't seem very efficient. By that I mean the ball is stuttering back and forth while it is trying to keep in sync via the update loop. I may have programmed it loosely, but it was the best I could come up with. I hope someone who knows a lot more about how this kind of thing works can help me clear some things up.

我的问题直截了当。我需要知道更好的方式来更有效地在互联网上发送球位置和球员位置。目前花费的时间太长了。虽然,我可能会以错误的方式更新它。

My question put straight. I need to know a better way to send the ball positions and player position over the internet more efficiently. Currently the time it takes is too long. Although, I could be updating it the wrong way.

构建流的方式:

    oostream = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
    oostream.flush();
    oistream = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));

这是玩家2的更新循环:

This is player 2's update loop:

            IntData id = new IntData();

            while (running) {

                id.ballx = ballx;
                id.bally = bally;
                id.player2Y = player2Y;
                oostream.writeObject(id);
                oostream.flush();

                Thread.sleep(updaterate);

                id = (IntData) oistream.readObject();
                player1Y = id.player1Y;
                    ballx = id.ballx;
                bally = id.bally;

            }

播放器1是服务器主机。
这是玩家1的更新循环:

Player 1 is the server host. This is player 1's update loop:

            IntData id = new IntData();

            while (running) {

                id = (IntData) oistream.readObject();
                player2Y = id.player2Y;
                ballx = id.ballx;
                bally = id.bally;

                Thread.sleep(updaterate);

                id.ballx = ballx;
                id.bally = bally;
                id.player1Y = player1Y;
                oostream.writeObject(id);
                oostream.flush();

            }


推荐答案

我建议不使用完全序列化的简单原语。改为使用 DataInputStream 等:

I suggest not using full serialization for simply primitives. use DataInputStream and the like instead:

dostream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
distream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));

然后阅读:

 ballx=distream.readInt();
 bally=distream.readInt();

并写为:

 dostream.writeInt(ballx);
 dostream.writeInt(bally);

另外我建议你不要等待双方的数据。睡在一个上,让第二个只是等待一整套数据,然后通过切断 Thread.sleep()来传输。

Also I suggest you not sleep awaiting data on both sides. Sleep on one and let the second simply await for a full set of data before transmitting by cutting out the Thread.sleep() there.

这篇关于创建一种通过网络发送整数的有效方法。 TCP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆