I am getting quite desperate.
I managed to get your new code to work, but I still cannot get connected to the WIFI network, when the Android 'Active Network' is set to MOBILE.
Code:
uses Posix.SysSocket, Posix.NetIf, Posix.Termios, Posix.StrOpts
function StrToIfreq(Value:string):Posix.NetIf.ifreq;
var
i: integer;
TestStr: RawByteString;
begin
FillChar(Result, Sizeof(Result), 0);
TestStr := 'wlan1'; // <<<<< When using a non-existing name, the ioctl gives a "-1" error result. Else it gives a "0"
System.SetCodePage(TestStr,28591,false);
for i := 0 to length(TestStr) do
begin
Result.ifrn_name[i] := ord(TestStr[i]);
end;
end;
procedure TTCPThread.IdTCPClient1OnAfterBind(ASender: TObject);
const
SIOCGIFINDEX = $8933;
var
M: TMarshaller;
ifr: Posix.NetIf.ifreq;
AResult: integer;
begin
LocalLog('IdTCPClient: OnAfterBind: ');
if NetworkData.LocalIP <> '' then
begin
try
if NetworkData.WIFIInterface <> '' then
begin
LocalLog('IdTCPClient1.Socket.Binding.Handle='+IntToStr(IdTCPClient1.Socket.Binding.Handle)); // Result: "46"
ifr := StrToIfreq(NetworkData.WIFIInterface);
AResult := ioctl(IdTCPClient1.Socket.Binding.Handle, SIOCGIFINDEX, @ifr);
LocalLog('ioctl returned: '+IntToStr(AResult));
Posix.SysSocket.setsockopt(IdTCPClient1.Socket.Binding.Handle, Id_SOL_SOCKET, SO_BINDTODEVICE, ifr, SizeOf(ifr))
end;
except
on E:Exception do LocalLog('TCPThread: GStack failed: WIFIInterface='+NetworkData.WIFIInterface
+' Length(WIFIInterface)='+IntToStr(length(NetworkData.WIFIInterface))
+': '+E.Message,d_error);
end;
end; // ELSE: We are now using the Android Active Network, whatever that may be.
end;
What happens is this.
1) When Android sets the Active Network to 'wlan0', and I use same name in the ioctl, this function gives a result of '0'. AND I now get connected to the WIFI based server.
2) When Android sets its Active Network to the MOBILE network, the ioctl return still '0', but I do NOT get any connection to local WIFI server, nothing goes out over the WIFI network.
3) Using situation (1), but now I use a non-valid interface of 'wlan1', the ioctl gives an error result '-1' as it should. But then afterwards, I still get connected to the WIFI server: that is, after the failure of the ioctl, it reverts back to the 'default' network of 'wlan0' (which is active at that moment).
This means that the ioctl works correctly. But I still do net get access to the 'wlan0' interface, unless Android has set this to the "Active Network".
And note that the ioctl return a successful '0'. I am not sure however, if there is another error hidden somewhere in the interface
What else can I do?
Can you please explain to me how Indy knows to which of the two networks to connect all by itself?
There are TWO working interfaces available.
But Indy automatically select the one which Android has set to the 'Active Network'.
How does this work then? As Indy works directly with the Linux interfaces?
Why would Indy use the one Android selects at Linux level? E.g. What makes it a "default"??
Thanks, Bart