无法在 PC 上接收来自 Android phone 的 UDP 数据包
Unable to receive UDP packet on PC from Android phone
我正在尝试在 PC 运行 Java UDP 服务器和 Android phone 运行 UDP 客户端之间建立一个简单的连接。 Android表示包裹已发送,但电脑没有收到任何东西。
两台设备在同一网络下,PC 通过以太网连接,IP 192.168.56.1,Android 通过 Wi-fi 连接。两者的端口都是2050,缓冲区大小都是1024。我正在尝试从 Android phone 发送一个 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;
}
客户(Androidphone)代码:
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 似乎工作正常,因为它记录 "Sent",但它在 PC 上没有效果。由于 PC 服务器不接收消息,它也不发送任何消息,因此 Android phone 不接收任何消息。
好的,问题是我试图连接到 VirtualBox Host-Only 网络。我 运行 在 Powershell 上使用命令 ipconfig 并看到了我的真实 IP。更改应用程序中的 IP 以匹配以太网接口 one 解决了我的问题。
我正在尝试在 PC 运行 Java UDP 服务器和 Android phone 运行 UDP 客户端之间建立一个简单的连接。 Android表示包裹已发送,但电脑没有收到任何东西。
两台设备在同一网络下,PC 通过以太网连接,IP 192.168.56.1,Android 通过 Wi-fi 连接。两者的端口都是2050,缓冲区大小都是1024。我正在尝试从 Android phone 发送一个 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;
}
客户(Androidphone)代码:
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 似乎工作正常,因为它记录 "Sent",但它在 PC 上没有效果。由于 PC 服务器不接收消息,它也不发送任何消息,因此 Android phone 不接收任何消息。
好的,问题是我试图连接到 VirtualBox Host-Only 网络。我 运行 在 Powershell 上使用命令 ipconfig 并看到了我的真实 IP。更改应用程序中的 IP 以匹配以太网接口 one 解决了我的问题。