Okay in the classical understanding of the server and the client - we create a socket and listen to a specific port, the client connects to us via our ip and the port that we listen to.

And how do we suppose to do in this case: we have a certain wifi coverage area, there may even be a number of repeaters. In this zone, we have cars - n pieces. Each of the n machines can be either a server or a client. And the client when he wants to connect to the server, he chooses a server from a variety of servers that exist within the coverage of wifi - which work in the local network.

The question is really bloated to the level of "we have wifi coverage", not necessarily wifi, maybe even ethernet, the main thing is that they are all on the same local network.

UPD

I specify at the request of the user Alexey : the client has no idea what server it is connected to, it does not have a port and ip address. It simply scans the local network for the presence of servers, and connects, the servers themselves may have, say, a name.

  • Clarify the question. It is not entirely clear what exactly you want to learn / implement. Strictly speaking, if there are many client servers on the network this does not in any way change the “classical understanding”. - Alexey
  • @Alexey added a question - ParanoidPanda
  • So, in general, every machine on the network initially does not know about the others. But does he know (or is he just going to find out?) All the hosts on the current local network, and not all of the list are “our” machines? If the "server" is still yours and you determine the port of communication with clients, then each potential client obtains a list of hosts in turn polling them to a known port specifying whether it is the server or not. Those who are not a server are excluded from the list. - Alexey
  • If the situation is such that there are several machines on the network, but they do not know about the existence of each other, they don’t know the connection ports, then I see only one option: each server of yours throws a broadcast UDP packet with a certain frequency informing that it is a server and what to connect to it at such an address and such a port. - Alexey

1 answer 1

It is not very clear why the question is placed in the C # category and what, in fact, it is, but I will try to guess.

Do you want to be able to connect to a previously unknown server host port? Usually, broadcast-UDP packets are used for such needs. The operation scheme is as follows: servers listen on a predetermined (and known to the client) UDP port (for example, 15000), clients listen for answers on another port (alternatively, dynamically selectable - not the essence, for example, 15001). The client sends a broadcast UPD packet to this port, indicating its "reverse" port:

 UdpClient client = new UdpClient(); IPEndPoint ip = new IPEndPoint(IPAddress.Broadcast, 15000); byte[] bytes = Encoding.ASCII.GetBytes("15001"); client.Send(bytes, bytes.Length, ip); client.Close(); 

The server, having received the packet, immediately sends a response to the return address, in which it already indicates its "work" address and port, and, if necessary, any service information.

The client immediately after sending starts some time (for example, a minute) to listen to the answer (s) on the return port. Having received the answers, he chooses the address he needs by some principle (for example, response time, server load, etc.) and then begins the usual interaction.

  • Simply implementation is written in C #. That is, in essence, when a client, at the moment when it is ready to receive a list of servers, throws a broadcast packet, and then, based on the responses of the servers that gave the answer, it is possible to form a list of servers in this local network. - ParanoidPanda