C# 利用socekt做到http监听,怎么样才能做到高性能

王朝学院·作者佚名  2016-05-20
窄屏简体版  字體: |||超大  

C# 利用socekt做到http监听,怎么样才能做到高性能c#原始提供了http的监听的类HttpListener,实现了简单的http。文章地址《C# 控制台或者winform程序开启http的监听状态》

但是经过我测试,这个HttpListener提供的真的就只是简单的http监听功能,无法实现高并发处理。

不知道是我处理问题还是其他什么原因,无法实现,当上一个http请求连接尚未关闭的情况下,即便是把请求放到另外一个线程执行,都要等到处理结束,close了才能接受和处理下一次的连接请求。

也许你会说HttpListener不是提供了异步监听的嘛?异步不就可以类使用多线程实现嘛。但是经过我测试,确实没有得到我想要的实际效果。

所以另辟蹊径。http其实质就是socket的tcp封装实现的功能,单次请求,处理,关闭的socket功能。

所以这里找到了可以使用最原始的socket的来提供http监听,处理数据,关闭状态。

好了直接上代码,,一下代码部分来至于博客园,园友帖子提供,时间久远亦不知道是哪位仁兄的帖子,见谅。

1 internal class HttpServer 2 { 3 PRivate ipEndPoint _IP; 4 private TcpListener _Listeners; 5 private volatile bool IsInit = false; 6 HashSet<string> Names; 7 8 /// <summary> 9 /// 初始化服务器 10 /// </summary> 11 public HttpServer(string ip, int port, HashSet<string> names) 12 { 13 IPEndPoint localEP = new IPEndPoint(IPAddress.Parse(ip), port); 14 this._IP = localEP; 15 Names = names; 16 if (Names == null) 17 { 18 Names = new HashSet<string>(); 19 } 20 try 21 { 22 foreach (var item in names) 23 { 24 Console.WriteLine(string.Format(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff:") + "Start Listen Http Socket -> {0}:{1}{2} ", ip, port, item)); 25 } 26 this._Listeners = new TcpListener(IPAddress.Parse(ip), port); 27 this._Listeners.Start(5000); 28 IsInit = true; 29 this.AcceptAsync(); 30 } 31 catch (Exception ex) 32 { 33 Console.WriteLine(ex); 34 this.Dispose(); 35 } 36 } 37 38 private void AcceptAsync() 39 { 40 try 41 { 42 this._Listeners.BeginAcceptTcpClient(new AsyncCallback(AcceptAsync_Async), null); 43 } 44 catch (Exception) { } 45 } 46 47 private void AcceptAsync_Async(IAsyncResult iar) 48 { 49 this.AcceptAsync(); 50 try 51 { 52 TcpClient client = this._Listeners.EndAcceptTcpClient(iar); 53 var socket = new HttpClient(client); 54 Console.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff:") + "Create Http Socket Remote Socket LocalEndPoint:" + client.Client.LocalEndPoint + " RemoteEndPoint:" + client.Client.RemoteEndPoint.ToString()); 55 foreach (var item in Names) 56 { 57 if (socket.http_url.StartsWith(item)) 58 { 59 try 60 { 61 socket.process(); 62 return; 63 } 64 catch { break; } 65 } 66 } 67 socket.WriteFailure(); 68 socket.Close(); 69 } 70 catch (Exception) { } 71 } 72 73 /// <summary> 74 /// 释放资源 75 /// </summary> 76 public void Dispose() 77 { 78 if (IsInit) 79 { 80 IsInit = false; 81 this.Dispose(true); 82 GC.SuppressFinalize(this); 83 } 84 } 85 86 /// <summary> 87 /// 释放所占用的资源 88 /// </summary> 89 /// <param name="flag1"></param> 90 protected virtual void Dispose([MarshalAs(UnmanagedType.U1)] bool flag1) 91 { 92 if (flag1) 93 { 94 if (_Listeners != null) 95 { 96 try 97 { 98 Console.WriteLine(string.Format("Stop Http Listener -> {0}:{1} ", this.IP.Address.ToString(), this.IP.Port)); 99 _Listeners.Stop();100 _Listeners = null;101 }102 catch { }103 }104 }105 }106 107 /// <summary>108 /// 获取绑定终结点109 /// </summary>110 public IPEndPoint IP { get { return this._IP; } }111 }

这个是实现socket监听状态

1 public class HttpClient 2 { 3 private static int MAX_POST_SIZE = 10 * 1024 * 1024; // 10MB 4 private const int BUF_SIZE = 4096; 5 private Stream inputStream; 6 public StreamWriter OutputStream; 7 public String http_method; 8 public String http_url; 9 public String http_protocol_versionstring; 10 public Hashtable httpHeaders = new Hashtable(); 11 internal TcpClient _Socket; 12 13 /// <summary> 14 /// 这个是服务器收到有效链接初始化 15 /// </summary> 16 internal HttpClient(TcpClient client) 17 { 18 this._Socket = client; 19 inputStream = new BufferedStream(_Socket.GetStream()); 20 OutputStream = new StreamWriter(new BufferedStream(_Socket.GetStream()), UTF8Encoding.Default); 21 ParseRequest(); 22 } 23 24 internal void process() 25 { 26 try 27 { 28 if (http_method.Equals("GET")) 29 { 30 Program.Pool.ActiveHttp(this, GetRequestExec()); 31 } 32 else if (http_method.Equals("POST")) 33 { 34 Program.Pool.ActiveHttp(this, PostRequestExec()); 35 } 36 } 37 catch (Exception e) 38 { 39 Console.WriteLine("Exception: " + e.ToString()); 40 WriteFailure(); 41 } 42 } 43 44 public void Close() 45 { 46 OutputStream.Flush(); 47 inputStream.Dispose(); 48 inputStream = null; 49 OutputStream.Dispose(); 50 OutputStream = null; // bs = null; 51 this._Socket.Close(); 52 } 53 54 #region 读取流的一行 private string ReadLine() 55 /// <summary> 56 /// 读取流的一行 57 /// </summary> 58 /// <returns></returns> 59 private string ReadLine() 60 { 61 int next_char; 62 string data = ""; 63 while (true) 64 { 65 next_char = this.inputStream.ReadByte(); 66 if (next_char == '\n') { break; } 67 if (next_char == '\r') { continue; } 68 if (next_char == -1) { Thread.Sleep(1); continue; }; 69 data += Convert.ToChar(next_char); 70 } 71 return data; 72 } 73 #endregion 74 75 #region 转化出 Request private void ParseRequest() 76 /// <summary> 77 /// 转化出 Request 78 /// </summary> 79 private void ParseRequest() 80 { 81 String request = ReadLine(); 82 if (request != null) 83 { 84 string[] tokens = request.Split(' '); 85 if (tokens.Length != 3) 86 { 87 throw new Exception("invalid http request line"); 88 } 89 http_method = tokens[0].ToUpper(); 90 http_url = tokens[1]; 91 http_protocol_versionstring = tokens[2]; 92 } 93 String line; 94 while ((line = ReadLine()) != null) 95 { 96 if (line.Equals("")) 97 { 98 break; 99 }100 int separator = line.IndexOf(':');101 if (separator == -1)102 {103 throw new Exception("invalid http header line: " + line);104 }105 String name = line.Sub

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有  導航