访问连接到安卓手机的以太网端口

huangapple go评论62阅读模式
英文:

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&#39;ve attached a USB to ethernet port converter and I&#39;m trying to communicate through my laptop as a server.
I&#39;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&#39;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&#39;ve my entire wifi internet, it worked.

Not able to figure out the problem.

[Table][1]

    Thread t1 = new Thread(() -&gt; {
                    /* 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&#39;s address

                        con = new TCPMasterConnection(addr);
                        con.setPort(port);
                        con.setTimeout(500);
                        Log.d(&quot;Connection parameters&quot;, &quot;Timeout: &quot; + con.getTimeout() + &quot; Port: &quot; + port);
                        con.connect();
                        runOnUiThread(() -&gt; {
                            tv.setText(&quot;Connected!!!&quot;);
                            connect.setText(&quot;Disconnect&quot;);
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.e(&quot;Error!&quot;, e.toString());
                        runOnUiThread(() -&gt; {
                            tv.setText(e.toString());
                        });
                    }
                });
                if (connect.getText().equals(&quot;Connect&quot;))
                    t1.start();
                else {
                    Log.d(&quot;In diconnect&quot;, &quot;Coonection closed!&quot;);
                    con.close();
                    connect.setText(&quot;Connect&quot;);
                    tv.setText(&quot;Disconnected!!!&quot;);
                }

  [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&#39;s the IP address from the DHCP server, but when you connect it directly to your laptop, your phone simply doesn&#39;t have any IP address, unless you set up a static address, so it&#39;s not visible on the network.

</details>



huangapple
  • 本文由 发表于 2023年3月3日 17:37:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75625388.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定