/// <summary> Function to start the SocketServer </summary> /// <param name="ipAddress"> The IpAddress to listening on </param> /// <param name="port"> The Port to listen on </param> /// <param name="sizeOfRawBuffer"> Size of the Raw Buffer </param> /// <param name="sizeOfByteBuffer"> Size of the byte buffer </param> /// <param name="userArg"> User supplied arguments </param> /// <param name="messageHandler"> Function pointer to the user MessageHandler function </param> /// <param name="acceptHandler"> Function pointer to the user AcceptHandler function </param> /// <param name="closeHandler"> Function pointer to the user CloseHandler function </param> /// <param name="errorHandler"> Function pointer to the user ErrorHandler function </param> public void Start(string ipAddress, int port, int sizeOfRawBuffer, int sizeOfByteBuffer, object userArg, MESSAGE_HANDLER messageHandler, ACCEPT_HANDLER acceptHandler, CLOSE_HANDLER closeHandler, ERROR_HANDLER errorHandler) { // Is an AcceptThread currently running if (AcceptThread == null) { // Set connection values IpAddress = ipAddress; Port = port; // Save the Handler Functions MessageHandler = messageHandler; AcceptHandler = acceptHandler; CloseHandler = closeHandler; ErrorHandler = errorHandler; // Save the buffer size and user arguments SizeOfRawBuffer = sizeOfRawBuffer; SizeOfByteBuffer = sizeOfByteBuffer; UserArg = userArg; // Start the listening thread if one is currently not running ThreadStart tsThread = new ThreadStart(AcceptConnections); AcceptThread = new Thread(tsThread); AcceptThread.Name = string.Format("SocketAccept-{0}", ipAddress); AcceptThread.Start(); } }
/// <summary> /// Starts the SocketServer /// </summary> /// <param name="ipAddress">IP Address to listen on</param> /// <param name="port">Port to listen on</param> /// <param name="maxClientConnections">Maximum number of client connections</param> /// <param name="bufferSize">Size of incoming data buffer</param> /// <param name="userArg">(Optional) User supplied args. If not in use, pass in null</param> /// <param name="msgHandler">Pointer to custom MessageHandler function</param> /// <param name="acptHandler">Pointer to custom AcceptHandler function</param> /// <param name="clsHandler">Pointer to custom CloseHAndler function</param> /// <param name="errHandler">Pointer to custom ErrorHandler function</param> public void Start(string ipAddress, short port, int maxClientConnections, int bufferSize, Object userArg, MESSAGE_HANDLER msgHandler, ACCEPT_HANDLER acptHandler, CLOSE_HANDLER clsHandler, ERROR_HANDLER errHandler) { if (acceptThread == null) { this.ipAddress = ipAddress; this.port = port; this.maxClientConnections = maxClientConnections; socketClientList = new SocketClient[maxClientConnections]; this.bufferSize = bufferSize; this.userArg = userArg; messageHandler = msgHandler; acceptHandler = acptHandler; closeHandler = clsHandler; errorHandler = errHandler; ThreadStart ts = new ThreadStart(AcceptThread); acceptThread = new Thread(ts) { Name = "Accept" }; acceptThread.Start(); } }
/// <summary> Function to stop the SocketServer. It can be restarted with Start </summary> public void Stop() { // Abort the accept thread if (AcceptThread != null) { TcpListener.Stop(); AcceptThread.Join(); AcceptThread = null; } lock (SocketClientList) { // Dispose of all of the socket connections foreach (SocketClient socketClient in SocketClientList) { socketClient.Dispose(); } } // Wait for all of the socket client objects to be destroyed GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); // Empty the Socket Client List SocketClientList.Clear(); // Clear the Handler Functions MessageHandler = null; AcceptHandler = null; CloseHandler = null; ErrorHandler = null; // Clear the buffer size and user arguments SizeOfRawBuffer = 0; UserArg = null; }