Android实现局域网组播

package com.example.jinchao.dimenstest;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MulticastSocket;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;


public class MainActivity extends Activity {

    private static final String TAG = MainActivity.class.getSimpleName();

    private static final int PORT = 5734;
    private MulticastSocket multicastSocket;
    private DatagramPacket datagramPacket;
    private InetAddress group;

    private Button button1;
    private Button button2;
    private Button button3;
    private TextView result;
    private EditText editText;

    private HandlerThread handlerThread;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        result = findViewById(R.id.result);
        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        button3 = findViewById(R.id.button3);
        editText = findViewById(R.id.edit1);


        handlerThread = new HandlerThread(TAG);
        handlerThread.start();
        handler = new Handler(handlerThread.getLooper());

        init();

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        String msg = "hello";
                        for (int i = 0; i < 1000; i++) {
                            String message = msg + i;
                            datagramPacket = new DatagramPacket(message.getBytes(), message.length(), group, PORT);
                            try {
                                Log.d(TAG, "send: " + message);
                                multicastSocket.send(datagramPacket);
                                Thread.sleep(1000);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });

            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {

                        byte[] buffer = new byte[512];
                        DatagramPacket recv = new DatagramPacket(buffer, buffer.length, group, PORT);
                        while (true) {
                            try {
                                multicastSocket.receive(recv);
                                Log.d(TAG, "rev: " + new String(buffer, StandardCharsets.UTF_8));
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
            }
        });

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                    }
                });

            }
        });
    }

    private void init() {
        try {
            multicastSocket = new MulticastSocket(PORT);
            group = InetAddress.getByName("230.0.0.0");
            multicastSocket.setLoopbackMode(false);
            multicastSocket.setTimeToLive(1);
            multicastSocket.joinGroup(group);

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

}

你可能感兴趣的:(知否?)