Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IdTCPClient on Android: Connecting to non-active network
#13
(05-09-2018, 01:36 PM)BartKindt Wrote:
Code:
if NOT assigned(GStack) then // just checking...

You don't need that check. The GStack pointer is always valid whenever an Indy component is alive.

(05-09-2018, 01:36 PM)BartKindt Wrote:
Code:
Posix.SysSocket.setsockopt(
         IdTCPClient1.Socket.Binding.Handle,
         Id_SOL_SOCKET,
         SO_BINDTODEVICE,
         M.AsAnsi(NetworkData.WIFIInterface).ToPointer^,
         Length(NetworkData.WIFIInterface)+1 // assuming the device name uses ASCII chars only
         )

Not sure if this applies to Android's flavor of Linux, but on some Linux platforms, SO_BINDTODEVICE takes an ifreq struct as input instead of a null-terminated string pointer, eg:

Code:
struct ifreq ifr;
memset(&ifr, 0, sizeof(struct ifreq));
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "eth0");
ioctl(fd, SIOCGIFINDEX, &ifr);
setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,  (void*)&ifr, sizeof(struct ifreq));

Which would translate to Delphi as (roughly):

Code:
uses
  ... Posix.NetIf, Posix.StrOpts, Posix.SysSocket;

const
  SIOCGIFINDEX = $8933;

var
  ifr: Posix.NetIf.ifreq;

FillChar(ifr, Sizeof(ifr), 0);
StrLCopy(ifr.ifr_name, M.AsAnsi(NetworkData.WIFIInterface).ToPointer, IFNAMSIZ-1);
Posix.StrOpts.ioctl(IdTCPClient1.Socket.Binding.Handle, SIOCGIFINDEX, @ifr);
Posix.SysSocket.setsockopt(IdTCPClient1.Socket.Binding.Handle, Id_SOL_SOCKET, SO_BINDTODEVICE, ifr, SizeOf(ifr))

(05-09-2018, 01:36 PM)BartKindt Wrote: ERROR: TCPThread: GStack failed: WIFIInterface=wlan0 Length(WIFIInterface)=5: Socket Error # 1

Error #1 is EPERM ("Operation not permitted"). You likely need root/sudo access to your Android device to bind to a specific interface by name.

Reply


Messages In This Thread
RE: IdTCPClient on Android: Connecting to non-active network - by rlebeau - 05-09-2018, 06:53 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)