在Wireshark中,我看不到从android多播套接字发送的数据包

时间:2018-10-24 01:14:20

标签: android wireshark multicastsocket

我遵循步骤here来设置多播服务器,但是由于某种原因,我无法在Wireshark中看到我的数据包。我正在监视我的wifi接口。这是我使用的代码

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener((e) -> {
            try {
                boom();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        });
    }

    private void boom() throws Exception
    {
        new AsyncServer().execute("");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    class AsyncServer extends AsyncTask<String, Void, Void> {

        private Exception exception;

        protected Void doInBackground(String... args)
        {
            String msg = "Hello";
            InetAddress group = null;
            try {
                group = InetAddress.getByName("239.1.2.3");
                MulticastSocket s = new MulticastSocket(4333);
                // s.setInterface(InetAddress.getByName("192.168.232.2")); // To use wifi interface, but doesn't seem to be necessary. The message I will send at 75 and receive at 79 will have address 192.168.232.2
                s.joinGroup(group);
                DatagramPacket hi = new DatagramPacket(msg.getBytes(), msg.length(), group, 4333);
                s.send(hi);

                byte[] buf = new byte[1000];
                DatagramPacket recv = new DatagramPacket(buf, buf.length);
                s.receive(recv);

                System.out.println("Received Message: " + recv.getData());
                System.out.println("Send By: " + recv.getAddress());

                // OK, I'm done talking - leave the group...
                s.leaveGroup(group);

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

            return null;
        }
    }
}

我在Java应用程序中尝试了类似的方法,但效果很好,所以我假设这是android的问题。你们有什么主意吗?

谢谢

0 个答案:

没有答案