Android Question how to bind a socket to a specific interface?

Alessandro71

Well-Known Member
Licensed User
Longtime User
consider this (strange) scenario:
android phone, with cellular data connectivity by a SIM, connected to a wi-fi network, and with an USB-C ethernet adapter.
cellular and wi-fi are connected to the "external world" (i.e. internet), while the ethernet adapter is connected with cross cable to a windows machine acting as a custom server.
i need to connect to that windows machine.

if i list the available network interfaces with the following code

B4X:
    Dim sb As StringBuilder
    sb.Initialize
    sb.Append("Interfaces").Append(CRLF)
    
    Dim niIterator As JavaObject
    niIterator = niIterator.InitializeStatic("java.net.NetworkInterface").RunMethod("getNetworkInterfaces", Null)
    Do While niIterator.RunMethod("hasMoreElements", Null)
        Dim ni As JavaObject = niIterator.RunMethod("nextElement", Null)
        If ni.RunMethod("isLoopback", Null) = False Then
            Dim name As String = ni.RunMethod("getName", Null)
            sb.Append($"Name: ${name}"$).Append(CRLF)
            
            Dim addresses As List = ni.RunMethod("getInterfaceAddresses", Null)
            For Each ia As JavaObject In addresses

                Dim addr As Object = ia.RunMethod("toString", Null)
                If addr <> Null Then
                    Dim b As String = addr
                    sb.Append($"  IP: ${b}"$).Append(CRLF)
                End If
            Next
        End If
    Loop
    
    WriteLog(sb.ToString)
i get this topology, where the device has 3 different network connected

B4X:
Name: eth0
  IP: /fe80::7030:eae:58a4:22e4%eth0/64 [null]
  IP: /192.168.11.10/24 [/192.168.11.255]
Name: wlan0
  IP: /fe80::88c8:3cff:fe9e:f1cb%wlan0/64 [null]
  IP: /192.168.50.5/24 [/192.168.50.255]
Name: rmnet1
  IP: /10.5.34.164/24 [null]

the server that is connected to the eth0 interface, answers to UDP broadcasts with a UDP packet.
i initialize the UDP socket for the answer with

B4X:
m_UDPSocket.Initialize("m_UDPSocket", 0, 64)
i send the UDP broadcast with

B4X:
Dim udpp As UDPPacket
udpp.Initialize(m_buffer, "255.255.255.255", PORT)
socket.Send(udpp)

and i get the answer with

B4X:
Sub m_UDPSocket_PacketArrived (Packet As UDPPacket)
    WriteLog($"UDP received << ${Packet.HostAddress}:${Packet.Port} ${Packet.Length} ${Packet.Offset}"$)
End Sub
this all works fine if the phone is in Airplane mode and eth0 is the only active interface.
if one of the other interfaces are active (rmnet1 or wlan0) the windows server does not receive the UDP broadcast.
i suspect it is sent via one of the other interfaces.

how can i make sure the UDP packet is sent via eth0?
 

Alessandro71

Well-Known Member
Licensed User
Longtime User
Thank you.
I'm currently browsing the docs to check if it's possible to use Ethernet for only the locally connected subnet, while maintaining general internet connectivity through the other interfaces.
 
Upvote 0
Top