brenden
brenden

Reputation: 604

C Find Computer on LAN With Open Server

Im trying to figure out a way for one user to open a server, on a port of their choice, and for a user on the same LAN, to be able to see that server without manually putting in the ip and port. Think like when you open a minecraft LAN server, and people on the LAN can see the server, even though they never put in the ip, and the port is different every time. Is there a way to do this in c on linux with the Berkley Sockets API, with TCP sockets?

Upvotes: 2

Views: 101

Answers (1)

Addison
Addison

Reputation: 8347

There's plenty of articles out there on how to use Unix built-in's to scan your Local Area Network for other hosts.

You can find your own IP address and Network using ifconfig, like in this article.

It's then a simple task to get a list of IP's/hostnames on your LAN using a command like nmap, and then sort through the output.

Here's an example:

sudo nmap -sn 10.84.32.54/24 | grep -Po "(\d{1,3}\.){3}\d{1,3}"

Which will return you a list of ip-addresses on your network like:

10.84.32.1
10.84.32.11
10.84.32.12
10.84.32.14
10.84.32.17

You could then go about scan each of the ports on these by running:

nmap -p- 10.84.32.1

To get an output like:

PORT     STATE SERVICE
22/tcp   open  ssh
2581/tcp open  unknown
2443/tcp open  unknown

Upvotes: 1

Related Questions