public void ConnectService() { int retryTimes = 0; label_ConnectService: connectServiceThreadBlock.WaitOne(); serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { IPAddress ip = IPAddress.Any; IPEndPoint endPoint = new IPEndPoint(ip, serverContext.port); serverSocket.Bind(endPoint); serverSocket.Listen(10); while (true) { Socket socket = serverSocket.Accept(); ServerUserContext serverUserContext = new ServerUserContext(); serverUserContext.socket = socket; var testEndPoint = socket.RemoteEndPoint as IPEndPoint; var testEndPoint2 = socket.LocalEndPoint as IPEndPoint; if (testEndPoint.Address.Equals(testEndPoint2.Address)) { serverUserContext.authority = int.MaxValue; } Task.Factory.StartNew(UserConnectService, serverUserContext, TaskCreationOptions.LongRunning); } } catch (Exception e) { serverContext.Log(e); Thread.Sleep(retryTimes < 10 ? 500 : 5000); retryTimes++; goto label_ConnectService; } finally { serverSocket.Close(); } return; }
public void UserConnectService(object _serverUserContext) { ServerUserContext serverUserContext = (ServerUserContext)_serverUserContext; ref Socket socket = ref serverUserContext.socket;