C # uses the socket to create a small Web server code share

Source: Internet
Author: User
Tags small web server
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:

    • First line: http/1.x + space + status code + Space + Description

    • Middle row: Name + colon + space (can also be omitted) + value

    • Last line: Blank line

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.