Android UDP DatagramSocket 发送接收数据

网上看了很多,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.

 

具体步骤:

  1. Android手机与PC在同一局域网中;(这里用的360随身Wifi);
  2. 在手机中查看当前手机IP;
  3. 配置网络权限: <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();
}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值