/// <summary> /// Function to stop the SocketServer. It can be restarted with Start /// </summary> public void Stop() { // Abort the accept thread if (this.acceptThread != null) { this.tcpListener.Stop(); this.acceptThread.Join(); this.acceptThread = null; } // Dispose of all of the socket connections for (int iSocket = 0; iSocket < this.socketClientList.Count; ++iSocket) { SocketClient socket = (SocketClient)socketClientList[iSocket]; socketClientList.Remove(socket); socket.Dispose(); } // Wait for all of the socket client objects to be destroyed //GC.Collect(); //GC.WaitForPendingFinalizers(); // Clear the Handler Functions this.messageHandler = null; this.acceptHandler = null; this.closeHandler = null; this.errorHandler = null; // Clear the buffer size and user arguments this.sizeOfRawBuffer = 0; this.userArg = null; }
/// <summary> /// Notifies connected clients of new alert from system /// </summary> /// <param name="data"></param> public int NotifyConnectedClients(Byte[] rawBuffer) { int count = 0; ArrayList ObjectsToRemove = null; for (int x = 0; x < this.socketClientList.Count; x++) { try { SocketClient socket = (SocketClient)this.socketClientList[x]; if (socket.ClientSocket.Connected == true && socket.Send(rawBuffer) == true) { count++; } else { if (ObjectsToRemove == null) { ObjectsToRemove = new ArrayList(); } ObjectsToRemove.Add(socket); } } catch (Exception e) { Console.WriteLine("Error:SocketServer: While in NotifyConnectedClients" + e.Message); //System.Diagnostics.Debugger.Break(); } } if (ObjectsToRemove != null) { foreach (SocketClient socket in ObjectsToRemove) { socket.Disconnect(); socketClientList.Remove(socket); socket.Dispose(); } } return(count); }