网上看了很多,Java的偏多,这里只介绍Android 小米8测试结果,如有不当之处,敬请谅解!
Android UDP 测试
Android手机作为监听部分
DatagramSocket 介绍:https://docs.oracle.com/javase/7/docs/api/java/net/DatagramSocket.html
public class DatagramSocket
extends Object
implements Closeable
This class represents a socket for sending and receiving datagram packets.
A datagram socket is the sending or receiving point for a packet delivery service. Each packet sent or received on a datagram socket is individually addressed and routed. Multiple packets sent from one machine to another may be routed differently, and may arrive in any order.
Where possible, a newly constructed DatagramSocket
has the SO_BROADCAST
socket option enabled so as to allow the transmission of broadcast datagrams. In order to receive broadcast packets a DatagramSocket should be bound to the wildcard address. In some implementations, broadcast packets may also be received when a DatagramSocket is bound to a more specific address.
Example: DatagramSocket s = new DatagramSocket(null); s.bind(new InetSocketAddress(8888));
Which is equivalent to: DatagramSocket s = new DatagramSocket(8888);
Both cases will create a DatagramSocket able to receive broadcasts on UDP port 8888.
具体步骤:
- Android手机与PC在同一局域网中;(这里用的360随身Wifi);
- 在手机中查看当前手机IP;
- 配置网络权限: <uses-permission android:name="android.permission.INTERNET"/>;
监听代码部分(原):
private void startListen() {
new Thread(new Runnable() {
public void run() {
try {
showUIMsg("开始监听");
DatagramSocket receviceSocket = null;
while (true)
{
receviceSocket = new DatagramSocket(4003);
byte[] bytedata = new byte[1024];
DatagramPacket recevicePackt = new DatagramPacket(bytedata, bytedata.length);
receviceSocket.receive(recevicePackt);
String receviceStr = new String(bytedata, 0, recevicePackt.getLength(), "gb2312");
showUIMsg(receviceStr);
}
} catch (Exception e) {
showUIMsg("报错了");
e.printStackTrace();
if (receviceSocket != null)
{
receviceSocket.close();
}
} finally {
if (receviceSocket != null)
{
receviceSocket.close();
showUIMsg("监听结束");
}
}
}
}).start();
}
发现问题:此段代码,在接收完第一次数据之后,突然报错,关闭了。
原因:https://blog.csdn.net/harry_helei/article/details/45220315
修改:
receviceSocket = new DatagramSocket(null);
receviceSocket.setReuseAddress(true);
receviceSocket.bind(new InetSocketAddress(4003));
byte[] bytedata = new byte[1024];
DatagramPacket recevicePackt = new DatagramPacket(bytedata, bytedata.length);
receviceSocket.receive(recevicePackt);
String receviceStr = new String(bytedata, 0, recevicePackt.getLength(), "gb2312");
发送数据部分:xx:xx:xx:xx ,修改为手机IP;
private void sendUDPData()
{
new Thread(new Runnable()
{
public void run() {
try {
DatagramSocket socket = new DatagramSocket();
byte[] sendStr = "39.6666,116.355".getBytes();
InetAddress address = InetAddress.getByName("xx.xx.xx.xx");
DatagramPacket packet = new DatagramPacket(sendStr, sendStr.length, address, 4003);
socket.send(packet);
socket.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}