IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> Android Socket UDP 点对点或者广播通讯包含发送端和接收端 -> 正文阅读

[网络协议]Android Socket UDP 点对点或者广播通讯包含发送端和接收端

Android Socket UDP 包含发送端和接收端( 两种切换方式,点对点,或者广播通讯)

不想看博客的,请直接移步下载Demo

不想看博客的,请直接移步下载Demo

不想看博客的,请直接移步下载Demo

??????AndroidSocketUDP通讯手机对手机或者手机对PC端,点对点,或者广播通讯发送端和接收端-Android文档类资源-CSDN下载

Android中接受和发送都是需要在子线程中开启的,然后在回主线程更新UI效果,所以这里使用的是ThreadManager管理工具类管理 或者自己开启线程都一样,记得在主线程更新UI就好,

 
package com.example.mydemo;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.mydemo.bean.BroadCastDataBean;
import com.example.mydemo.bean.SocketDataBean;
import com.example.mydemo.utils.CalculateUtils;
import com.example.mydemo.utils.DeviceIdUtil;
import com.example.mydemo.utils.LogUtils;
import com.example.mydemo.utils.MD5ChangeUtil;
import com.example.mydemo.utils.ThreadManager;
import com.google.gson.Gson;
import com.lzh.easythread.AsyncCallback;
import com.lzh.easythread.EasyThread;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.concurrent.Callable;

/**
 * 服务端界面
 * 这里使用EasyThread线程池管理   效率大大滴
 * 有接收线程--接收数据---固定核心线程(FixedThreadPool )
 * 有发送线程--回写数据---缓存线程(CachedThreadPool )
 */
public class ServerActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private static String CurrentIP;
    private static int BROADCAST_PORT = 7006;
    private static int SEND_PORT = 7005;
    private static int RECEIVE_PORT = 7003;
    //    private static String SEND_IP = "192.168.64.13";
    private static String SEND_IP = "255.255.255.255";
    //    private static String BROADCAST_IP = "192.168.64.13";
    private InetAddress inetAddress = null;
    private DatagramSocket mSendSocket = null;
    private DatagramSocket mReceiveSocket = null;
    private volatile boolean isRuning = true;
    private EditText mSendContent;
    private Button mSend;
    private Button mClear;
    private TextView mReceive;
    private TextView mMyIp;
    private String sendStringData;
    private Runnable mSendRunnable;
    private EasyThread easyCacheThread;
    private Runnable mReceiveRunnable;
    private EasyThread easyFixed2Thread;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main02);
        easyCacheThread = ThreadManager.getCache();
        easyFixed2Thread = ThreadManager.getIO();
        initView();
        try {
            inetAddress = InetAddress.getByName(SEND_IP);
        } catch (Exception e) {
            e.printStackTrace();
        }
        initTask();



    }


    private void initTask() {
        //接收端-异步回调任务
        startAsyncReceive();
        //普通的Runnable任务,更新UI需要在主线程

        //发送端:方式二点对点
        //easyFixed2Thread.execute(getReceiveRunnable());
        //easyCacheThread.execute(getSendRunnable(mSendContent.getText().toString().trim()));

        //发送端:方式一:发送广播
        easyCacheThread.execute(getSendBroadcastRunnable(mSendContent.getText().toString().trim()));

    }

    private void startAsyncReceive() {
        // 异步执行任务
        Callable<SocketDataBean> callable = new Callable<SocketDataBean>() {
            @Override
            public SocketDataBean call() throws Exception {
                // do something
                LogUtils.e("正在执行Runnable任务:%s" + Thread.currentThread().getName());
                byte[] receiveData = new byte[1024];
                DatagramPacket mReceivePacket = new DatagramPacket(receiveData, receiveData.length);
                try {
                    mReceiveSocket = new DatagramSocket(BROADCAST_PORT);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                while (true) {
                    if (isRuning) {
                        try {
                            LogUtils.e("======ReceiveThread=====000==");
                            mReceiveSocket.receive(mReceivePacket);
                            LogUtils.e("======ReceiveThread=====111==");
                            String rec = CalculateUtils.byteArrayToHexString(mReceivePacket.getData()).trim();
                            int dd = rec.indexOf("DD");
                            String recIp = rec.substring(0, dd + 2);
                            LogUtils.e("======ReceiveThread=====222==" + recIp);
                            if (mReceivePacket != null) {
//                                Message revMessage = Message.obtain();
//                                revMessage.what = 1;
//                                revMessage.obj = recIp;

                                SocketDataBean socketDataBean = new SocketDataBean();
                                socketDataBean.setData("" + recIp);
                                Log.i(TAG, "handleMessage: ReceiveThread receive ip" + recIp);
                                return socketDataBean;

//                                for (int i = 0; i < 500000; i++) {
//                                    int finalI = i;
//                                    runOnUiThread(new Runnable() {
//                                        @Override
//                                        public void run() {
//                                            mReceive.setText(recIp + "===" + finalI);
//                                        }
//                                    });
//                                }
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }
                }

            }
        };

        // 异步回调
        AsyncCallback<SocketDataBean> async = new AsyncCallback<SocketDataBean>() {
            @Override
            public void onSuccess(SocketDataBean user) {
                // notify success;
                LogUtils.e("======ReceiveThread=====onSuccess==" + user.getData());
                for (int i = 0; i < 500; i++) {
                    mReceive.setText(user.getData() + "===" + i);
                }
            }

            @Override
            public void onFailed(Throwable t) {
                // notify failed.
                LogUtils.e("======ReceiveThread=====onFailed==");

            }
        };

        // 启动异步任务
        easyFixed2Thread.async(callable, async);
    }

    private Runnable getReceiveRunnable() {
        mReceiveRunnable = new Runnable() {
            @Override
            public void run() {
                LogUtils.e("正在执行Runnable任务:%s" + Thread.currentThread().getName());
                byte[] receiveData = new byte[1024];
                DatagramPacket mReceivePacket = new DatagramPacket(receiveData, receiveData.length);
                try {
                    mReceiveSocket = new DatagramSocket(RECEIVE_PORT);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                while (true) {
                    if (isRuning) {
                        try {
                            LogUtils.e("======ReceiveThread=====000==");
                            mReceiveSocket.receive(mReceivePacket);
                            LogUtils.e("======ReceiveThread=====111==");
                            String rec = CalculateUtils.byteArrayToHexString(mReceivePacket.getData()).trim();
                            int dd = rec.indexOf("DD");
                            String recIp = rec.substring(0, dd + 2);
                            LogUtils.e("======ReceiveThread=====222==" + recIp);
                            if (mReceivePacket != null) {
//                                Message revMessage = Message.obtain();
//                                revMessage.what = 1;
//                                revMessage.obj = recIp;
                                for (int i = 0; i < 500000; i++) {
                                    int finalI = i;
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            mReceive.setText(recIp + "===" + finalI);
                                        }
                                    });
                                }
                                Log.i(TAG, "handleMessage: ReceiveThread receive ip" + recIp);


                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }
                }
            }
        };
        return mReceiveRunnable;

    }

    //
    private Runnable getSendBroadcastRunnable(String data) {

        mSendRunnable = new Runnable() {
            @Override
            public void run() {
                try {
                    byte[] sendData = getSendData(data);
//                    byte[] sendData = data.getBytes();
                    DatagramPacket mSendPacket = new DatagramPacket(sendData, sendData.length, inetAddress, BROADCAST_PORT);
                    for (int i = 0; i < 2; i++) {
                        mSendSocket = new DatagramSocket();
                        mSendSocket.send(mSendPacket);
                        mSendSocket.setBroadcast(true);
                        mSendSocket.close();
                    }
                } catch (Exception e) {

                }
            }
        };
        return mSendRunnable;
    }

    //    private Runnable getSendRunnable(String data) {
//
//        mSendRunnable = new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    byte[] sendData = getSendData(data);
//                    DatagramPacket mSendPacket = new DatagramPacket(sendData, sendData.length, inetAddress, SEND_PORT);
//                    for (int i = 0; i < 2; i++) {
//                        mSendSocket = new DatagramSocket();
//                        mSendSocket.send(mSendPacket);
//                        mSendSocket.close();
//                    }
//                } catch (Exception e) {
//
//                }
//            }
//        };
//        return mSendRunnable;
//    }
    private void initView() {

        mSendContent = findViewById(R.id.et_sendContent);
        mSend = findViewById(R.id.btn_sendInfo);
        mClear = findViewById(R.id.btnClear);
        mReceive = findViewById(R.id.tv_receive);
        mMyIp = findViewById(R.id.ip_info);

        //Wifi状态判断
        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if (wifiManager.isWifiEnabled()) {
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            CurrentIP = getIpString(wifiInfo.getIpAddress());
            mMyIp.append(CurrentIP);
            LogUtils.e("mMyIp====:" + CurrentIP);
        }
        mSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                easyCacheThread.execute(getSendBroadcastRunnable(mSendContent.getText().toString().trim()));
            }
        });
        mClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mReceive.setText("");
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isRuning = false;
        mReceiveSocket.close();
        LogUtils.e("UDP Server程序退出,关掉mReceiveSocket,接收消息");
        finish();
    }

    /**
     * 将获取到的int型ip转成string类型
     */
    private String getIpString(int i) {
        return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "."
                + ((i >> 16) & 0xFF) + "." + (i >> 24 & 0xFF);
    }


    private byte[] getSendData(String trim) {
        LogUtils.e("TAG==输入光源数值=trim===" + trim);
        String letterAndNumber = "1234abcdABCD56789";


//        CharMatcher.javaDigit().matches('1');
        //非空等等校验
        if ("".equals(trim)) {
            trim = "50";
        }
        int iData = Integer.parseInt(trim);
        if (iData <= 0) {
            iData = 0;
        } else if (iData >= 63) {
            iData = 63;
        }

        String inputSumLightData = CalculateUtils.numToHex8(iData);
//      aa c5 00 -00 08 00 00 01 21-- 15 --97 dd
        String str = "aac5000008000001211597dd";   //该亮度   21

        /**
         * 计算异或校验值
         * 先截取需要做校验的字符串,再计算校验值
         */
        //AA+截取数据命令+输入光源16进制    之后再做异或校验值
        LogUtils.e("TAG==输入光源数值==16进制==" + inputSumLightData);  //15
        LogUtils.e("TAG==输入光源数值==截取==" + str.substring(6, str.length() - 6));//000800000121

        String checkData = "AA" + str.substring(6, str.length() - 6);
        LogUtils.e("TAG==截取的长度=" + checkData);                            //AA000800000121
        LogUtils.e("TAG==需要计算异或的数据=" + checkData + inputSumLightData); //AA00080000012115
        String hexXORData = CalculateUtils.get16HexXORData(checkData + inputSumLightData);
        LogUtils.e("TAG==发送的异或结果=" + hexXORData);
        //AA+截取数据命令+输入光源16进制    之后再做异或校验值+DD结尾
        sendStringData = str.substring(0, str.length() - 6) + inputSumLightData + hexXORData + "dd";
        LogUtils.e("TAG==发送的结果=" + sendStringData);
        //16进制String转换成byte字节数组
        byte[] bytes = CalculateUtils.hexString2Bytes(sendStringData);
        return bytes;
    }


}

下面是上面使用到的工具类:略~~可以下载具体Demo 里面全部有,还有PC端调试工具

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2022-01-29 23:25:33  更:2022-01-29 23:26:04 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 11:26:24-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码