无法从Android手机在PC上接收UDP数据包

时间:2019-05-01 20:30:19

标签: android networking udp

我正在尝试在运行Java UDP服务器的PC与运行UDP客户端的Android手机之间建立简单的连接。 Android表示包裹已发送,但PC没有收到任何东西。

两个设备都在同一网络中,PC通过IP 192.168.56.1的以太网连接,而Android通过Wi-fi连接。两者的端口均为2050,两者的缓冲区大小均为1024。我正在尝试从Android手机发送4位PIN码,再加上字符#,再加上4位接受码(在这种情况下为0001),因此消息类似于2348#0001。 PC应该收到包裹并解码此消息。

服务器(PC)代码(永远保存在接收中):

private boolean connect() throws Exception{
    System.out.println("Waiting for PIN code...");
    String msg;
    byte[] buffer = new byte[BUFFER_SIZE];
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
    socket.receive(packet);
    System.out.println("Received packet");
    msg = new String(packet.getData()).trim();
    String[] msgDecoded = msg.split("#");
    clientIP = packet.getAddress();

    if(msgDecoded[0].equals(password) && msgDecoded[1].equals(CONNECTION_ACCEPT_CODE)) return true;
    else return false;
}

客户端(Android手机)代码:

public void establishConnection(View view) throws Exception{

    /* Variable assignments and initializations */
    clientSocket = new DatagramSocket();
    serverIP = ((EditText)findViewById(R.id.ipInput)).getText().toString();
    Log.d("Info", "IP ADDRESS: " + serverIP);
    InetAddress IPAddress = InetAddress.getByName(serverIP);
    final String password = ((EditText) findViewById(R.id.passwordInput)).getText().toString();
    String message = password + "#" + CONNECTION_ACCEPT_CODE;
    data = message.getBytes();
    final DatagramPacket sendPacket = new DatagramPacket(data, data.length, IPAddress, DEFAULT_PORT);

    /* Send the data to the server */
    /* Needs to be done in another thread, otherwise NetworkOnMainThreadException is thrown */
    Thread sendThread = new Thread(new Runnable() {
        @Override
        public void run(){
            try{ clientSocket.send(sendPacket); Log.d("TEST", "Sent" + sendPacket.getData().toString());}
            catch (Exception e){ Log.d("TEST", "EXCEPTION SENDING"); }
        }
    });
    sendThread.start();


    /* Now wait for server ACK */
    receiveData = new byte[BUFFER_SIZE];
    final DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

    /* Receive data from the server */
    /* Needs to be done in another thread, otherwise NetworkOnMainThreadException is thrown */
    Thread receiveThread = new Thread(new Runnable() {
        @Override
        public void run(){
            try{
                clientSocket.receive(receivePacket);
                Log.d("RECEIVED", receivePacket.getData().toString());
                String ack = new String(receivePacket.getData(), StandardCharsets.UTF_8);
                /* If receive ACK go to next activity */
                if(ack.equals(ACK_ACCEPTED)){
                    Intent intent = new Intent(getApplicationContext(), KeylogActivity.class);
                    intent.putExtra(EXTRA_IPADDR, serverIP);
                    intent.putExtra(EXTRA_PASSWD, password);
                    startActivity(intent);
                }

                /* If we don't, log error */
                // TODO: do actual job here
                else{
                    Log.d("[Error]", "Server rejected the connection: " + ack);
                }

            }
            catch (Exception e) { Log.d("ERROR", "Exception receiving: " + e); }
        }
    });
    receiveThread.start();
}

SendThread似乎运行良好,因为它记录为“已发送”,但对PC无效。由于PC服务器不接收消息,因此它也不会发送任何消息,因此Android手机也不会接收任何消息。

1 个答案:

答案 0 :(得分:0)

好吧,问题是我试图连接到VirtualBox仅主机网络。我在Powershell上运行了ipconfig命令,并看到了我的真实IP。更改应用程序中的IP以匹配以太网接口可以解决我的问题。