I was told that I can send broadcast message
That is one way to do it, yes (it is not the only way). However, that only works if the client PC and server PC are connected to the same network.
The server app can open two listening sockets, one for TCP and one for UDP (IPv4 only) or Multicast (IPv4 or IPv6). IPv6 does not support UDP broadcasts, so you have to use Multicast instead.
The client app can then:
for UDP, send a message to the UDP port using the broadcast IPv4 address of the subnet that it is connected to. The message will be delivered to every device connected to that subnet. If the server PC is connected to the same subnet and the server app receives the broadcast message, it can send a reply back to the IPv4 address that sent the broadcast. When the client app receives the reply, it will know the IPv4 address that sent the reply.
for Multicast, subscribe to the server PC's Multicast group address. The server can periodically send its current IP address to the group, and any clients that are subscribed to the same group will receive it. When the client receives the message, it will know the IP address that sent the message.
Once the client has discovered the server's IP address, it can connect its TCP client socket to that IP address using the TCP port.
If the two PCs are not on the same network (ie, they are connecting over the Internet), then the above does not apply. The server will have to publish its current IP address somewhere that the client can find it, such as on a public website, or a Dynamic DNS service.
Of course, the simplest solution is to just ask the server admin for the server's current TCP IP/port, and then you can enter that into your client app's configuration as needed.
I tried to write it like this (c++ builder 6):
ClientSocket->Socket->SendText("Message");
TClientSocket
uses TCP/IP. To send a subnet broadcast using UDP, you need to use UDP component instead. C++Builder 6 ships with FastNet, which has a TNMUDP
component, as well as Indy, which has TIdUDPClient
and TIdUDPServer
components. Or, you can write your own UDP socket code using the WinSock API directly.
Indy also provides Multicast
The address and host is 192.168.0.255
That is the broadcast IP address for an IPv4 192.168.0.x subnet that has a 255.255.255.0 subnet mask. If that is your actual subnet configuration, and your client and server PCs are both connected to the 192.168.0.x subnet, then yes, you can send a UDP broadcast message to that IP address.
To obtain the subnet broadcast IP address, you can either:
calculate it manually. Use GetAdaptersInfo()
or GetAdaptersAddresses()
to get the local PC's current IPv4 address and subnet mask, then mask the bits of the IP address with the bits of the subnet mask using an AND
operator, and then OR
the result with the inverse bits of the network mask.
ask Windows. Create a socket and bind()
it to the desired local network adapter. and then use WSAIoctl(SIO_GET_BROADCAST_ADDRESS)
to query the broadcast IP address associated with that network.