TcpClient.Connect函数连接超时的问题(转载)
TcpClient.Connect函數連接超時的問題
問題原述:
http://topic.csdn.net/t/20060616/15/4825920.html
調用TcpClient.Connect函數連接其他機器。我在一臺機器上測試程序,對于連接根本無法連接(物理連接不通)的機器,該函數用時5秒左右返回,并捕獲SocketException異常。我在另一臺機器上測試時,Connect函數用時26秒左右的時間才返回。請問有沒有方法設置Connect函數連接超時的時間,如果超過一定時間還沒有連上則返回。
解決方法:
?? 寫了個幫助類,用了線程池 ?
? ?
? class ? TcpClientConnector ?
? { ?
? ? ? ? ? /// ? <summary> ?
? ? ? ? ? /// ? 在指定時間內嘗試連接指定主機上的指定端口。 ?
? ? ? ? ? /// ? </summary> ?
? ? ? ? ? /// ? <param ? name="hostname">要連接到的遠程主機的 ? DNS ? 名。</param> ?
? ? ? ? ? /// ? <param ? name="port">要連接到的遠程主機的端口號。</param> ?
? ? ? ? ? /// ? <param ? name="millisecondsTimeout">要等待的毫秒數,或 ? -1 ? 表示無限期等待。</param> ?
? ? ? ? ? /// ? <returns>已連接的一個 ? TcpClient ? 實例。</returns> ?
? ? ? ? ? /// ? <remarks>本方法可能拋出的異常與 ? TcpClient ? 的構造函數重載之一 ?
? ? ? ? ? /// ? public ? TcpClient(string, ? int) ? 相同,并若指定的等待時間是個負數且不等于 ?
? ? ? ? ? /// ? -1,將會拋出 ? ArgumentOutOfRangeException。</remarks> ?
? ? ? ? ? public ? static ? TcpClient ? Connect(string ? hostname, ? int ? port, ? int ? millisecondsTimeout) ?
? ? ? ? ? { ?
? ? ? ? ? ? ? ? ? ConnectorState ? cs ? = ? new ? ConnectorState(); ?
? ? ? ? ? ? ? ? ? cs.Hostname ? = ? hostname; ?
? ? ? ? ? ? ? ? ? cs.Port ? = ? port; ?
? ? ? ? ? ? ? ? ? ThreadPool.QueueUserWorkItem(new ? WaitCallback(ConnectThreaded), ? cs); ?
? ? ? ? ? ? ? ? ? if ? (cs.Completed.WaitOne(millisecondsTimeout, ? false)) ?
? ? ? ? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? ? ? ? ? ? if ? (cs.TcpClient ? != ? null) ? return ? cs.TcpClient; ?
? ? ? ? ? ? ? ? ? ? ? ? ? throw ? cs.Exception; ?
? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? ? else ?
? ? ? ? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? ? ? ? ? ? cs.Abort(); ?
? ? ? ? ? ? ? ? ? ? ? ? ? throw ? new ? SocketException(11001); ? // ? cannot ? connect ?
? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? } ?
? ?
? ? ? ? ? private ? static ? void ? ConnectThreaded(object ? state) ?
? ? ? ? ? { ?
? ? ? ? ? ? ? ? ? ConnectorState ? cs ? = ? (ConnectorState)state; ?
? ? ? ? ? ? ? ? ? cs.Thread ? = ? Thread.CurrentThread; ?
? ? ? ? ? ? ? ? ? try ?
? ? ? ? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? ? ? ? ? ? TcpClient ? tc ? = ? new ? TcpClient(cs.Hostname, ? cs.Port); ?
? ? ? ? ? ? ? ? ? ? ? ? ? if ? (cs.Aborted) ?
? ? ? ? ? ? ? ? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? try ? { ? tc.GetStream().Close(); ? } ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? catch ? { ? } ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? try ? { ? tc.Close(); ? } ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? catch ? { ? } ?
? ? ? ? ? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? ? ? ? ? ? else ?
? ? ? ? ? ? ? ? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cs.TcpClient ? = ? tc; ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cs.Completed.Set(); ?
? ? ? ? ? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? ? catch ? (Exception ? e) ?
? ? ? ? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? ? ? ? ? ? cs.Exception ? = ? e; ?
? ? ? ? ? ? ? ? ? ? ? ? ? cs.Completed.Set(); ?
? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? } ?
? ?
? ? ? ? ? private ? class ? ConnectorState ?
? ? ? ? ? { ?
? ? ? ? ? ? ? ? ? public ? string ? Hostname; ?
? ? ? ? ? ? ? ? ? public ? int ? Port; ?
? ? ? ? ? ? ? ? ? public ? volatile ? Thread ? Thread; ?
? ? ? ? ? ? ? ? ? public ? readonly ? ManualResetEvent ? Completed ? = ? new ? ManualResetEvent(false); ?
? ? ? ? ? ? ? ? ? public ? volatile ? TcpClient ? TcpClient; ?
? ? ? ? ? ? ? ? ? public ? volatile ? Exception ? Exception; ?
? ? ? ? ? ? ? ? ? public ? volatile ? bool ? Aborted; ?
? ? ? ? ? ? ? ? ? public ? void ? Abort() ?
? ? ? ? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? ? ? ? ? ? if ? (Aborted ? != ? true) ?
? ? ? ? ? ? ? ? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Aborted ? = ? true; ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? try ? { ? Thread.Abort(); ? } ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? catch ? { ? } ?
? ? ? ? ? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? } ?
? } ?
? ?
? ================================= ?
? 用法示例: ?
? ?
? try ?
? { ?
? ? ? ? ? Console.WriteLine("Connecting ? to ? nonexistenthost..."); ?
? ? ? ? ? TcpClient ? tc ? = ? TcpClientConnector.Connect("nonexistent", ? 80, ? 1000); ?
? ? ? ? ? Console.WriteLine("Returned"); ?
? ? ? ? ? try ? { ? tc.GetStream().Close(); ? } ?
? ? ? ? ? catch ? { ? } ?
? ? ? ? ? try ? { ? tc.Close(); ? } ?
? ? ? ? ? catch ? { ? } ?
? } ?
? catch ? (Exception ? e) ?
? { ?
? ? ? ? ? Console.WriteLine("Exception: ? " ? + ? e.Message); ?
? }??
轉載于:https://www.cnblogs.com/sql4me/archive/2009/04/29/1446080.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的TcpClient.Connect函数连接超时的问题(转载)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle动态性能视图和静态,orac
- 下一篇: Oracle 数据定义语言,oracle