Android UDP 套接字 - 接收“垃圾”
好的,目前我正在尝试创建一个 Android 游戏,我有一个正在运行的线程,这很好。
但是我不确定如何确定传入缓冲区大小以及如何读取它。
目前这是我的代码:
if(!thisPlayer.isHost)
{
byte[] incMsg = new byte[64];
try {
socket = new DatagramSocket(port);
//socket.setSoTimeout(10);
DatagramPacket packet = new DatagramPacket(incMsg, incMsg.length);
socket.receive(packet);
Log.d("Receiving Game Message", "Received"+incMsg.toString());
} catch (UnknownHostException err) {
Log.e("", "", err);
} catch (Exception err) {
Log.e("", "", err);
}
testString = incMsg.toString();
} else {
byte[] msg = "Game On".getBytes();
try {
String compIP = "192.168.1.102";
String ip;
if(thisPlayer.ipAddress.equals(compIP))
ip = "192.168.1.107";
else
ip = compIP;
InetAddress sendingIP = InetAddress.getByName(ip);
socket = new DatagramSocket(port);
DatagramPacket p = new DatagramPacket(msg, msg.length, sendingIP, 5130);
socket.send(p);
Log.d("Sending Game Message", "Sent");
socket.close();
} catch (UnknownHostException err) {
Log.e("", "", err);
} catch (Exception err) {
Log.e("", "", err);
}
}
socket.close();
这种工作方式。主机正在发送数据。客户端正在接收数据(我已经注释掉了 sotimeout 并且线程继续,所以我知道它正在接收数据)。
我将 byte[] 转换为字符串,然后显示它。然而,显示的是“[B@448xxxx”,其中 xxxx 是一系列重复的数字/字母。
不幸的是,我已经到了沮丧的地步,现在大脑一片混乱,无法思考我哪里出了问题。
TIA
附注我什至尝试使接收字节数组与传出字节数组的大小相同,但没有任何运气:/
OK so at the moment I am trying to create an Android Game, I have a Thread running which is fine.
However I am unsure of how to determine how big the incoming buffer size, and how to read it.
Currently this is my code:
if(!thisPlayer.isHost)
{
byte[] incMsg = new byte[64];
try {
socket = new DatagramSocket(port);
//socket.setSoTimeout(10);
DatagramPacket packet = new DatagramPacket(incMsg, incMsg.length);
socket.receive(packet);
Log.d("Receiving Game Message", "Received"+incMsg.toString());
} catch (UnknownHostException err) {
Log.e("", "", err);
} catch (Exception err) {
Log.e("", "", err);
}
testString = incMsg.toString();
} else {
byte[] msg = "Game On".getBytes();
try {
String compIP = "192.168.1.102";
String ip;
if(thisPlayer.ipAddress.equals(compIP))
ip = "192.168.1.107";
else
ip = compIP;
InetAddress sendingIP = InetAddress.getByName(ip);
socket = new DatagramSocket(port);
DatagramPacket p = new DatagramPacket(msg, msg.length, sendingIP, 5130);
socket.send(p);
Log.d("Sending Game Message", "Sent");
socket.close();
} catch (UnknownHostException err) {
Log.e("", "", err);
} catch (Exception err) {
Log.e("", "", err);
}
}
socket.close();
This kind of works. The Host is sending data. The Client is receiving data (I have commented out the sotimeout and the thread continues, so I know its receving data).
I convert the byte[] to a string and then display it. However what is displaying is "[B@448xxxx" where xxxx is a series of repeating numbers/letters.
Unfortunately I am up to the point where I am getting frustrated and now clouded brain, and cannot think for the life of me where I have gone wrong.
TIA
p.s. I have even tried making the receiving byte array the same size as the outgoing, without any luck :/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,这意味着指向字节数组的指针。您不能只将其分配给字符串,而是使用
(new String(inMsg))
。另外,由于您没有初始化数组,因此在收到数据后您将收到垃圾(如果比数组短)。
As far as I know, this means a pointer to byte array. You can't just assign it to string, instead, use
(new String(inMsg))
.Also, since you didn't initialize the array, you'll receive garbage after your received data (if shorter than the array).