There are: server and client . When the server receives a message in the check_user branch:

private void ProcessReceive(SocketAsyncEventArgs e) {if (e.SocketError == SocketError.Success) {getalltables(); log = new StreamWriter(new FileStream("log.txt", FileMode.Append, FileAccess.Write, FileShare.ReadWrite)); string str = Encoding.UTF8.GetString(e.Buffer, 0, e.BytesTransferred); string[] tmp = str.Split('+'); Console.WriteLine("{2} msg fr C№{0}({3}): {1}", ClientNumber, str, DateTime.Now.TimeOfDay.ToString().Substring(0,10), (Sock.RemoteEndPoint as IPEndPoint).Address); log.Write("\n"); log.Write("{2} Client msg from Client №{0}({3}): {1}\n", ClientNumber, str, DateTime.Now, (Sock.RemoteEndPoint as IPEndPoint).Address); MemoryStream outtabl = new MemoryStream(); switch (tmp[0].Trim()) { case "check_user": this.resp ="check_user"+"+"+checkUser(tmp[1].Trim().ToString()); SendAsync(this.resp); Console.WriteLine("{2} msg to C№{0}({3}): {1}", ClientNumber, "success query " + resp, DateTime.Now.TimeOfDay.ToString().Substring(0, 10), (Sock.RemoteEndPoint as IPEndPoint).Address); log.Write("{2} Message to Client №{0}({3}): {1}", ClientNumber, "success query check_user:", DateTime.Now, (Sock.RemoteEndPoint as IPEndPoint).Address); 

how do I send the string "resp" to all clients that are online? (list from ClientConnection) If you try to

 foreach(user in onlineusers) { user.sock.SendAsync(this.resp); } 

Throws an error:

An asynchronous socket operation is in progress using this SocketAsyncEventArgs instance

    2 answers 2

    Several steps: 1. Each client must have their own sending queue. This can be implemented, for example, on ActionBlocks. As soon as one shipment is completed, you can proceed to the next. If you do this in parallel, then you can run into an exception (as you did). 2. In this case, it will be most effective for you to create mass bytes (what we send) and shove in a queue to all clients. They will independently send data to each other.

    By the way, SocketAsyncEventArgs cannot work in parallel with two sockets.

      I haven’t written on Sharpe for a long time, but with non-blocking sockets, the thing is, as soon as the client starts reading from the socket, the corresponding event will arrive at the handler and at that moment (in this handler itself) you can write to the socket.

      All clients will be blocked from reading, callbacks on the server will be sequentially called and you will be able to write a message for each one, like the last time I did it in (java.noi).

      This is a very popular idea of ​​processing multiple connections in one thread, so I think there are no features.