C # Socket simulate http server help class,
0x01 Preface
0x02 Http protocol
0x03 TCP/IP
0x04 view code
0x05 Summary
0x01 Preface
During work, it is often necessary to communicate between servers or between processes and assign tasks. Using a Socket is too troublesome. You need to establish a connection to process messages. So
Prepare to create a Socket to simulate the help class of the Http server and call it directly through WebClient.
0x02 Http protocol
My understanding: the http protocol is actually based on a TCP/IP protocol. It can also send messages through socket, just to say. If the format of the sent content meets the http conditions, it can be understood as the http protocol.
Common Http headers. The Http Version can also be 1.1.
HTTP/1.0 200 OKContent-Type: text/htmlConnection: keep-aliveContent-Encoding: utf-8
0x03 TCP/IP
My understanding: the TCP/IP protocol is actually a big classification of a certain type of protocol. Many protocols transmit messages based on this protocol. Search for more details.
0x04 view code
SocketHttpHelper. cs socket simulate http help class
1 public class SocketHttpHelper 2 {3 private string ip = "127.0.0.1"; 4 private int port = 8123; 5 private int count = 0; 6 private Socket server = null; 7 8 public string DefaultReturn = string. empty; 9 10 public event Func <string, string, string> Handler = null; 11 12 public SocketHttpHelper () 13 {14} 15 16 public SocketHttpHelper (string ip, int port) 17 {18 this. ip = ip; 19 this. port = port; 20} 21 22 public void StartListen (int count = 10) 23 {24 this. count = count; 25 Thread t = new Thread (new ThreadStart (ProcessThread); 26 t. isBackground = true; 27 t. start (); 28} 29 30 public void CloseSocket () 31 {32 try 33 {34 server. close (); 35} 36 catch {} 37} 38 39 private void ProcessThread () 40 {41 server = new Socket (AddressFamily. interNetwork, SocketType. stream, ProtocolType. tcp ); 42 server. bind (new System. net. IPEndPoint (System. net. IPAddress. parse (ip), port); 43 server. listen (count); 44 while (true) 45 {46 try 47 {48 Socket client = server. accept (); 49 ThreadPool. queueUserWorkItem (new WaitCallback (ListenExecute), client); 50} 51 catch {} 52 finally 53 {54} 55} 56} 57 58 private void ListenExecute (object obj) 59 {60 Socket client = obj as Socket; 61 try 62 {63 String ip = (client. remoteEndPoint as System. net. IPEndPoint ). address. toString (); 64 byte [] buffer = new byte [1024]; 65 int count = client. receive (buffer); 66 if (count> 0) 67 {68 string content = Encoding. UTF8.GetString (buffer, 0, count); 69 70 // parse content 71 Regex actionRegex = new Regex (@ "GET \ s + /(? <Action> \ w + )\? (? <Args> [^ \ s] {0,}) "); 72 string action = actionRegex. match (content ). groups ["action"]. value; 73 string args = actionRegex. match (content ). groups ["args"]. value; 74 string headStr = @ "HTTP/1.0 200 OK 75 Content-Type: text/html 76 Connection: keep-alive 77 Content-Encoding: UTF-8 78 79 "; 80 if (Handler! = Null) 81 {82 try 83 {84 string result = Handler (action, args); 85 string data = string. format (headStr + result); 86 client. send (Encoding. UTF8.GetBytes (data); 87} 88 catch {} 89 finally 90 {91} 92} 93 else 94 {95 string data = string. format (headStr + DefaultReturn); 96 client. send (Encoding. UTF8.GetBytes (data); 97} 98} 99} 100 catch {} 101 finally102 {103 try104 {105 client. shutdown (SocketShutdown. both); 106 client. close (); 107 client. dispose (); 108} 109 catch {} 110} 111} 112}View Code0x05 Summary
Because each time you write data from a Socket, you can only find it when you write more. In the past, you need to write a public class, which is not time-consuming.