首页 > 编程 > C# > 正文

C#实现多线程的Web代理服务器实例

2019-10-29 21:40:54
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了C#实现多线程的Web代理服务器,涉及C#多线程代理服务器的实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了C#实现多线程的Web代理服务器。分享给大家供大家参考。具体如下:

 

 
  1. /** 
  2. Proxy.cs: 
  3. C# Programming Tips & Techniques 
  4. by Charles Wright, Kris Jamsa 
  5. Publisher: Osborne/McGraw-Hill (December 28, 2001) 
  6. ISBN: 0072193794 
  7. */ 
  8. // Proxy.cs -- Implements a multi-threaded Web proxy server 
  9. // 
  10. // Compile this program with the following command line: 
  11. // C:>csc Proxy.cs 
  12. using System; 
  13. using System.Net; 
  14. using System.Net.Sockets; 
  15. using System.Text; 
  16. using System.IO; 
  17. using System.Threading; 
  18. namespace nsProxyServer 
  19. { 
  20. public class ProxyServer 
  21. { 
  22. static public void Main (string [] args) 
  23. { 
  24. int Port = 3125; 
  25. if (args.Length > 0) 
  26. { 
  27. try 
  28. { 
  29. Port = Convert.ToInt32 (args[0]); 
  30. } 
  31. catch 
  32. { 
  33. Console.WriteLine ("Please enter a port number."); 
  34. return; 
  35. } 
  36. } 
  37. try 
  38. { 
  39. // Create a listener for the proxy port 
  40. TcpListener sockServer = new TcpListener (Port); 
  41. sockServer.Start (); 
  42. while (true) 
  43. { 
  44. // Accept connections on the proxy port. 
  45. Socket socket = sockServer.AcceptSocket (); 
  46. // When AcceptSocket returns, it means there is a connection. Create 
  47. // an instance of the proxy server class and start a thread running. 
  48. clsProxyConnection proxy = new clsProxyConnection (socket); 
  49. Thread thrd = new Thread (new ThreadStart (proxy.Run)); 
  50. thrd.Start (); 
  51. // While the thread is running, the main program thread will loop around 
  52. // and listen for the next connection request. 
  53. } 
  54. } 
  55. catch (IOException e) 
  56. { 
  57. Console.WriteLine (e.Message); 
  58. } 
  59. } 
  60. } 
  61. class clsProxyConnection 
  62. { 
  63. public clsProxyConnection (Socket sockClient) 
  64. { 
  65. m_sockClient = sockClient; 
  66. } 
  67. Socket m_sockClient; //, m_sockServer; 
  68. Byte [] readBuf = new Byte [1024]; 
  69. Byte [] buffer = null; 
  70. Encoding ASCII = Encoding.ASCII; 
  71. public void Run () 
  72. { 
  73. string strFromClient = ""; 
  74. try 
  75. { 
  76. // Read the incoming text on the socket/ 
  77. int bytes = ReadMessage (m_sockClient, 
  78. readBuf, ref strFromClient); 
  79. // If it's empty, it's an error, so just return. 
  80. // This will termiate the thread. 
  81. if (bytes == 0) 
  82. return; 
  83. // Get the URL for the connection. The client browser sends a GET command 
  84. // followed by a space, then the URL, then and identifer for the HTTP version. 
  85. // Extract the URL as the string betweeen the spaces. 
  86. int index1 = strFromClient.IndexOf (' '); 
  87. int index2 = strFromClient.IndexOf (' ', index1 + 1); 
  88. string strClientConnection = 
  89. strFromClient.Substring (index1 + 1, index2 - index1); 
  90. if ((index1 < 0) || (index2 < 0)) 
  91. { 
  92. throw (new IOException ()); 
  93. } 
  94. // Write a messsage that we are connecting. 
  95. Console.WriteLine ("Connecting to Site " + 
  96. strClientConnection); 
  97. Console.WriteLine ("Connection from " + 
  98. m_sockClient.RemoteEndPoint); 
  99. // Create a WebRequest object. 
  100. WebRequest req = (WebRequest) WebRequest.Create 
  101. (strClientConnection); 
  102. // Get the response from the Web site. 
  103. WebResponse response = req.GetResponse (); 
  104. int BytesRead = 0; 
  105. Byte [] Buffer = new Byte[32]; 
  106. int BytesSent = 0; 
  107. // Create a response stream object. 
  108. Stream ResponseStream = response.GetResponseStream(); 
  109. // Read the response into a buffer. 
  110. BytesRead = ResponseStream.Read(Buffer,0,32); 
  111. StringBuilder strResponse = new StringBuilder(""); 
  112. while (BytesRead != 0) 
  113. { 
  114. // Pass the response back to the client 
  115. strResponse.Append(Encoding.ASCII.GetString(Buffer, 
  116. 0, BytesRead)); 
  117. m_sockClient.Send(Buffer, BytesRead, 0); 
  118. BytesSent += BytesRead; 
  119. // Read the next part of the response 
  120. BytesRead = ResponseStream.Read(Buffer, 0, 32); 
  121. } 
  122. } 
  123. catch (FileNotFoundException e) 
  124. { 
  125. SendErrorPage (404, "File Not Found", e.Message); 
  126. } 
  127. catch (IOException e) 
  128. { 
  129. SendErrorPage (503, "Service not available", e.Message); 
  130. } 
  131. catch (Exception e) 
  132. { 
  133. SendErrorPage (404, "File Not Found", e.Message); 
  134. Console.WriteLine (e.StackTrace); 
  135. Console.WriteLine (e.Message); 
  136. } 
  137. finally 
  138. { 
  139. // Disconnect and close the socket. 
  140. if (m_sockClient != null) 
  141. { 
  142. if (m_sockClient.Connected) 
  143. { 
  144. m_sockClient.Close (); 
  145. } 
  146. } 
  147. } 
  148. // Returning from this method will terminate the thread. 
  149. } 
  150. // Write an error response to the client. 
  151. void SendErrorPage (int status, string strReason, string strText) 
  152. { 
  153. SendMessage (m_sockClient, "HTTP/1.0" + " " + 
  154. status + " " + strReason + "/r/n"); 
  155. SendMessage (m_sockClient, "Content-Type: text/plain" + "/r/n"); 
  156. SendMessage (m_sockClient, "Proxy-Connection: close" + "/r/n"); 
  157. SendMessage (m_sockClient, "/r/n"); 
  158. SendMessage (m_sockClient, status + " " + strReason); 
  159. SendMessage (m_sockClient, strText); 
  160. } 
  161. // Send a string to a socket. 
  162. void SendMessage (Socket sock, string strMessage) 
  163. { 
  164. buffer = new Byte [strMessage.Length + 1]; 
  165. int len = ASCII.GetBytes (strMessage.ToCharArray(), 
  166. 0, strMessage.Length, buffer, 0); 
  167. sock.Send (buffer, len, 0); 
  168. } 
  169. // Read a string from a socket. 
  170. int ReadMessage (Socket sock, byte [] buf, ref string strMessage) 
  171. { 
  172. int iBytes = sock.Receive (buf, 1024, 0); 
  173. strMessage = Encoding.ASCII.GetString (buf); 
  174. return (iBytes); 
  175. } 
  176. } 
  177. } 

希望本文所述对大家的C#程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表