android:Udp广播单播组播的实现

android:Udp广播单播组播的实现,第1张

udp,单播组播广播的讲解,很细致,可以学习一下

单播,组播(多播),广播以及任播单播(unicast): 是指封包在计算机网络的传输中,目的地址为单一目标的一种传输方式。它是现今网络应用最为广泛,通常所使用的网络协议或服务大多采用单播传输,例如一切基于TCP的协议。组播(multicast): 也叫多播, 多点广播或群播。 指把信息同时传递给一组目的地址。它使用策略是最高效的,因为消息在每条网络链路上只需传递一次,而且只有在链路分叉的时候,消息才会被复制。广播(broadcahttps://colobu.com/2014/10/21/udp-and-unicast-multicast-broadcast-anycast/

package cn.titansys.xview_pptv;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.yunze.udp.jh601a_message;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class Main3Activity extends AppCompatActivity {
    //组播地址组播端口
    private static String MULTICAST_IP = "238.1.238.1";
    private static int MULTICAST_PORT = 5001;
    //广播地址,广播端口
    private static String BROADCAST_IP = "255.255.255.255";
    private static int BROADCAST_PORT = 8887;
    //单播端口,单播地址
    private String serverHost = "192.168.200.211";
    private static int UNICAST_PORT = 8888;

    private boolean isRuning = true;
    String mip = "238.1.238.1";
    int mport = 5001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        onBroadcastReceive();
        onUnicastReceive();
        onMulticastSendReceive();
    }


    /**
         * 广播发送
         */
    public void onBroadcastSend(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    InetAddress inetAddress = InetAddress.getByName(BROADCAST_IP);
                    DatagramSocket datagramSocketSend = new DatagramSocket();

                    byte[] data = jh601a_message.message(mip,mport,"01","0000","8899aabb","ccdd");
                    DatagramPacket datagramPacket = new DatagramPacket(data, data.length, inetAddress, BROADCAST_PORT);
                    datagramSocketSend.send(datagramPacket);
// 发送设置为广播
                    datagramSocketSend.setBroadcast(true);
                    datagramSocketSend.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }

    /**
         * 广播接受
         */
    public void onBroadcastReceive() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
// 创建接收数据报套接字并将其绑定到本地主机上的指定端口
                    DatagramSocket datagramSocket = new DatagramSocket(BROADCAST_PORT);
                    while (isRuning) {
                        byte[] buf = new byte[1024];
                        final DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
                        datagramSocket.receive(datagramPacket);

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                serverHost = datagramPacket.getAddress().getHostAddress();
                                final String message = new String(datagramPacket.getData(), 0, datagramPacket.getLength())
                                        + " from " + datagramPacket.getAddress().getHostAddress() + ":" + datagramPacket.getPort();
                                Toast.makeText(Main3Activity.this, "广播接受=" + message, Toast.LENGTH_SHORT).show();
                            }
                        });
                        Thread.sleep(1000);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
         * 单播发送
         */
    public void onUnicastSend(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    InetAddress inetAddress = InetAddress.getByName(serverHost);
                    byte[] message = jh601a_message.message(mip,mport,"01","0000","8899aabb","ccdd");
                    DatagramPacket datagramPacket = new DatagramPacket(message, message.length,
                            inetAddress, UNICAST_PORT);
                    DatagramSocket datagramSocket = new DatagramSocket();
                    datagramSocket.send(datagramPacket);
                    datagramSocket.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }

    /**
         * 单播接受
         */
    public void onUnicastReceive() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    DatagramSocket server = new DatagramSocket(UNICAST_PORT);
                    while (isRuning) {
                        byte[] buf = new byte[1024];
                        final DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
                        server.receive(datagramPacket);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                String receiveMsg = new String(datagramPacket.getData(), 0, datagramPacket.getLength())
                                        + " from " + datagramPacket.getAddress().getHostAddress() + ":" + datagramPacket.getPort();
                                Toast.makeText(Main3Activity.this, "单播接受=" + receiveMsg, Toast.LENGTH_SHORT).show();
                            }
                        });
                        Thread.sleep(1000);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
        }).start();
    }

    /**
         * 组播发送
         */
    public void onMulticastSend(View view) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
//IP组
                    InetAddress inetAddress = InetAddress.getByName(MULTICAST_IP);
//组播监听端口
                    MulticastSocket multicastSocket = new MulticastSocket(MULTICAST_PORT);
                    multicastSocket.setTimeToLive(1);
//加入该组
                    multicastSocket.joinGroup(inetAddress);
//将本机的IP(这里可以写动态获取的IP)地址放到数据包里,其实server端接收到数据包后也能获取到发包方的IP的
                    byte[] data = jh601a_message.message(mip,mport,"01","0000","8899aabb","ccdd");
                    DatagramPacket dataPacket = new DatagramPacket(data, data.length, inetAddress, MULTICAST_PORT);
                    multicastSocket.send(dataPacket);
                    multicastSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
         * 组播接受
         */
    private void onMulticastSendReceive() {


        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    InetAddress inetAddress = InetAddress.getByName(MULTICAST_IP);
                    MulticastSocket multicastSocket = new MulticastSocket(MULTICAST_PORT);
                    multicastSocket.joinGroup(inetAddress);
                    byte buf[] = new byte[1024];
                    DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length, inetAddress, MULTICAST_PORT);

                    while (isRuning) {
                        multicastSocket.receive(datagramPacket);
                        final String message = new String(buf, 0, datagramPacket.getLength());
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(Main3Activity.this, "组播接受=" + message, Toast.LENGTH_SHORT).show();
                            }
                        });
                        Thread.sleep(1000);
                    }
                } catch (
                        Exception e) {
                    e.printStackTrace();
                }
            }


        }).start();
    }


}



    

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/993024.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存