java 如何在android中使用UDP套接字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12652261/
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-10-31 09:42:45  来源:igfitidea点击:

How to use UDP sockets in android?

javaandroidudp

提问by Mr_Hmp

I am trying to use UDP sockets in a android, here I send my string from android emulator and receive that by my Java program on PC, but my Java program does not receive anything, although when I used Java program as both client and server (I made two different Java programs) it worked.

我正在尝试在 android 中使用 UDP 套接字,在这里我从 android 模拟器发送我的字符串并通过我在 PC 上的 Java 程序接收它,但是我的 Java 程序没有收到任何东西,尽管当我将 Java 程序用作客户端和服务器时(我制作了两个不同的 Java 程序)它有效。

This is my android main activity :

这是我的 android 主要活动:

public class First extends Activity {
Button b;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Thread t = new Thread(new Second());
            t.start();
        }
    });

Here is my second class in android :

这是我在 android 中的第二堂课:

    public class Second implements Runnable {
Second()
{
    run();
}
public void run() {
    // TODO Auto-generated method stub
    try {

        String messageStr = "Hello Android!";
        int server_port = 9876;
        DatagramSocket s = new DatagramSocket();
        InetAddress local = InetAddress.getByName("127.0.0.1");
        int msg_length = messageStr.length();
        byte[] message = messageStr.getBytes();
        DatagramPacket p = new DatagramPacket(message, msg_length, local,
                server_port);
        s.send(p);
    } catch (Exception e) {

    }
}
}

This is my Java code on PC:

这是我在 PC 上的 Java 代码:

    public static void main(String args[]) throws Exception
    {
            DatagramSocket serverSocket = new DatagramSocket(9876);
            byte[] receiveData = new byte[1024];
            byte[] sendData = new byte[1024];
            while(true)
            {
                    DatagramPacket receivePacket = new     DatagramPacket(receiveData,receiveData.length);
                    serverSocket.receive(receivePacket);
                    String sentence = new String(receivePacket.getData(),0,receivePacket.getLength());
                    InetAddress IPAddress = receivePacket.getAddress();
                    int port = receivePacket.getPort();
                    System.out.println("MESSAGE RECEIVED  "+sentence+"  "+IPAddress+"         "+port);
            }
     }

采纳答案by zmbq

On your Android emulation (and Android device), 127.0.0.1 means the Android emulation machine, not the host PC. You can access your host at 10.0.2.2

在您的 Android 仿真(和 Android 设备)上,127.0.0.1 表示 Android 仿真机器,而不是主机 PC。您可以在10.0.2.2访问您的主机