Read the original article:How to resolve the IPv6 network connection error 'code': 2301101
How to resolve the IPv6 network connection error 'code': 2301101
Problem Description
When using mobile data, the initial connection to an IPv6 address is successful. However, after turning off and then turning on the mobile data switch again, attempting to connect to the IPv6 address results in a failure. The tcpSocket.connect throws an exception with the error message: {'code': 2301101, 'essage': 'Network unreachable'}.
Additionally, when subscribing to the network event netAvailable and printing connection information, it was observed that during the initial connection, the linkAddresses included an IPv6 address. After turning off and then turning on the mobile data switch, the printed linkAddresses no longer contained any IPv6 addresses.
The problematic code is as follows:
let conn = connection.createNetConnection();
conn.register((error: BusinessError) => {});
conn.on('netAvailable', ((netHandle: connection.NetHandle) => {
let properties = connection.getConnectionPropertiesSync(netHandle);
if (properties && properties.linkAddresses) {
console.info(LOG_TAG, JSON.stringify(properties.linkAddresses))
}
// Connect the socket.
this.connect()
}));
Log:
03-27 11:10:24.179 A03D00/com.exa...pv6test/JSAPP apppool I ipv6_test [{"address":{"address":"10.74.xxx.6","family":1,"port":0},"prefixLength":32},{"address":{"address":"2409:8915:xxxx:xxxx:8e49","family":2,"port":0},"prefixLength":128}]
03-27 11:10:24.227 A03D00/com.exa...pv6test/JSAPP com.example.ipv6test I ipv6_test socket connect result:true
03-27 11:10:30.012 A03D00/com.exa...pv6test/JSAPP com.example.ipv6test I ipv6_test [{"address":{"address":"10.103.xxx.203","family":1,"port":0},"prefixLength":32}]
03-27 11:10:30.015 A03D00/com.exa...pv6test/JSAPP com.example.ipv6test I ipv6_test tcp connect error:{"code":2301101,"message":"Network unreachable"}
03-27 11:10:30.015 A03D00/com.exa...pv6test/JSAPP com.example.ipv6test I ipv6_test socket connect result:false
Background Knowledge
- on('netAvailable'): Subscribes to the network availability event, which is triggered when the network becomes available.
- on('netConnectionPropertiesChange'): Subscribes to the network connection information change event, which is triggered when there is a change in network connection information, such as transitioning from no network to having a network, or switching from WiFi to cellular. Actions like turning on the network, turning it off, and then turning it on again also constitute changes in network connection information. Using netConnectionPropertiesChange allows for more intuitive monitoring of network status.
Solution
- netAvailable indicates that there is currently an available network, but it does not guarantee that both IPv4 and IPv6 are ready at this time.
- When the mobile network is restarted, IPv4 is activated first, followed by IPv6. The netAvailable event is triggered once IPv4 is activated. Therefore, before using IPv6, please ensure that a valid IPv6 address has been assigned; otherwise, the connection will fail.
The code solution is as follows:
conn.on('netConnectionPropertiesChange', ((date: connection.NetConnectionPropertyInfo) => {
for (let i = 0; i < date.connectionProperties.linkAddresses.length; i++) {
console.info(LOG_TAG, JSON.stringify(date.connectionProperties.linkAddresses))
if (date.connectionProperties.linkAddresses[i].address.family === 2) {
this.connect()
}
}
}))
Verification Result
03-28 11:04:25.184 6790-6790 A03D00/com.exa...pv6test/JSAPP apppool I ipv6_test [{"address":{"address":"10.xxx.29.79","family":1,"port":0},"prefixLength":32},{"address":{"address":"2409:891f:xxxx:f5c0","family":2,"port":0},"prefixLength":128}]
03-28 11:04:25.184 6790-6790 A03D00/com.exa...pv6test/JSAPP apppool I ipv6_test [{"address":{"address":"10.xxx.29.79","family":1,"port":0},"prefixLength":32},{"address":{"address":"2409:891f:xxxx:f5c0","family":2,"port":0},"prefixLength":128}]
03-28 11:04:25.222 6790-6790 A03D00/com.exa...pv6test/JSAPP apppool I ipv6_test socket connect result:true
03-28 11:04:45.614 6790-6790 A03D00/com.exa...pv6test/JSAPP com.example.ipv6test I ipv6_test [{"address":{"address":"10.88.xx.24","family":1,"port":0},"prefixLength":32}]
03-28 11:04:45.684 6790-6790 A03D00/com.exa...pv6test/JSAPP com.example.ipv6test I ipv6_test [{"address":{"address":"10.88.xx.24","family":1,"port":0},"prefixLength":32},{"address":{"address":"2409:891f:b805:xxxx:2c7a","family":2,"port":0},"prefixLength":128}]
03-28 11:04:45.685 6790-6790 A03D00/com.exa...pv6test/JSAPP com.example.ipv6test I ipv6_test [{"address":{"address":"10.xx.30.24","family":1,"port":0},"prefixLength":32},{"address":{"address":"2409:891f:xxxx:2232:2c7a","family":2,"port":0},"prefixLength":128}]
03-28 11:04:45.719 6790-6790 A03D00/com.exa...pv6test/JSAPP com.example.ipv6test I ipv6_test socket connect result:true
Top comments (0)