Question

I'm trying to write a function that can get me the IP adress (and the name of the device as bonus) of devices that are in my network, the network is gonna be a direct connection between two computers using Ethernet cable or creating an access point (using wi-fi)

I tried to search about how to do it but it seems like I need to listen to the network or something which seems to be difficult.

hope you can guide me to what I should do or read to get started.

Note: I'm using Windows on both computers.

Edited: P.S: I need the IP Address so I can send a message to the other computer using winsock in a Client/Server program I wrote. can't I make the server send its IP to the client or the opposite ?

Was it helpful?

Solution

If your software is running on both machines, you can have one (or both) machines send out a particular broadcast (or, if you prefer, multicast) UDP packet on a specific port. Your program should also be listening on that same port. When it receives that packet (using recvfrom()), recvfrom()'s fifth argument will contain the IP address of the machine that sent the packet, i.e. the IP address you want.

(If OTOH your software is not running on the remote machine, you'll need to use some more general-purpose discovery mechanism such as mDNS or LLDP -- hopefully you won't have to do that, though, as it's a good deal more complicated)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top