最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

本文實(shí)例講述了C#實(shí)現(xiàn)多線(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)文章

  • unity實(shí)現(xiàn)簡(jiǎn)單計(jì)算器

    unity實(shí)現(xiàn)簡(jiǎn)單計(jì)算器

    這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)簡(jiǎn)單計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • C#中的out參數(shù)、ref參數(shù)和params可變參數(shù)用法介紹

    C#中的out參數(shù)、ref參數(shù)和params可變參數(shù)用法介紹

    這篇文章介紹了C#中的out參數(shù)、ref參數(shù)和params可變參數(shù)用法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-01-01
  • c# 線(xiàn)程安全隊(duì)列的用法原理及使用示例

    c# 線(xiàn)程安全隊(duì)列的用法原理及使用示例

    這篇文章主要介紹了c# 線(xiàn)程安全隊(duì)列的用法原理及使用示例,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2020-11-11
  • C#實(shí)現(xiàn)的滾動(dòng)網(wǎng)頁(yè)截圖功能示例

    C#實(shí)現(xiàn)的滾動(dòng)網(wǎng)頁(yè)截圖功能示例

    這篇文章主要介紹了C#實(shí)現(xiàn)的滾動(dòng)網(wǎng)頁(yè)截圖功能,結(jié)合具體實(shí)例形式分析了C#圖形操作的相關(guān)技巧,需要的朋友可以參考下
    2017-07-07
  • C#解析json文件的實(shí)現(xiàn)代碼

    C#解析json文件的實(shí)現(xiàn)代碼

    最近需要用c#解析json文件,以前沒(méi)用過(guò)這個(gè),百度了一下找到了這篇文章感覺(jué)不錯(cuò),特分享下
    2013-06-06
  • C# PadLeft、PadRight用法詳解

    C# PadLeft、PadRight用法詳解

    本文主要介紹了C# PadLeft、PadRight用法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • C#中使用ADOMD.NET查詢(xún)多維數(shù)據(jù)集的實(shí)現(xiàn)方法

    C#中使用ADOMD.NET查詢(xún)多維數(shù)據(jù)集的實(shí)現(xiàn)方法

    這篇文章主要介紹了C#中使用ADOMD.NET查詢(xún)多維數(shù)據(jù)集的實(shí)現(xiàn)方法,詳細(xì)講述了C#中使用ADOMD.NET查詢(xún)多維數(shù)據(jù)集的原理與實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2014-10-10
  • C#讀取txt文件數(shù)據(jù)的方法實(shí)例

    C#讀取txt文件數(shù)據(jù)的方法實(shí)例

    讀取txt文本數(shù)據(jù)的內(nèi)容,是我們開(kāi)發(fā)中經(jīng)常會(huì)遇到的一個(gè)功能,這篇文章主要給大家介紹了關(guān)于C#讀取txt文件數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下
    2021-05-05
  • C#使用Log4.net記錄日志文件

    C#使用Log4.net記錄日志文件

    這篇文章介紹了C#使用Log4.net記錄日志文件的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • C#客戶(hù)端程序調(diào)用外部程序的3種實(shí)現(xiàn)方法

    C#客戶(hù)端程序調(diào)用外部程序的3種實(shí)現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于C#客戶(hù)端程序調(diào)用外部程序的3種實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-04-04

最新評(píng)論

房产| 白朗县| 抚顺市| 泰安市| 景谷| 噶尔县| 衢州市| 尼木县| 河北省| 汤阴县| 昌黎县| 鄂托克旗| 吐鲁番市| 夏津县| 洛隆县| 保山市| 朝阳区| 花莲市| 墨脱县| 宜城市| 蒲城县| 云梦县| 铁力市| 保定市| 高青县| 丰镇市| 昭苏县| 广西| 长治市| 吉隆县| 永春县| 合作市| 北川| 紫阳县| 灌南县| 宜良县| 台前县| 萝北县| 罗定市| 新巴尔虎右旗| 伽师县|