【发布时间】:2019-05-01 20:30:19
【问题描述】:
我正在尝试在运行 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;
}
客户端(安卓手机)代码:
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手机也不会收到任何内容。
【问题讨论】:
-
手机是wifi还是蜂窝?网络和所有网络之间是否允许 UDP(不是全部都允许)?您的路由器是否设置为允许 UDP 到 PC 的 IP 和端口?这很可能是网络设置问题。
-
手机通过wifi连接到与PC相同的网络。如何检查我的路由器是否设置为允许 UDP 到该 IP 和端口?
-
取决于路由器。进入其设置并查找任何提及 UDP 或端口路由的内容
-
指定的IP有问题。我使用的是VirtualBox,我在尝试打开端口时意识到。感谢您的帮助!
标签: android networking udp