I’m trying to write a server-client socket program in C. The objective is for the server to listen on a specific port, but across a range of IP addresses belonging to the same IP subnet. This IP subnet is part of the 127.x.x.x range (not 127.0.0.1 of course).
Couple of points to note:
- This is a stream-based socket, and not Datagram sockets.
- This is not a broadcast address.
- Implementation in C/C++ only on Unix/Linux platform
I do not want to open multiple sockets on the server for each IP address in the range. This is not scalable.
Any help would be ideally appreciated. Is this even feasible?
You can only bind to one address on a single socket. Why can’t you bind to INADDR_ANY and simply reject any packets not bound for your target IPs? Alternatively, you could bind to an arbitrary port and use OS-level magic (e.g. iptables, bpf) to reroute packets destined for those IP/port combinations to your socket.