使用.NET訪問Internet(5) Paul

來源:互聯網
上載者:User
訪問

非同步伺服器通訊端樣本


下面的樣本程式建立一個接收來自用戶端的串連請求的伺服器。該伺服器是用非同步通訊端產生的,因此在等待來自用戶端的串連時不掛起伺服器應用程式的執行。該應用程式接收來自用戶端的字串,在控制台顯示該字串,然後將該字串回顯到用戶端。來自用戶端的字串必須包含字串“<EOF>”,以發出表示訊息結尾的訊號。
 [C#]
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
 
// State object for reading client data asynchronously
public class StateObject {
   public Socket workSocket = null;       // Client  socket.
   public const int BufferSize = 1024;       // Size of receive buffer.
   public byte[] buffer = new byte[BufferSize]; // Receive buffer.
   public StringBuilder sb = new StringBuilder();  // Received data string.
}
 
public class AsynchronousSocketListener {
   
   // Incoming data from client.
   public static string data = null;
 
   // Thread signal.
   public static ManualResetEvent allDone = new ManualResetEvent(false);
 
   public AsynchronousSocketListener() {
   }
 
   public static void StartListening() {
      // Data buffer for incoming data.
      byte[] bytes = new Byte[1024];
 
      // Establish the local endpoint for the  socket.
      //   The DNS name of the computer
      //  running the listener is "host.contoso.com".
      IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
      IPAddress ipAddress = ipHostInfo.AddressList[0];
      IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
 
      // Create a TCP/IP  socket.
      Socket listener = new Socket(AddressFamily.InterNetwork,
         SocketType.Stream, ProtocolType.Tcp );
 
      // Bind the  socket to the local endpoint and listen for incoming connections.
      try {
         listener.Bind(localEndPoint);
         listener.Listen(100);
 
         while (true) {
            // Set the event to  nonsignaled state.
            allDone.Reset();
 
            // Start  an asynchronous socket to listen for connections.
            Console.WriteLine("Waiting for a connection...");
            listener.BeginAccept( 
               new AsyncCallback(AcceptCallback),
               listener );
 
            // Wait until a connection is made before continuing.
            allDone.WaitOne();
         }
 
      } catch (Exception e) {
         Console.WriteLine(e.ToString());
      }
 
      Console.WriteLine("\nHit enter to continue...");
      Console.Read();
      
   }
 
   public static void AcceptCallback(IAsyncResult ar) {
      // Signal the main thread to continue.
      allDone.Set();
 
      // Get the socket that handles the client request.
      Socket listener = (Socket) ar.AsyncState;
      Socket handler = listener.EndAccept(ar);
 
      // Create the state object.
      StateObject state = new StateObject();
      state.workSocket = handler;
      handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0,
         new AsyncCallback(ReadCallback), state);
   }
 
   public static void ReadCallback(IAsyncResult ar) {
      String content = String.Empty;
      
      // Retrieve the state object and the handler socket
      // from the async state object.
      StateObject state = (StateObject) ar.AsyncState;
      Socket handler = state.workSocket;
 
      // Read data from the client socket. 
      int bytesRead = handler.EndReceive(ar);
 
      if (bytesRead > 0) {
         // There  might be more data, so store  the data received so far.
         state.sb.Append(Encoding.ASCII.GetString(
            state.buffer,0,bytesRead));
 
         // Check for end-of-file tag. If  it is not there, read 
         // more data.
         content = state.sb.ToString();
         if (content.IndexOf("<EOF>") > -1) {
            // All the data has been read from the 
            // client. Display it on the console.
            Console.WriteLine("Read {0} bytes from socket. \n Data : {1}",
               content.Length, content );
            // Echo the data back to the client.
            Send(handler, content);
         } else {
            // Not all data received. Get more.
            handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
            new AsyncCallback(ReadCallback), state);
         }
      }
   }
   
   private static void Send(Socket handler, String data) {
      // Convert the string data to byte data using ASCII encoding.
      byte[] byteData = Encoding.ASCII.GetBytes(data);
 
      // Begin sending the data to the remote device.
      handler.BeginSend(byteData, 0, byteData.Length, 0,
         new AsyncCallback(SendCallback), handler);
   }
 
   private static void SendCallback(IAsyncResult ar) {
      try {
         // Retrieve the socket from the state object.
         Socket handler = (Socket) ar.AsyncState;
 
         // Complete sending the data to the remote device.
         int bytesSent = handler.EndSend(ar);
         Console.WriteLine("Sent {0} bytes to client.", bytesSent);
 
         handler.Shutdown(SocketShutdown.Both);
         handler.Close();
 
      } catch (Exception e) {
         Console.WriteLine(e.ToString());
      }
   }
 
 
   public static int Main(String[] args) {
      StartListening();
      return 0;
   }
}
使用 Net 類的最佳做法
下列建議將協助您最有效地使用 System.Net 中包含的類:
  • 儘可能使用 WebRequest 和 WebResponse 而不是類型轉換為子代類。使用 WebRequestWebResponse 的應用程式可以利用新的 網際網路通訊協定 (IP),而不需要進行大範圍的代碼更改。
  • 當使用 System.Net 類編寫運行在伺服器上的 ASP.NET 應用程式時,從效能的角度來看,使用 GetResponse 和 GetResponseStream 的非同步方法呼叫通常更好。
  • 對 Internet 資源開啟的串連數可能對網路效能和輸送量有顯著的影響。預設情況下,System.Net 對每個主機的每個應用程式使用兩個串連。設定應用程式的 ServicePoint 中的 ConnectionLimit 屬性可為特定主機增加此數目。設定 ServicePointManager.DefaultPersistentConnectionLimit 屬性可為所有主機增加此預設值。
  • 當編寫通訊端層級的協議時,請儘可能嘗試使用 TCPClient 或 UDPClient,而不是直接向 Socket 中寫。這兩個用戶端類封裝 TCP 和 UDP 通訊端的建立,而不需要您處理串連的細節。
  • 當訪問要求憑據的網站時,請使用 CredentialCache 類建立憑據的緩衝而不要對每個請求都提供它們。CredentialCache 類搜尋緩衝以尋找要提供給請求的適當憑據,從而使您不必根據統一資源定位器來建立和提供憑據。
總結
    上面是VS.NET中.NET訪問Internet的一些基本概念和程式碼範例(包括訪問Internet的各種方法和步驟),給大家參考一下。有任何建議請MAIL我 paulni@citiz.net(paulni@citiz.net)。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.