public bool SendOthers(AsyncUserToken token, byte[] msg) { lock (ClientDictionary) { foreach (KeyValuePair <string, AsyncUserToken> Key in ClientDictionary) { if (token.Socket.RemoteEndPoint.ToString() != Key.Key) { sendingQueue.Add(new MessageData { Message = msg, Token = Key.Value }); } } } return(true); }
private void ProcessAccept(SocketAsyncEventArgs e) { try { SocketAsyncEventArgs readEventArgs = _socketAsyncReceiveEventArgsPool.Pop(); if (readEventArgs != null) { var token = new AsyncUserToken(e.AcceptSocket); readEventArgs.UserToken = token; e.UserToken = token; Interlocked.Increment(ref _connectedSocketCount); ClientDictionary.Add(e.AcceptSocket.RemoteEndPoint.ToString(), token); if (OnConnectedEvent != null) { OnConnectedEvent(token); } Console.WriteLine("Client connection accepted. There are {0} clients connected to the server", _connectedSocketCount); if (!e.AcceptSocket.ReceiveAsync(readEventArgs)) { ProcessReceive(readEventArgs); } } else { Console.WriteLine("There are no more available sockets to allocate."); } } catch (SocketException ex) { AsyncUserToken token = e.UserToken as AsyncUserToken; Console.WriteLine("Error when processing data received from {0}:\r\n{1}", token.Socket.RemoteEndPoint, ex.ToString()); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } // Accept the next connection request. StartAccept(e); }