Re: How to know if a network adapter is connected
- From: "Helge Kruse" <Helge.Kruse-nospam@xxxxxxx>
- Date: Mon, 13 Oct 2008 17:53:58 +0200
"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:No. You don't do it in your excerpt.
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().
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
.
- References:
- How to know if a network adapter is connected
- From: fred_d
- Re: How to know if a network adapter is connected
- From: Helge Kruse
- Re: How to know if a network adapter is connected
- From: fred_d
- Re: How to know if a network adapter is connected
- From: fred_d
- Re: How to know if a network adapter is connected
- From: Helge Kruse
- Re: How to know if a network adapter is connected
- From: fred_d
- How to know if a network adapter is connected
- Prev by Date: WSAAsyncSelect indicates FD_ACCEPT on connection close
- Next by Date: Re: How to know if a network adapter is connected
- Previous by thread: Re: How to know if a network adapter is connected
- Next by thread: Re: How to know if a network adapter is connected
- Index(es):
Relevant Pages
|