欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)多線程的Web代理服務(wù)器實(shí)例

 更新時(shí)間:2015年07月15日 18:06:06   作者:紅薯  
這篇文章主要介紹了C#實(shí)現(xiàn)多線程的Web代理服務(wù)器,涉及C#多線程代理服務(wù)器的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了C#實(shí)現(xiàn)多線程的Web代理服務(wù)器。分享給大家供大家參考。具體如下:

/**
Proxy.cs:
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// Proxy.cs -- Implements a multi-threaded Web proxy server
//
//    Compile this program with the following command line:
//     C:>csc Proxy.cs
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;
using System.Threading;
namespace nsProxyServer
{
 public class ProxyServer
 {
  static public void Main (string [] args)
  {
   int Port = 3125;
   if (args.Length > 0)
   {
    try
    {
     Port = Convert.ToInt32 (args[0]);
    }
    catch
    {
     Console.WriteLine ("Please enter a port number.");
     return;
    }
   }
   try
   {
    // Create a listener for the proxy port
    TcpListener sockServer = new TcpListener (Port);
    sockServer.Start ();
    while (true)
    {
     // Accept connections on the proxy port.
     Socket socket = sockServer.AcceptSocket ();
     // When AcceptSocket returns, it means there is a connection. Create
     // an instance of the proxy server class and start a thread running.
     clsProxyConnection proxy = new clsProxyConnection (socket);
     Thread thrd = new Thread (new ThreadStart (proxy.Run));
     thrd.Start ();
     // While the thread is running, the main program thread will loop around
     // and listen for the next connection request.
    }
   }
   catch (IOException e)
   {
    Console.WriteLine (e.Message);
   }
  }
 }
 class clsProxyConnection
 {
  public clsProxyConnection (Socket sockClient)
  {
   m_sockClient = sockClient;
  }
  Socket m_sockClient; //, m_sockServer;
  Byte [] readBuf = new Byte [1024];
  Byte [] buffer = null;
  Encoding ASCII = Encoding.ASCII;
  public void Run ()
  {
   string strFromClient = "";
   try
   {
    // Read the incoming text on the socket/
    int bytes = ReadMessage (m_sockClient,
           readBuf, ref strFromClient);
    // If it's empty, it's an error, so just return.
    // This will termiate the thread.
    if (bytes == 0)
     return;
    // Get the URL for the connection. The client browser sends a GET command
    // followed by a space, then the URL, then and identifer for the HTTP version.
    // Extract the URL as the string betweeen the spaces.
    int index1 = strFromClient.IndexOf (' ');
    int index2 = strFromClient.IndexOf (' ', index1 + 1);
    string strClientConnection =
      strFromClient.Substring (index1 + 1, index2 - index1);
    if ((index1 < 0) || (index2 < 0))
    {
     throw (new IOException ());
    }
    // Write a messsage that we are connecting.
    Console.WriteLine ("Connecting to Site " +
         strClientConnection);
    Console.WriteLine ("Connection from " +
         m_sockClient.RemoteEndPoint);
    // Create a WebRequest object.
    WebRequest req = (WebRequest) WebRequest.Create
              (strClientConnection);
    // Get the response from the Web site.
    WebResponse response = req.GetResponse ();
    int BytesRead = 0;
    Byte [] Buffer = new Byte[32];
    int BytesSent = 0;
    // Create a response stream object.
    Stream ResponseStream = response.GetResponseStream();
    // Read the response into a buffer.
    BytesRead = ResponseStream.Read(Buffer,0,32);
    StringBuilder strResponse = new StringBuilder("");
    while (BytesRead != 0)
    {
     // Pass the response back to the client
     strResponse.Append(Encoding.ASCII.GetString(Buffer,
          0, BytesRead));
     m_sockClient.Send(Buffer, BytesRead, 0);
     BytesSent += BytesRead;
     // Read the next part of the response
     BytesRead = ResponseStream.Read(Buffer, 0, 32);
    }
   }
   catch (FileNotFoundException e)
   {
    SendErrorPage (404, "File Not Found", e.Message);
   }
   catch (IOException e)
   {
    SendErrorPage (503, "Service not available", e.Message);
   }
   catch (Exception e)
   {
     SendErrorPage (404, "File Not Found", e.Message);
     Console.WriteLine (e.StackTrace);
     Console.WriteLine (e.Message);
   }
   finally
   {
    // Disconnect and close the socket.
    if (m_sockClient != null)
    {
     if (m_sockClient.Connected)
     {
      m_sockClient.Close ();
     }
    }
   }
   // Returning from this method will terminate the thread.
  }
  // Write an error response to the client.
  void SendErrorPage (int status, string strReason, string strText)
  {
   SendMessage (m_sockClient, "HTTP/1.0" + " " +
       status + " " + strReason + "\r\n");
   SendMessage (m_sockClient, "Content-Type: text/plain" + "\r\n");
   SendMessage (m_sockClient, "Proxy-Connection: close" + "\r\n");
   SendMessage (m_sockClient, "\r\n");
   SendMessage (m_sockClient, status + " " + strReason);
   SendMessage (m_sockClient, strText);
  }
  // Send a string to a socket.
  void SendMessage (Socket sock, string strMessage)
  {
   buffer = new Byte [strMessage.Length + 1];
   int len = ASCII.GetBytes (strMessage.ToCharArray(),
          0, strMessage.Length, buffer, 0);
   sock.Send (buffer, len, 0);
  }
  // Read a string from a socket.
  int ReadMessage (Socket sock, byte [] buf, ref string strMessage)
  {
   int iBytes = sock.Receive (buf, 1024, 0);
   strMessage = Encoding.ASCII.GetString (buf);
   return (iBytes);
  }
 }
}

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • C#比較時(shí)間大小的方法總結(jié)

    C#比較時(shí)間大小的方法總結(jié)

    在本篇內(nèi)容里小編給大家分享的是關(guān)于C#比較時(shí)間大小的方法總結(jié),對(duì)此有需要的朋友們可以學(xué)習(xí)下。
    2018-12-12
  • 淺談C#泛型的用處與特點(diǎn)

    淺談C#泛型的用處與特點(diǎn)

    泛型是 2.0 版 C# 語言和公共語言運(yùn)行庫 (CLR) 中的一個(gè)新功能。泛型將類型參數(shù)的概念引入 .NET Framework,類型參數(shù)使得設(shè)計(jì)如下類和方法成為可能:這些類和方法將一個(gè)或多個(gè)類型的指定推遲到客戶端代碼聲明并實(shí)例化該類或方法的時(shí)候
    2013-09-09
  • C#使用CryptoStream類加密和解密字符串的實(shí)現(xiàn)

    C#使用CryptoStream類加密和解密字符串的實(shí)現(xiàn)

    CryptoStream設(shè)計(jì)用于在內(nèi)容以流的形式輸出到文件時(shí)加密和解密內(nèi)容,本文主要介紹了C#使用CryptoStream類加密和解密字符串的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • C#預(yù)定義數(shù)據(jù)類型之值類型和引用類型介紹

    C#預(yù)定義數(shù)據(jù)類型之值類型和引用類型介紹

    這篇文章主要介紹了C#預(yù)定義數(shù)據(jù)類型之值類型和引用類型介紹,本文著重講解了引用類型中的object(對(duì)象)類型和string(字符串)類型,需要的朋友可以參考下
    2015-03-03
  • C#中設(shè)置textbox限制條件的方法

    C#中設(shè)置textbox限制條件的方法

    這篇文章主要介紹了C#中設(shè)置textbox限制條件的方法,可實(shí)現(xiàn)設(shè)置像數(shù)量、價(jià)格、金額等的textbox的限制條件,用戶只能輸入數(shù)字或小數(shù),是非常實(shí)用的技巧,需要的朋友可以參考下
    2014-12-12
  • unity通過Mesh網(wǎng)格繪制圖形球體

    unity通過Mesh網(wǎng)格繪制圖形球體

    這篇文章主要為大家詳細(xì)介紹了unity通過Mesh網(wǎng)格繪制圖形球體,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • c# wpf如何使用Blend工具繪制Control樣式

    c# wpf如何使用Blend工具繪制Control樣式

    這篇文章主要介紹了c# wpf如何使用Blend工具繪制Control樣式,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-03-03
  • 用C#編寫ActiveX控件(三)

    用C#編寫ActiveX控件(三)

    用C#編寫ActiveX控件(三)...
    2007-03-03
  • C# DataSet查看返回結(jié)果集的實(shí)現(xiàn)

    C# DataSet查看返回結(jié)果集的實(shí)現(xiàn)

    這篇文章主要介紹了C# DataSet查看返回結(jié)果集的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10

最新評(píng)論