Problem with connecting 2 computers on LAN in C++

Hello fellow programmers! I am writing a program in C++ that is supposed to allow for real time data transfer between 2 computers over LAN using windows sockets. Since the data is supposed to be transferred as fast as possible I use UDP (this is a game, therefore only the newest data is relevant). Now, since I tested it on single machine i use the following code to assign server IP to the client

1
2
3
4
5
6
7
SOCKADDR_IN server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(PORT);
server_address.sin_addr.S_un.S_un_b.s_b1 = 127;
server_address.sin_addr.S_un.S_un_b.s_b2 = 0;
server_address.sin_addr.S_un.S_un_b.s_b3 = 0;
server_address.sin_addr.S_un.S_un_b.s_b4 = 1;


Now, I need it to run within LAN where the exact addresses of server and client will be unknown. How to connect in such a case? Is it enough if I broadcast to 255.255.255.255?
Last edited on
I need it to run within LAN where the exact addresses of server and client will be unknown.

You client must know who is server. either server domain which the client resolves or fixed address.

you don't want you client to sniff entry local network for open ports or server services.
Yeah, I understand that it is not really safe to broadcast to all the net. However, this is part of an assignment I got, where I am told to not to hardwire IP addresses. Is there a simple way to avoid it?
I guess the most normal way to do it by using multicast DNS.
When an mDNS client needs to resolve a hostname, it sends an IP multicast query message that asks the host having that name to identify itself. That target machine then multicasts a message that includes its IP address.

https://en.wikipedia.org/wiki/Multicast_DNS

I never worked with mDNS, but if I had to I would surely use it as that quote does what you need, assuming your client will need to know server computer name on local network.

there may be other ways, I don't know but client not knowing to where to connect just doesn't sound very good.
Last edited on
However, this is part of an assignment I got, where I am told to not to hardwire IP addresses.
What that means is that you're supposed to accept the IP from the user, either directly or from a configuration file, not that you have to broadcast to the entire network.
Topic archived. No new replies allowed.