Use C# (C++) Socket backend to write your own IIS server instead of IIS

 This article touches on the knowledge of Socket TCP/IP programming. In fact, this is a very simple content. You can read it down if you don't see it, and read it again later. You may find it very simple.

 

Many people are used to writing websites and use advanced backends such as IIS and Tomcat to do web backend services. However, before these backends came out, how did people write websites? What are the norms and standards that they abide by at the bottom?

 

This is the http protocol and html, js, css standards. These are all internationally unified standards and have nothing to do with languages ​​such as .net, java, and php. For example, the following piece of code:

<html>

<body>

<form runat="server" id="form1">

<%

string str_response="hello world";

........................................

Response.Write(str_response);

%>

<asp:Button ID="bn_1" runat="Server" Text="点击"/>

</form>

</body>

</html>

The long piece of code will eventually be parsed into the following

<html>

<body>

<form id="form1" action="">

hello world

<input type="submit" value="点击"  name="bn_1"/>

</form>

</body>

</html>

 

IIS helps us to convert the content in <%%> into the content to be output, and at the same time convert these tags in <asp: into corresponding tags (here, <input> tags), which are non-standard tags in html. Belonging to the tags specified by iis, iis has the responsibility to convert these tags into standard tags and return them to customers.

 


The above are the basics. I believe that many people who have written a website know what is going on. Let’s return to the topic below.

The following code may be rarely used by everyone:

GET /siteserver/mypage.youlike?h_username=myname HTTP/1.1
Accept: */*
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152;

.NET CLR 3.5.30729)
Host: 127.0.0.1:45632
Content-Length: 93
Connection: Keep-Alive
Cache-Control: no-cache

In fact, this is very simple. This is what the client IE opens a URL http://127.0.0.1:45632/siteserver/mypage.youlike?h_username=myname , http://127.0.0.1:45632 all received by the background of this website Protocol code.

 

These strings are separated by newlines.
Among them, 127.0.0.1:45632 is the address of a standard site, 127.0.0.1 is the IP, and 45632 is the port monitored by the server.
After User-Agent is the browser information, /siteserver/mypage.youlike between GET and HTTP/1.1 is the target address to be visited. Of course, if it is a backend written by yourself, this target address is actually an index, such as index. aspx, tell the server what data he wants, mypage.youlike may really exist in this file, or it may be just an index defined by the server, the switch statement of the service judges it to be mypage.youlike, then the corresponding data is returned.
Of course, /siteserver/mypage.youlike is followed by the parameter information of get. I believe those who write the web must be very clear.


Okay, the above is an example of get, what if the other party came by post? It's actually very simple, just add a bunch of things at the end:


POST /siteserver/mypage.youlike?h_username=myname HTTP/1.1
Accept: */*
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152;

.NET CLR 3.5.30729)
Host: 127.0.0.1:45632
Content-Length: 93
Connection: Keep-Alive
Cache-Control: no-cache

 

page=12&title_id=300


After the server receives these data, what will it do?

In fact, the process mentioned above is a request request process, that is, the server receiving process, and the other process on the server side is called return, which is the response process of response. What are we going to do next?

Let me give you a code for reference:

        if(str_request_file=="/siteserver/mypage.youlike")
       {
                string send_msg = "<!DOCTYPE HTML PUBLIC /" -//W3C//DTD HTML 4.0 Transitional//EN/">";
                send_msg += "<html  xmlns=/"http://www.w3.org/1999/xhtml/">";
                send_msg += "<head><meta http-equiv=/"content-type/" content=/"text/html; charset=gb2312/"/><title>广业 IIS Server</title></head><body>";
                send_msg += "<form action=/"http://127.0.0.1/siteserver/gps.aspx/" method=/"post/" id=/"f1/" name=/"f1/">";
                send_msg += "Hello World";
                send_msg += "<input id=/"h_my_username/" name=/"h_my_username/" type=/"hidden/" value=/"nl000/" />";
                send_msg += "</form>";
                send_msg += "<script type=/"text/javascript/">";
                //send_msg += "document.getElementById(/"f1/").submit();";
                send_msg += "</script>";
///根据page=12&title_id=300,h_username=myname这些参数读取数据库,生成一些内容加进send_msg里
                send_msg += "</body>";
                send_msg += "</html>";
                byte []msg=System.Text.Encoding.GetEncoding("gb2312").GetBytes(send_msg);

                string str_senddd="HTTP/1.1 200 OK/r/nVia: 1.1 GY-14SWFMJH7AP2/r/nConnection: Keep-Alive/r/nProxy-Connection: Keep-Alive/r/nContent-Length: "+msg.Length.ToString()+"/r/nContent-Type: text/html/r/nServer: GYXX/1.0/r/nCache-Control: private/r/n/r/n";
                sendtoclient(sock, str_senddd + send_msg);


          }


The sendtoclient method here is to write the string str_senddd + send_msg to the socket


In fact, the simplest iis service is such a process of reading and writing. The http protocol is inseparable from this process. Let’s look at this for getting started, and good luck depends on the individual.

 

Contact: QQ: 13896398, Tel: 13437839547

Guess you like

Origin blog.csdn.net/handsome0916/article/details/4581503