英文:
Accessing ethernet port connected to android phone
问题
I am trying to build an app for modbus tcp/ip client on my android device.
我正在尝试在我的安卓设备上构建一个Modbus TCP/IP客户端应用。
I've attached a USB to ethernet port converter and I'm trying to communicate through my laptop as a server.
我连接了一个USB到以太网端口转换器,尝试通过我的笔记本电脑作为服务器进行通信。
I'm not able to connect to the network with the error saying:
我无法连接到网络,出现错误提示:
java.net.ConnectionException: failed to connect to /192.168.29.244 (port 502) from /::(port 0): connect failed: ENETUNREACH (network is unreachable)
java.net.ConnectionException:无法连接到/192.168.29.244(端口502),从/::(端口0):连接失败:ENETUNREACH(网络不可达)
I'm trying that by keeping the phone on Wifi and data off/airplane mode so as not to confuse it with Wifi signals.
我尝试通过让手机保持在Wi-Fi上,关闭数据/飞行模式,以避免与Wi-Fi信号混淆。
It works flawlessly when my android device and laptop are on the same wireless network, but when attempting a wired connection with the laptop, it fails with the above-mentioned error.
当我的安卓设备和笔记本电脑连接到相同的无线网络时,它运行得非常顺畅,但尝试使用笔记本电脑进行有线连接时,出现上述错误。
But when I tried a wired connection with the network switch on which I've my entire wifi internet, it worked.
但当我尝试在安装了整个Wi-Fi互联网的网络交换机上进行有线连接时,它起作用。
Not able to figure out the problem.
无法找出问题。
Thread t1 = new Thread(() -> {
/* The important instances of the classes mentioned before */
con = null; //the connection
/* Variables for storing the parameters */
int port = Integer.parseInt(etPort.getText().toString());
String ip = etIP.getText().toString();
try {
InetAddress addr = InetAddress.getByName(ip);//the slave's address
con = a TCPMasterConnection(addr);
con.setPort(port);
con.setTimeout(500);
Log.d("Connection parameters", "Timeout: " + con.getTimeout() + " Port: " + port);
con.connect();
runOnUiThread(() -> {
tv.setText("Connected!!!");
connect.setText("Disconnect");
});
} catch (Exception e) {
e.printStackTrace();
Log.e("Error!", e.toString());
runOnUiThread(() -> {
tv.setText(e.toString());
});
}
});
if (connect.getText().equals("Connect"))
t1.start();
else {
Log.d("In diconnect", "Coonection closed!");
con.close();
connect.setText("Connect");
tv.setText("Disconnected!!!");
}
<details>
<summary>英文:</summary>
I am trying to build an app for modbus tcp/ip client on my android device.
I've attached a USB to ethernet port converter and I'm trying to communicate through my laptop as a server.
I'm not able to connect to the network with the error saying:
java.net.ConnectionException: failed to connect to /192.168.29.244 (port 502) from /::(port 0): connect failed: ENETUNREACH (network is unreachable)
I'm trying that by keeping the phone on Wifi and data off/airplane mode so as not to confuse it with Wifi signals.
It works flawlessly when my android device and laptop are on the same wireless network, but when attempting a wired connection with the laptop, it fails with the above-mentioned error.
But when I tried a wired connection with the network switch on which I've my entire wifi internet, it worked.
Not able to figure out the problem.
[Table][1]
Thread t1 = new Thread(() -> {
/* The important instances of the classes mentioned before */
con = null; //the connection
/* Variables for storing the parameters */
int port = Integer.parseInt(etPort.getText().toString());
String ip = etIP.getText().toString();
try {
InetAddress addr = InetAddress.getByName(ip);//the slave's address
con = new TCPMasterConnection(addr);
con.setPort(port);
con.setTimeout(500);
Log.d("Connection parameters", "Timeout: " + con.getTimeout() + " Port: " + port);
con.connect();
runOnUiThread(() -> {
tv.setText("Connected!!!");
connect.setText("Disconnect");
});
} catch (Exception e) {
e.printStackTrace();
Log.e("Error!", e.toString());
runOnUiThread(() -> {
tv.setText(e.toString());
});
}
});
if (connect.getText().equals("Connect"))
t1.start();
else {
Log.d("In diconnect", "Coonection closed!");
con.close();
connect.setText("Connect");
tv.setText("Disconnected!!!");
}
[1]: https://i.stack.imgur.com/jDPYK.png
</details>
# 答案1
**得分**: 0
我不确定我完全理解问题,但我认为它与您将手机直接连接到笔记本电脑有关。
当您将手机连接到网络设备(如路由器或交换机)时,它会从DHCP服务器获取IP地址,但当您将其直接连接到笔记本电脑时,您的手机简单地没有任何IP地址,除非您设置了静态地址,否则它在网络上是不可见的。
<details>
<summary>英文:</summary>
I am not sure that I completely understand the question, but I do think that it has something to do with you connecting your phone directly to your laptop.
When you connect your phone to a network device, such as router or switch, it get's the IP address from the DHCP server, but when you connect it directly to your laptop, your phone simply doesn't have any IP address, unless you set up a static address, so it's not visible on the network.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论