I’m developing a web application which waits for N (say, around 5) users to connect to a webservice via a TCP socket. The server keeps track of the number of sockets connected, and once it reaches N he then sends a (single) file, say up to 2MB, to all of them simultaneously. The emphasis here is on timing – they should all receive the file at the same time, rather than one after the other.
As a newbie to c# and socket programming, I wish to know what would be the best approach in this case?
I thought of the following:
The server (asynchronously) receives socket connections and once it reaches N, loops through all of the bytes in the file, and on each iteration sends a single Kbyte (or maybe less?) to each one of the clients connected – this, in my opinion, is the closest way to sending a file simultaneously.
I’m kind of confused about how to implement this.
Any help would be great.
Thanks
EDIT:
by receiving at the same time I meant, technically, that I don’t want the server sending the file to the first client, then to the second, then to the third etc, but rather send it to all of them at once, once it’s out of the server, it’s out of my hands! Assuming there are no network problems, and the clients are all very near (physically), they should receive the file more or less at the same time.
maybe the solution is simpler than I thought…
You already plan to accept socket connections asynchronously and since a callback occurs on a separate thread, you could simply have an async callback that:
If multiple clients connect at once, this will result in multiple threads sending the file concurrently, and almost at the same time.
Your file may be accessed by multiple threads at once, so you’ll have to for instance open it with
FileShare.Read.Hope this helps, I ignored the “N simultaneous clients” as I understand this is not really a requirement.