This article mainly introduces the use of the socket implementation of C # to create a small Web server, the article through the sample code introduced in very detailed, the need for friends can refer to, let's take a look at it.
To implement Web Server, you can access the data by following a few lines of code browser.
Socket Socketwatch = new socket (addressfamily.internetwork, SocketType.Stream, protocoltype.tcp); Socketwatch.bind ( New IPEndPoint (Ipaddress.parse ("127.0.0.1"), Bayi); Socketwatch.listen (20); The parameter represents the maximum number of incoming connections that can be accommodated and does not contain a connection that has already been established. Thread thread = new Thread (delegate (object obj) {socket Socketlisten = (socket) obj; while (true) { socket socket = Soc Ketlisten.accept (); byte[] data = new BYTE[1024 * 1024 * 4]; The data sent by the browser is limited to 4K. int length = socket. Receive (data, 0, data. Length, socketflags.none); Socket. Send (Encoding.UTF8.GetBytes ("Welcome to www.cftea.com\r\n" + DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss.fff")); Socket. Shutdown (Socketshutdown.both); Socket. Close (); }}); thread. IsBackground = True;thread. Start (Socketwatch);
But the above is just the principle, the actual will be very complex, but even if we want to do a simple Web Server, still need to solve two problems:
One, the output HTTP header
byte[] BODY = Encoding.UTF8.GetBytes ("Welcome to www.cftea.com\r\n" + DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss.fff"); Byte[] head = Encoding.UTF8.GetBytes (@ "http/1.1 okcontent-length:" + body. Length + @ "Content-type:text/plaindate:" + string. Format ("{0:r}", DateTime.Now) + @ "Server:cftea Web Server"); socket. Send (head); socket. Send (body);
As long as there is a specific format, it will be treated as HTTP headers by the browser. The format of the HTTP header is:
Format must be correct, otherwise affect the browser to the HTTP header and HTTP body recognition.
Second, request HTTP header
So far, we don't know what URL is entered in the browser. The HTTP header of the request also has a specific format, we just need to get it out, to disassemble, we can get the URL.
Disassembly is not a difficult task, let's talk about how to get it. The previous data, length is not always useless? As follows:
String requesttext = Encoding.UTF8.GetString (data, 0, length);
Full code
Socket Socketwatch = new socket (addressfamily.internetwork, SocketType.Stream, protocoltype.tcp); Socketwatch.bind ( New IPEndPoint (Ipaddress.parse ("127.0.0.1"), Bayi); Socketwatch.listen (20); The parameter represents the maximum number of incoming connections that can be accommodated and does not contain a connection that has already been established. Thread thread = new Thread (delegate (object obj) {socket Socketlisten = (socket) obj; while (true) {using (Socket socket = Socketlisten.accept ()) {byte[] data = new BYTE[1024 * 1024 * 4];//The data sent by the browser int length = socket. Receive (data, 0, data. Length, Socketflags.none); if (length > 0) {string requesttext = Encoding.UTF8.GetString (data, 0, length); byte[] BODY = Encoding.UTF8.GetBytes (DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss.fff")); Byte[] head = Encoding.UTF8.GetBytes (@ "http/1.1 okcontent-length:" + body. Length + @ "Content-type:text/plaindate:" + string. Format ("{0:r}", DateTime.Now) + @ "Server:cftea Web Server"); Socket. Send (head); Socket. Send (body); } socket. Shutdown (Socketshutdown.both); Socket. Close (); }}); thread. IsBaCkground = True;thread. Start (Socketwatch);
Summary