/// <summary> /// 启动服务 /// </summary> public void Start() { if (!IsRunning) { Init(); IsRunning = true; // 创建监听socket _serverSock = new Socket(LocalEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); if (LocalEndPoint.AddressFamily == AddressFamily.InterNetworkV6) { // 配置监听socket为 dual-mode (IPv4 & IPv6) // 27 is equivalent to IPV6_V6ONLY socket option in the winsock snippet below, _serverSock.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)27, false); _serverSock.Bind(new IPEndPoint(IPAddress.IPv6Any, LocalEndPoint.Port)); } else { _serverSock.Bind(LocalEndPoint); } // 开始监听 _serverSock.Listen(this._maxClient); // 在监听Socket上投递一个接受请求。 StartAccept(null); _daemonThread = new DaemonThread(this); Log4Debug("服务器已启动....[" + LocalEndPoint.ToString() + "]"); } }
public void Start(IPEndPoint localEndPoint) { listenSocket = new Socket(localEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); listenSocket.Bind(localEndPoint); listenSocket.Listen(m_numConnections);//m_numConnections--挂起连接队列的最大长度(能接收多少个连接,如果超出则拒绝) Program.Logger.InfoFormat("Start listen socket {0} success", localEndPoint.ToString()); StartAccept(null); m_daemonThread = new DaemonThread(this); }
public void Start(IPEndPoint localEndPoint) { listenSocket = new Socket(localEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); listenSocket.Bind(localEndPoint); listenSocket.Listen(m_numConnections); Program.Logger.InfoFormat("Start listen socket {0} success", localEndPoint.ToString()); //for (int i = 0; i < 64; i++) //不能循环投递多次AcceptAsync,会造成只接收8000连接后不接收连接了 StartAccept(null); m_daemonThread = new DaemonThread(this); }
public void Start(IPEndPoint localEndPoint) { try { listenSocket = new Socket(localEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); listenSocket.Bind(localEndPoint); listenSocket.Listen(m_numConnections); Debug.WriteLog.Log(eLogLevel.ell_Debug, "Start listen socket " + localEndPoint.ToString() + " success"); //for (int i = 0; i < 64; i++) //不能循环投递多次AcceptAsync,会造成只接收8000连接后不接收连接了 StartAccept(null); m_daemonThread = new DaemonThread(this); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
/// <summary> /// 开启服务器,准备接收客户端连接 /// </summary> /// <param name="localEndPoint"></param> public void Start(IPEndPoint localEndPoint) { //绑定操作 #region listenSocket = new Socket(localEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); listenSocket.Bind(localEndPoint); listenSocket.Listen(m_numConnections); Program.Logger.InfoFormat("Start listen socket {0} success", localEndPoint.ToString()); #endregion //for (int i = 0; i < 64; i++) //不能循环投递多次AcceptAsync,会造成只接收8000连接后不接收连接了 StartAccept(null);//开始接收客户端 m_daemonThread = new DaemonThread(this);//检测客户端状态,如果连接超时即关闭 }
static void Main(string[] args) { //DateTime currentTime = DateTime.Now; //log4net.GlobalContext.Properties["LogDir"] = currentTime.ToString("yyyyMM"); //log4net.GlobalContext.Properties["LogFileName"] = "_SocketAsyncServer" + currentTime.ToString("yyyyMMdd"); //Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); //FileDirectory = config.AppSettings.Settings["FileDirectory"].Value; //if (FileDirectory == "") // FileDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Files"); //if (!Directory.Exists(FileDirectory)) // Directory.CreateDirectory(FileDirectory); // authorize by remote server /* * int userID = -1; * if (!(int.TryParse(config.AppSettings.Settings["ServerUserID"].Value, out userID))) * userID = -1; * String authorizeURL = "http://222.175.75.230:8081/OperationUser/ashx/SoftUserLoginHandler.ashx?userid="; * authorizeURL += userID; * string strMsg = ""; * try * { * WebRequest request = WebRequest.Create(authorizeURL); * WebResponse response = request.GetResponse(); * StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312")); * strMsg = reader.ReadToEnd(); * reader.Close(); * reader.Dispose(); * response.Close(); * } * catch * { } * if (strMsg == null || strMsg!="true") * { * System.Environment.Exit(0); * } */ String serverIP = "127.0.0.1"; if (config.AppSettings.Settings["serverIP"].Value != null) { serverIP = config.AppSettings.Settings["serverIP"].Value; } int port = 0; if (!(int.TryParse(config.AppSettings.Settings["Port"].Value, out port))) { port = 9999; } int parallelNum = 0; if (!(int.TryParse(config.AppSettings.Settings["ParallelNum"].Value, out parallelNum))) { parallelNum = 80; } int sleepTime = 0; if (!(int.TryParse(config.AppSettings.Settings["sleepTime"].Value, out sleepTime))) { sleepTime = 10000; } int socketTimeOutMS = 0; if (!(int.TryParse(config.AppSettings.Settings["SocketTimeOutMS"].Value, out socketTimeOutMS))) { socketTimeOutMS = 5 * 60 * 1000; } long start = 0; if (!(long.TryParse(config.AppSettings.Settings["start"].Value, out start))) { start = 0; } long FilterNumber = 0; if (!(long.TryParse(config.AppSettings.Settings["FilterNumber"].Value, out FilterNumber))) { FilterNumber = 0; } int count = 1; List <DaemonThread> list_DaemonThread = new List <DaemonThread>(); List <string> list_dtu_id = new List <string>(); //using (irrigatteEntities entities = new irrigatteEntities()) //{ // try // { // var query = from it in entities.DTU where it.GP_ID == 26 && (it.DTU_ID.StartsWith("811") || it.DTU_ID.StartsWith("900")) select it; // if (query != null && query.FirstOrDefault() != null) // { // count = query.Count(); // foreach(var it in query) // { // list_dtu_id.Add(it.DTU_ID); // } // } // } // catch (Exception ex) // { // Console.WriteLine(ex.ToString()); // } //} for (long i = start; i < start + parallelNum; i++) { if (i % 100 == 0 && i > start) { Thread.Sleep(sleepTime); } //DaemonThread DaemonThread = new DaemonThread(serverIP, port, list_dtu_id.ElementAt(i % count)); DaemonThread DaemonThread = new DaemonThread(serverIP, port, i + "", FilterNumber); list_DaemonThread.Add(DaemonThread); } Console.WriteLine(parallelNum + "个线程加载完成。Press any key to terminate the server process...."); Console.ReadKey(); }