Search code examples
c#windowssocketsservice

How to deal with multiple client TCP/IP in C#


I am starting a small file sharing project on a local network. A user must be able to send a sentence (for the moment) to another user of his choice, connected to the same wifi of our house. The problem is that I cannot make a server with several clients, because at any time the server is liable to stop.

For example: imagine that I have three laptops: A, B and C. A is the server and communicates with B and C, and B and C also communicates thanks to A. But I must be able to turn off A but maintain a connection between B and C so they can continue to send sentences to each other.

I have started to create a windows service in C # which will run in the background. But is there any other way than to make A, B and C be both client and server ?

Thanks


Solution

  • As I know there is no limit for a node in network to be a server and at the same time a client. If that system has IP and a port open, it can act like server. It should only listen on that port for incoming requests. Also at the same time it can connect to another server. If system B knows about system C's IP and port, they can communicate with each other. But in real situations like a messaging app, clients should send messages to server cause each time a client connects to a the network, it gets new IP. So if this is not a problem in your network you can do what you want. But if it is, you can define a simple protocol between clients that at the start of the connection, they exchange their IPs so if the server turned off, they can talk to each other.