基于Android系统的socket UDP协议通信

阅读: 评论:0

基于Android系统的socket UDP协议通信

基于Android系统的socket UDP协议通信

前段时间做的一个android项目需要通过socket 的UDP协议与机顶盒通信,自己在网上找了很多资料才将其解决,现在将我的解决方法做一下总结:
socket服务端:

/*** UDP服务类*/
@SuppressLint("HandlerLeak")
@SuppressWarnings("unused")
public class UdpServerSocket extends Service {private byte[] buffer = new byte[1024];private static DatagramSocket ds = null;private static DatagramSocket dsSocket = null;private DatagramPacket packet = null;;private InetSocketAddress socketAddress = null;private UdpServerSocket udpServerSocket;private int serverPort = 7001;private Context mContext;private ControlBroadcastReceiver receiver;private static Object DATA_LOCK = new Object();public UdpServerSocket() {}/*** 监听7001端口,等待接收客服端传过来的信息*/public void startUdpSocketServer() {new Thread() {public void run() {synchronized (DATA_LOCK) {try {udpServerSocket = new UdpServerSocket();while (true) {//这是一个阻塞方法,等待7001口的数据packet = receive(serverPort);String info = new Data(), Length(), "gb18030");if (info != null) {//接收到信息,向客服端发送消息response(info,packet);}}} catch (Exception e) {e.printStackTrace();}};}}.start();}/*** 构造函数,绑定主机和端口*/public UdpServerSocket(int port) {socketAddress = new InetSocketAddress(port);try {ds = new DatagramSocket(socketAddress);} catch (SocketException e) {Log.i("UdpServerSocket", "UdpServerSocket SocketException ----->"+ e);e.printStackTrace();}}/*** 接收数据包,该方法会造成线程阻塞*/public final DatagramPacket receive(int port) {InetSocketAddress address = new InetSocketAddress(port);DatagramSocket socket = null;DatagramPacket p = null;p = new DatagramPacket(buffer, buffer.length);try {socket = new DatagramSocket(address);ive(p);if (socket != null) {socket.close();}} catch (IOException e) {Log.i("receive", "receive IOException ----->" + e);e.printStackTrace();}return p;}/*** 将响应包发送给请求端*/public void response(String info, DatagramPacket pack) {DatagramSocket socket = null;DatagramPacket dp;try {socket = new DatagramSocket();dp = new Bytes("gb18030"),Bytes("gb18030").length, Address(), 7002);socket.send(dp);if (socket != null) {socket.close();}} catch (UnsupportedEncodingException e1) {Log.i("response", "responese UnsupportedEncodingException ----->"+ e1);e1.printStackTrace();} catch (IOException e) {Log.i("response", "responese IOException ----->" + e);e.printStackTrace();}/*if (packet != null) {packet = null;}*/}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {try {mContext = UdpServerSocket.this;startUdpSocketServer();} catch (Exception e) {e.printStackTrace();}StartCommand(intent, flags, startId);}@Overridepublic IBinder onBind(Intent arg0) {return null;}@Overridepublic void onCreate() {Create();}@Overridepublic void onDestroy() {Destroy();}}

socket客服端:

import java.io.IOException;
import java.DatagramPacket;
import java.DatagramSocket;
import java.InetAddress;
import java.InetSocketAddress;import android.util.Log;soft.android.h5moviebarment.Constants;public class UDPClient {/*** 默认请求参数*/private int sendPort = 7001;// 发送端口private int receivePort = 7002;// 接收端口private byte[] buffer = new byte[8*1024];public static final String ERROR_RESULT = "KONE_UDP_OBTAIN_DATA_OP_NO";/*** 发送信息到服务器并接收*/public String sendAndRec(String msg) {String result;try {result = receiveMsg(msg);} catch (Exception e) {e.printStackTrace();result = ERROR_RESULT;}return result;}void sendMsg(String msg) throws Exception {DatagramSocket dSocket = null;InetAddress local = null;local = ByName(Constants.FTP_ADRESS);dSocket = new DatagramSocket();int msg_len = 0;msg_len = msg == null ? 0 : Bytes("gb18030").length;int sPort = sendPort;DatagramPacket sendPacket = null;sendPacket = new Bytes("gb18030"), msg_len,local, sPort);dSocket.send(sendPacket);if (dSocket != null) {dSocket.close();}}String receiveMsg(String msg) throws Exception {String result = "";DatagramSocket dSocket = new DatagramSocket(null);dSocket.setReuseAddress(true);dSocket.bind(new InetSocketAddress(receivePort));dSocket.setSoTimeout(2000);DatagramPacket recPacket = new DatagramPacket(buffer, buffer.length);sendMsg(msg);try {ive(recPacket);if (Length() > 0) {result = new Data(), Length(), "gb18030");Log.i("msg", "返回正确结果:" + result);}if (dSocket != null) {dSocket.close(); }} catch (IOException e) { result = "false";e.printStackTrace();}return result;}}

在发送和接收的时候主要要注意每次请求的时候请求完了要情况pack里面的内容,否则就会出现服务端会记住客服端每次发过来的包,并每次一并发送给客户端。

本文发布于:2024-02-01 13:40:34,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170676603237000.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

下一篇:java udp 断开
标签:协议   通信   系统   Android   UDP
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23