Re: How to know if a network adapter is connected




"fred_d" <duchassin@xxxxxxxxx> wrote in message news:gcuqr0$2r1$1@xxxxxxxxxxxxxxxxxxxxx
Hello,

Binding a socket to an adapter instructs the NDIS to receive only from this socket. This is the only way I know to identify the network adapter, where a packet is received. You have to bind the adapter before calling any send/receive function. Sorry.

Yes i agree but if the adapter is not connected to the network, the bind() function return SOCKET_ERROR. So i need to bind every socket each time i call the recv() ????

No. You bind a socket to an adapter, when you determine that the adapter is connnected to the network. If you have a service you get the IOCTL_SERVICE_NOTIFY_ADDR_CHANGE notification. You call recv when the return value of the select (or WSAAsyncSelect or WSAEventSelect) indicates that you can receive anything.

This is an extract of my source code:

DWORD WINAPI DHCPThread(LPVOID pVoid)
{
...

sockETH = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
sockUSB = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

...

ret=setsockopt(sockETH, SOL_SOCKET, SO_BROADCAST, (const char *) &one, sizeof(one));
ret=setsockopt(sockUSB, SOL_SOCKET, SO_BROADCAST, (const char *) &one, sizeof(one));

while (!g_stopDHCP)
{
...
}
I try to bind the socket each time before calling recv().
No. You don't do it in your excerpt.

If you see some errors please.
The bind is done at initialization but not at "adapter connected" state change. As you have reported bind doesnt work, if the adapter is not connected to the network in this moment.

As mentioned before: bind has not to be called before any recv but once after the creation of a socket and the fist recv/send call.

I don't understand what you want to tell me. I doesn't write a service.
I would consider to write a helper service, that gets the IOCTL_SERVICE_NOTIFY_ADDR_CHANGE notification and indicates this change to your application. This indication could be done i.e. using an event (CreateEvent API). The application would check what adapters are connected to network and create (and bind) appropriate sockets.
Since you have a DHCPThread one design could be to have a thread for each adapter with just one socket. You can startup/shutdown thread instances for all adapters you want to support.

/Helge

.



Relevant Pages

  • Re: UDP question.
    ... >> My two adapters has two different IP address, and I bind one IP ... >> do you mean that I alloc two socket and bind different IP is not ... > sending a packet *to* 1.2.4.5, it will go out the first interface. ... not real bind the adapter? ...
    (Linux-Kernel)
  • Re: UDP question.
    ... > do you mean that I alloc two socket and bind different IP is not ... it will go out the first interface. ... not real bind the adapter? ...
    (Linux-Kernel)
  • Re: How to know if a network adapter is connected
    ... This is the only way I know to identify the network adapter, ... Yes i agree but if the adapter is not connected to the network, the bind() ... So i need to bind every socket each time i ... int lenETH = sizeof; ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: UDP question.
    ... >>> My two adapters has two different IP address, and I bind one IP ... >>> do you mean that I alloc two socket and bind different IP is not ... >> interface address it came from. ... why dont you try a different subnet for the other adapter & its dest. ...
    (Linux-Kernel)
  • Re: How to know if a network adapter is connected
    ... Because after receiving network packets i would like to bind to usb network and if it's ok, ... If i use bind before recv, and i give an IP address to bind, it's not working if the interface is not connected! ... This is the only way I know to identify the network adapter, ...
    (microsoft.public.windowsce.embedded.vc)