gpt4 book ai didi

java - DatagramSocket 在 Android 上失败并显示 'Try Again'

转载 作者:行者123 更新时间:2023-11-30 04:06:11 25 4
gpt4 key购买 nike

我正在尝试在我的 Android 应用程序中发送 DatagramPackets (UDP):

//create a byte to receive data
mClientSocket = new DatagramSocket();
byte[] receiveData = new byte[MAX_RECEIVE_DATA_SIZE_BYTES];
DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length);

// Set a receive timeout
mClientSocket.setSoTimeout(timeout);

// receive the packet
mClientSocket.receive(receivePacket);

return new String(receivePacket.getData(), 0,
receivePacket.getLength());

我收到以下错误:

Try again

我是不是漏掉了什么?

最佳答案

好吧,要发送 UDP,您需要类似于:

服务器:

String  messageStr="Hello Android!";
int server_port = 12345;
DatagramSocket s = new DatagramSocket ();
InetAddress local = InetAddress .getByName("192.168.1.102");
int msg_length=messageStr.length();
byte[] message = messageStr.getBytes();
DatagramPacket p = new DatagramPacket (message, msg_length,local,server_port);
s.send(p);

客户:

String  text;
int server_port = 12345;
byte[] message = new byte[1500];
DatagramPacket p = new DatagramPacket (message, message.length);
DatagramSocket s = new DatagramSocket (server_port);
s.receive(p);
text = new String (message, 0, p.getLength());
Log.d("Udp tutorial","message:" + text);
s.close();

引用资料:

关于java - DatagramSocket 在 Android 上失败并显示 'Try Again',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11617036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com