首页 > 解决方案 > 如何修复 Android 9.0 中的套接字 IO 异常?

问题描述

我正在使用 java.net.Socket 进行套接字通信。我已经尝试了 network-security-config.xml 文件的解决方案,该文件允许 localhost 进行套接字通信。

我的代码片段如下:

Socket socket = new Socket("192.168.1.1", "5000");

我的 network-security-config.xml 文件如下:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
    <trust-anchors>
        <certificates src="system" />
    </trust-anchors>
</base-config>
<domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="true">http://192.168.1.1:5000</domain>
</domain-config>

我也试过下面的片段

1) Replace <domain includeSubdomains="true">http://192.168.1.1:5000</domain> to <domain includeSubdomains="true">http://localhost:5000</domain>

2) Replace <domain includeSubdomains="true">http://192.168.1.1:5000</domain> to <domain includeSubdomains="true">localhost</domain>

但上述解决方案均无效。可能是我错过了什么。

如果有人有这个问题的经验。请提供解决方案。

谢谢你的帮助。

标签: socket.ioandroid-9.0-pie

解决方案


推荐阅读