在C#中帶連線逾時功能的TcpClient類(翻譯自MSDN論壇CMF)

來源:互聯網
上載者:User

關於TcpClient 類在C#中對於操作TCP connection是非常方便的,非常地好!

但是有一點就是,這個類對於CONNECT操作沒有設定逾時!

系統預設的是60秒的逾時,這明顯過於地長。

我們可以重新去用Thread的join這個帶參數的線程,來解決這個問題,下面的這個類就是但連線逾時參數的TCPCliento類

 

the TcpClientWithTimeout.cs class:

using System;using System.Net.Sockets;using System.Threading;/// <summary>/// TcpClientWithTimeout 用來設定一個帶連線逾時功能的類
/// 使用者可以設定毫秒級的等待逾時時間 (1000=1second)/// 例如:/// TcpClient connection = new TcpClientWithTimeout('127.0.0.1',80,1000).Connect();/// </summary>public class TcpClientWithTimeout{ protected string _hostname; protected int _port; protected int _timeout_milliseconds; protected TcpClient connection; protected bool connected; protected Exception exception; public TcpClientWithTimeout(string hostname,int port,int timeout_milliseconds) { _hostname = hostname; _port = port; _timeout_milliseconds = timeout_milliseconds; } public TcpClient Connect() { // kick off the thread that tries to connect connected = false; exception = null; Thread thread = new Thread(new ThreadStart(BeginConnect)); thread.IsBackground = true; // 作為後台線程處理
// 不會佔用機器太長的時間
thread.Start(); // 等待如下的時間
thread.Join(_timeout_milliseconds); if (connected == true) { // 如果成功就返回TcpClient對象 thread.Abort(); return connection; } if (exception != null) { // 如果失敗就拋出錯誤 thread.Abort(); throw exception; } else { // 同樣地拋出錯誤 thread.Abort(); string message = string.Format("TcpClient connection to {0}:{1} timed out", _hostname, _port); throw new TimeoutException(message); } } protected void BeginConnect() { try { connection = new TcpClient(_hostname, _port); // 標記成功,返回調用者 connected = true; } catch (Exception ex) { // 標記失敗 exception = ex; } }}

下面的這個例子就是如何利用5秒的逾時,去串連一個網站發送10位元組的資料,並且接收10位元組的資料。

// connect with a 5 second timeout on the connection
TcpClient connection = new TcpClientWithTimeout("www.google.com", 80, 5000).Connect();
NetworkStream stream = connection.GetStream();

// Send 10 bytes
byte[] to_send = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xa};
stream.Write(to_send, 0, to_send.Length);

// Receive 10 bytes
byte[] readbuf = new byte[10]; // you must allocate space first
stream.ReadTimeout = 10000; // 10 second timeout on the read
stream.Read(readbuf, 0, 10); // read

// Disconnect nicely
stream.Close();      // workaround for a .net bug: http://support.microsoft.com/kb/821625
connection.Close();

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.