public static IServer Create(SocketServerConfig config) { try { int port = config.Port; string ip = config.IPAddress; Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); SocketKeepAliveHelper.SetKeepAliveValues(config, listener, config); if (ip == null || ip.Length < 1 || ip == "127.0.0.1") { listener.Bind(new IPEndPoint(IPAddress.Any, port)); } else { listener.Bind(new IPEndPoint(IPAddress.Parse(ip), port)); } return(new SocketServer(listener, config)); } catch (Exception err) { SocketLogMgt.SetLastError(err); return(null); } }
internal SocketWorkerWithLongConnection(int id, Socket socket, SocketServer server) { _server = server; _socket = socket; UpdateActiveDT(); _strSocketID = id.ToString(); _remoteEndPointInfo = _socket.RemoteEndPoint.ToString(); _buffer = new byte[_server.Config.ReceiveRequestBufferSizeKB * 1024]; SocketKeepAliveHelper.SetKeepAliveValues(this, _socket, _server.Config); }
public static IServer Create(int port) { try { SocketServerConfig cfg = new SocketServerConfig(); cfg.Port = port; Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); SocketKeepAliveHelper.SetKeepAliveValues(cfg, listener, cfg); listener.Bind(new IPEndPoint(IPAddress.Any, port)); return(new SocketServer(listener, cfg)); } catch (Exception err) { SocketLogMgt.SetLastError(err); return(null); } }
public static IServer Create(string ip, int port) { if (ip == null || ip.Length < 1 || ip == "127.0.0.1") { return(Create(port)); } try { SocketServerConfig cfg = new SocketServerConfig(); cfg.IPAddress = ip; cfg.Port = port; Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); SocketKeepAliveHelper.SetKeepAliveValues(cfg, listener, cfg); listener.Bind(new IPEndPoint(IPAddress.Parse(ip), port)); return(new SocketServer(listener, cfg)); } catch (Exception err) { SocketLogMgt.SetLastError(err); return(null); } }
/// <summary> /// 20130206 /// To avoid the following exception when HL7GW is behind a NAT/Proxy/Firewall: /// System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine. /// https://groups.google.com/forum/?fromgroups#!topic/google-help-dataapi/5abZAyGUu3A /// http://code.google.com/p/google-gdata/wiki/KeepAliveAndUnderlyingConnectionIsClosed /// http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html#preventingdisconnection /// http://www.codeproject.com/Articles/117557/Set-Keep-Alive-Values /// </summary> /// <param name="obj"></param> /// <param name="socket"></param> /// <param name="cfg"></param> public static void SetKeepAliveValues(object obj, Socket socket, SocketServerConfig cfg) { if (socket == null || cfg == null) { return; } int res; uint time = cfg.KeepAliveTime; uint interval = cfg.KeepAliveInterval; if (cfg.KeepAlive) { res = SocketKeepAliveHelper.SetKeepAliveValues(socket, true, time, interval); SocketLogMgt.SetLog(SocketLogType.Debug, obj, string.Format("SetSocketKeepAlive: Set {0}ms alive every {1}ms, result:{2}", time, interval, res)); } else { res = SocketKeepAliveHelper.SetKeepAliveValues(socket, false, time, interval); SocketLogMgt.SetLog(SocketLogType.Debug, obj, string.Format("SetSocketKeepAlive: Disable, result:{0}", res)); } }