Java 在android中的数据报套接字上发送和接收UDP数据包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18430330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:33:56  来源:igfitidea点击:

sending and receiving UDP packet on datagram socket in android

javaandroidsocketsudpudpclient

提问by Ameer Humza

I have two classes,one sender class and the other is the receiver class.Both of the sending and receiving apps stops after few seconds and close down. My sender class is :

我有两个类,一个是发送者类,另一个是接收者类。发送和接收应用程序都在几秒钟后停止并关闭。我的发件人类是:

    public class MainActivity extends Activity {
InetAddress receiverAddress;
DatagramSocket datagramSocket;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    try {
        datagramSocket = new DatagramSocket(4444);
    } catch (SocketException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    byte[] buffer = "0123456789".getBytes();
    byte[] address="192.168.1.101".getBytes();

    try {
        receiverAddress = InetAddress.getByAddress(address);
    } catch (UnknownHostException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    DatagramPacket packet = new DatagramPacket(
            buffer, buffer.length, receiverAddress, 4444);

    try {
        datagramSocket.send(packet);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }




}

My receiving or listening class is:

我的接收或听力课程是:

public class MainActivity extends Activity {
DatagramSocket datagramSocket;
DatagramPacket packet;
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv1=(TextView)findViewById(R.id.textView1);
     try {
        datagramSocket = new DatagramSocket(80);
    } catch (SocketException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    byte[] buffer = new byte[10];
     packet = new DatagramPacket(buffer, buffer.length);

    try {
        datagramSocket.receive(packet);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    byte[] buff = packet.getData();

tv1.setText(buff.toString());

}

Thanks in advance for the help.

在此先感谢您的帮助。

回答by sleinen

The port numbers in the "new DatagramSocket(...)" calls look weird. The client should create an "unbound" socket - simply use "new DatagramSocket();". The sender should bind to the port that the client sends to, i.e. "new DatagramSocket(4444);".

“new DatagramSocket(...)”调用中的端口号看起来很奇怪。客户端应该创建一个“未绑定”的套接字——只需使用“new DatagramSocket();”。发送方应该绑定到客户端发送到的端口,即“new DatagramSocket(4444);”。

回答by sleinen

Source and destination port number should be same. Give same numbers in "DatagramSocket(xxx)". xxx must be same in both programs.

源端口号和目标端口号应该相同。在“DatagramSocket(xxx)”中给出相同的数字。两个程序中的 xxx 必须相同。

回答by Sven

In Android you're not allowed to execute Network operations on the UIThread (Main-Thread)

在 Android 中,您不允许在 UIThread(主线程)上执行网络操作

To Fix this: Copy your network-code to a new Thread and let it run.

要解决此问题:将您的网络代码复制到一个新线程并让它运行。