【问题标题】:Sending ints, receives wrong data发送整数,接收错误数据
【发布时间】:2013-07-10 20:19:56
【问题描述】:

我正在尝试将玩家的位置 (x, y) 从服务器发送到客户端。其他一切似乎都有效,但不是这两个整数。我可以发送其他整数,例如 3456,它会收到 3456。但是这些都行不通。

服务器 Java 代码:

公共无效 sendAccountInformation() 抛出 IOException { dos.write(player.id); main.log(player.id); dos.println(player.username); main.log(player.username); dos.write(player.x); main.log(player.x); dos.write(player.y); main.log(player.y); dos.write(player.mapid); main.log(player.mapid); dos.flush(); }

服务器输出:

0 sdfsdffsd 544 384 0

以上是应该发送的正确信息。

客户端 Java 代码:

公共 void loadAccount() 抛出 IOException { int id = dis.read(); main.player = new Player(id, main); main.log(id); main.player.username = dis.readLine(); main.log(main.player.username); main.player.x = dis.read(); main.log(main.player.x); main.player.y = dis.read(); main.log(main.player.y); main.player.mapid = dis.read(); main.log(main.player.mapid); }

客户端输出:

0 sdfsdffsd 63 63 0

如您所见,两个整数(544 和 384)变成了(63 和 63)。但是其他一切都正确发送和接收?

【问题讨论】:

  • 请在本站发布您的相关代码。
  • 嗯?您可以将代码粘贴到代码格式化块中,而不是链接到包含屏幕截图 URL 的粘贴箱吗?
  • 你能给我们展示一下重现这个问题的代码吗?例如没有不需要的 GUI 代码。
  • 嗯,你的意思是,你可以发送一个 int 而你需要发送两个?
  • 你如何将玩家的位置发送给客户端?

标签: java sockets


【解决方案1】:

我相信你应该尝试使用writeInt()readInt() 来写和读int

【讨论】:

  • 我会尝试并发布结果。谢谢!
  • 完美运行。感谢大家的帮助。 :) 有人告诉我如何将此问题设置为“已解决”
猜你喜欢
  • 2015-04-08
  • 1970-01-01
  • 1970-01-01
  • 2020-03-22
  • 2013-08-06
  • 1970-01-01
  • 1970-01-01
  • 2012-10-03
  • 2012-05-20
相关资源
最近更新 更多