/// <summary> /// Starts the server listening for incoming connection requests. /// </summary> /// <param name="port">Port where the server will listen for connection requests.</param> //internal void Start(Int32 port) internal void Start(List <Tracker.TcpServer.TcpServerSAEA.Model.ProtocolNPort> ProtocolNPort, int numOfConnInPool = 0) { try { log.InfoFormat("{0}/Start", _fileNm); //// Get host related information. //IPAddress[] addressList = Dns.GetHostEntry(Environment.MachineName).AddressList; listenSockets = new List <Socket>(); foreach (var pNP in ProtocolNPort) { try { int port = Convert.ToInt32(pNP.Value); // Create the socket which listens for incoming connections. var _listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _listenSocket.ReceiveBufferSize = this.bufferSize; _listenSocket.SendBufferSize = this.bufferSize; // TODO implementation for IPv6 //if (localEndPoint.AddressFamily == AddressFamily.InterNetworkV6) //{ // // Set dual-mode (IPv4 & IPv6) for the socket listener. // // 27 is equivalent to IPV6_V6ONLY socket option in the winsock snippet below, // // based on http://blogs.msdn.com/wndp/archive/2006/10/24/creating-ip-agnostic-applications-part-2-dual-mode-sockets.aspx // this.listenSocket.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)27, false); // this.listenSocket.Bind(new IPEndPoint(IPAddress.IPv6Any, localEndPoint.Port)); //} _listenSocket.Bind(new IPEndPoint(IPAddress.Any, port)); // Start the server. _listenSocket.Listen(this.numConnections); listenSockets.Add(_listenSocket); log.DebugFormat("{0}/Start at Port: {1} is for Protocol: {2}", _fileNm, pNP.Value, pNP.Key); ProtocolServerData.SaveData(pNP.Key, port, "STARTED", string.Format("Started for: {0}", pNP.Key)); // Post accepts on the listening socket. this.StartAccept(_listenSocket, null, pNP.Key, port); } catch (Exception innerEx) { ProtocolServerData.SaveData(pNP.Key, Convert.ToInt32(pNP.Value), "Error", string.Format("{0}", innerEx)); log.ErrorFormat("{0}/Start InnerException {1}", _fileNm, innerEx); Console.WriteLine("{0}/Start InnerException {1}", _fileNm, innerEx); } } // Blocks the current thread to receive incoming messages. mutex.WaitOne(); } catch (Exception ex) { log.ErrorFormat("{0}/Start {1}", _fileNm, ex); Console.WriteLine("{0}/Start {1}", _fileNm, ex); } }
public void Start(string protocolName, IPAddress iPAddress, int port) { log.InfoFormat("{0}/Start:", _fileNm); this.ProtocolName = protocolName; this.Port = port; log.DebugFormat("{0}/Start: ProtocolName: {1}, Port: {2}", _fileNm, this.ProtocolName, this.Port); //Type.GetType("Tracker.Protocol.***Protocol, Tracker.Protocol"); Type t = Type.GetType("Tracker.Parser." + this.ProtocolName + "Protocol, Tracker.Protocol"); if (t != null) { log.InfoFormat("{0}/Start: Known ProtocolName", _fileNm); this.ProtocolParser = (Tracker.Parser.Protocol)Activator.CreateInstance(t); } else { log.InfoFormat("{0}/Start: Unknown ProtocolName", _fileNm); this.ProtocolParser = new Tracker.Parser.UnknownProtocol(); } // Create a TCP/IP socket. try { listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); listener.Bind(new IPEndPoint(iPAddress, port)); listener.Listen(100); ProtocolServerData.SaveData(this.ProtocolName, this.Port, "STARTED", string.Format("Started for: {0}", this.ProtocolParser.GetType().FullName)); while (isListening) { // Set the event to nonsignaled state. allDone.Reset(); // Start an asynchronous socket to listen for connections. log.InfoFormat("{0}/Start: {1}:{2} Waiting for a connection...", _fileNm, this.ProtocolName, this.Port); Console.WriteLine("Waiting for a connection..."); listener.BeginAccept(new AsyncCallback(AcceptCallback), listener); // Wait until a connection is made before continuing. allDone.WaitOne(); } // https://msdn.microsoft.com/en-us/library/system.net.sockets.socket.begindisconnect(v=vs.110).aspx // Release the socket. // Will prevent from connecting new client listener.Close(0); log.DebugFormat("{0}/Start: Server stop command executed", _fileNm); ProtocolServerData.SaveData(this.ProtocolName, this.Port, "CLOSED", null); } catch (Exception ex) { Console.WriteLine(ex); log.ErrorFormat("/Start: {0}", ex); ProtocolServerData.SaveData(this.ProtocolName, this.Port, "ERROR", ex.Message + ex.StackTrace); } }