/// <summary> /// 获取指定主机的 Socket 连接 /// </summary> /// <param name="host"></param> /// <param name="port"></param> /// <param name="trafficControl"></param> /// <returns></returns> public static SmsSocket Get(string host, int port, int trafficControl) { var key = host + port; if (sockets.ContainsKey(key)) { return(sockets[key]); } lock (locker) { if (sockets.ContainsKey(key)) { return(sockets[key]); } var socket = new SmsSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, trafficControl); socket.Connect(host, port); sockets.Add(key, socket); if (_log != null) { _log.InfoFormat("{0}-{1}:初始化连接", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"), key); } return(socket); } }
/// <summary> /// 连接断开后只能重连 /// </summary> /// <param name="socket"></param> /// <returns></returns> public static SmsSocket Reconnect(SmsSocket socket) { var newSocket = new SmsSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, socket.TrafficControl) { KeepAlive = socket.KeepAlive }; newSocket.Connect(socket.RemoteEndPoint); sockets[socket.RemoteEndPoint.ToString()] = newSocket; return(newSocket); }
/// <summary> /// 连接断开后只能重连 /// </summary> /// <param name="socket"></param> /// <returns></returns> public static SmsSocket Reconnect(SmsSocket socket) { lock (locker) { if (IsSocketConnected(socket)) { return(socket); } else { int traffic = socket.TrafficControl; var endpoint = socket.RemoteEndPoint; var newSocket = new SmsSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, traffic) { KeepAlive = socket.KeepAlive }; //连接之前断开之前的连接释放资源 socket.Shutdown(SocketShutdown.Both); socket.Disconnect(true); socket.Close(); newSocket.Connect(endpoint); if (_log != null) { _log.InfoFormat("{0}:{1}重新建立连接", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"), endpoint.ToString()); } sockets[endpoint.ToString()] = newSocket; return(newSocket); } } //if (!IsSocketConnected(socket)) //{ // int traffic = socket.TrafficControl; // var endpoint = socket.RemoteEndPoint; // var newSocket = new SmsSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, traffic) // { // KeepAlive = socket.KeepAlive // }; // //socket.Shutdown(SocketShutdown.Both); // //socket.Disconnect(true); // //socket.Close(); // //newSocket.Connect(socket.RemoteEndPoint); // newSocket.Connect(endpoint); // if (_log != null) // { // _log.InfoFormat("{0}:{1}重新建立连接", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"), endpoint.ToString()); // } // sockets[endpoint.ToString()] = newSocket; // return newSocket; //} //return socket; }
/// <summary> /// 获取指定主机的 Socket 连接 /// </summary> /// <param name="host"></param> /// <param name="port"></param> /// <param name="trafficControl"></param> /// <returns></returns> public static SmsSocket Get(string host, int port, int trafficControl) { var key = host + port; if (sockets.ContainsKey(key)) { return(sockets[key]); } lock (locker) { if (sockets.ContainsKey(key)) { return(sockets[key]); } var socket = new SmsSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, trafficControl); socket.Connect(host, port); sockets.Add(key, socket); return(socket); } }