C # Socket implement Http WEB Server

Source: Internet
Author: User
Tags socket error
This is just a simple WEB server written in C #. It only implements get requests for html files. Interested friends can continue to develop more functions on this basis, I learned c # From my younger brother soon. If you have any mistakes, please see me !!

 

Abstract:

WWW is based on the client/server computing model. It consists of a Web browser (client) and a Web server (server). The two use Hypertext Transfer Protocol (HTTP) for communication, the principles of HTTP include connection, request, and response. Based on the principle of the above HTTP protocol, this article implements the GET request Web server program method, by creating a TcpListener Class Object, listening port 8080; waiting, accepting the client to connect to port 8080; create the input stream and output stream associated with the socket word. Then, read the client's request information. If the request type is GET, obtain the accessed HTML file name from the request information, if an HTML file exists, open the HTML file, return the HTTP header information and HTML file content to the Web browser through socket, and then close the file. Otherwise, an error message is sent to the Web browser. Finally, close the socket word connected to the corresponding Web browser.

I. Principle of the HTTP protocol

WWW is an application system that uses the Internet as the transmission media. The most basic transmission unit on WWW is Web pages. WWW is based on the client/server computing model. It consists of a Web browser (client) and a Web server (server). The two use Hypertext Transfer Protocol (HTTP) for communication. HTTP is a protocol based on TCP/IP. It is an application layer protocol between a Web browser and a Web server. It is a common, stateless, and object-oriented protocol. The principle of HTTP protocol includes four steps:

Connection: the Web browser establishes a connection with the Web server and opens a virtual file called socket. The establishment of this file indicates that the connection is established successfully.

Request: the Web browser submits a request to the Web server through socket. HTTP requests are generally GET or POST commands (POST is used for passing FORM parameters ). The format of the GET command is:

GET path/File Name HTTP/1.0

The file name indicates the accessed file, and HTTP/1.0 indicates the HTTP Version Used by the Web browser.

Response: After a Web browser submits a request, the request is sent to the Web server over HTTP. After the Web server receives the request, it processes the transaction and returns the result to the Web browser over HTTP, so that the requested page is displayed on the Web browser.

For example, if the client establishes a connection with www.mycomputer.com: 8080/mydir/index.html, the GET command: GET/mydir/index.html HTTP/1.0 is sent. The Web server named www.mycomputer.comsearches for the mydirfile index.html in the file space of the website. If the file is found, the Web server sends the file content to the corresponding Web browser.

To inform the Web browser of the type of content transmitted, the Web server first Transmits some HTTP header information and then transmits the specific content (that is, the HTTP body information ), the HTTP header and HTTP body are separated by a blank line.

Common HTTP header information:

① HTTP 1.0 200 OK

This is the first line of Web server response. It lists the HTTP version numbers and response code that the server is running. The Code "200 OK" indicates that the request is complete.

(2) MIME_Version: 1.0

It indicates the MIME Version.

③ Content_type: Type

This header indicates the MIME type of the HTTP body information. For example, content_type: text/html indicates that the transmitted data is an HTML document.

④ Content_length: Length Value

It indicates the length (in bytes) of the HTTP body information ).

Close connection: After the response is over, the Web browser and the Web server must be disconnected to ensure that other Web browsers can establish a connection with the Web server.

II. C # programming for implementing Web Server Functions

Based on the principle of the above HTTP protocol, the method for implementing the GET request Web server program is as follows:

Create a TcpListener Class Object and listen to a port (enter any idle port such as 8080 ).

Wait and accept the client to connect to the port to obtain the socket connected to the client;

Read the request information submitted by a client from the input stream associated with the socket. The request information format is: GET path/File Name HTTP/1.0

Obtain the request type from the request information. If the request type is GET, the accessed HTML file name is obtained from the request information. When there is no HTML file name, index.html is used as the file name;

If an HTML file exists, open the HTML file, return the HTTP header information and HTML file content to the Web browser through socket, and then close the file. Otherwise, an error message is sent to the Web browser;

Close the socket word connected to the corresponding Web browser.

The implementation code is as follows:
/// // Webserver. cs //////////////////

Namespace cnnbsun. webserver
{
Using System;
Using System. IO;
Using System. Net;
Using System. Net. Sockets;
Using System. Text;
Using System. Threading;

Class MyWebServer
{

Private TcpListener myListener;
Private int port = 8080; // select any idle port

// Start listening to both ports
// Start a concurrent listening process at the same time
Public MyWebServer ()
{
Try
{
// Start listening to both ports
MyListener = new TcpListener (port );
MyListener. Start ();
Console. WriteLine ("Web Server Running... Press ^ C to Stop ...");
// Start a simultaneous listening process ''startlisten''
Thread th = new Thread (new ThreadStart (StartListen ));
Th. Start ();

}
Catch (Exception e)
{
Console. WriteLine ("concurrent listening port error:" + e. ToString ());
}
}
Public void SendHeader (string sHttpVersion, string sMIMEHeader, int iTotBytes, string sStatusCode, ref Socket mySocket)
{

String sBuffer = "";

If (sMIMEHeader. Length = 0)
{
SMIMEHeader = "text/html"; // default text/html
}

SBuffer = sBuffer + sHttpVersion + sStatusCode + "\ r \ n ";
SBuffer = sBuffer + "Server: cx1193719-b \ r \ n ";
SBuffer = sBuffer + "Content-Type:" + sMIMEHeader + "\ r \ n ";
SBuffer = sBuffer + "Accept-Ranges: bytes \ r \ n ";
SBuffer = sBuffer + "Content-Length:" + iTotBytes + "\ r \ n ";

Byte [] bSendData = Encoding. ASCII. GetBytes (sBuffer );

SendToBrowser (bSendData, ref mySocket );

Console. WriteLine ("Total Bytes:" + iTotBytes. ToString ());

}

Public void SendToBrowser (String sData, ref Socket mySocket)
{
SendToBrowser (Encoding. ASCII. GetBytes (sData), ref mySocket );
}

Public void SendToBrowser (Byte [] bSendData, ref Socket mySocket)
{
Int numBytes = 0;

Try
{
If (mySocket. Connected)
{
If (numBytes = mySocket. Send (bSendData, bSendData. Length, 0) =-1)
Console. writeline ("socket error cannot send packet ");
Else
{
Console. writeline ("No. of bytes send {0}", numbytes );
}
}
Else
Console. writeline ("connection failed ....");
}
Catch (exception E)
{
Console. writeline ("error: {0}", e );

}
}
Public static void main ()
{
MyWebServer MWs = new MyWebServer ();
}
Public void startlisten ()
{

Int istartpos = 0;
String srequest;
String sdirname;
String srequestedfile;
String sErrorMessage;
String sLocalDir;
/// // Your own virtual directory /////////////////////////////////////
String sMyWebServerRoot = "E: \ MyWebServerRoot \"; // set your virtual directory
//////////////////////////////////////// //////////////////////////////////////// //////////////////
String sPhysicalFilePath = "";
String sFormattedMessage = "";
String sResponse = "";

While (true)
{
// Accept new connections
Socket mySocket = myListener. AcceptSocket ();

Console. WriteLine ("Socket Type" + mySocket. SocketType );
If (mySocket. Connected)
{
Console. WriteLine ("\ nClient Connected !! \ N ===========================\ nCLient IP {0} \ n ", mySocket. RemoteEndPoint );

Byte [] bReceive = new Byte [1, 1024];
Int I = mySocket. Receive (bReceive, bReceive. Length, 0 );

// Convert to string type
String sBuffer = Encoding. ASCII. GetString (bReceive );

// Only process the "get" request type
If (sBuffer. Substring (0, 3 )! = "GET ")
{
Console. WriteLine ("only processing get request type ..");
MySocket. Close ();
Return;
}

// Find the location of "HTTP"
IStartPos = sBuffer. IndexOf ("HTTP", 1 );

String sHttpVersion = sBuffer. Substring (iStartPos, 8 );

// Obtain the request type and file directory file name
SRequest = sBuffer. Substring (0, iStartPos-1 );

SRequest. Replace ("\\","/");

// Add "/" if the end is not the file name or the end is not "/"
If (sRequest. IndexOf (".") <1 )&&(! SRequest. EndsWith ("/")))
{
SRequest = sRequest + "/";
}

// Get the request file name
IStartPos = sRequest. LastIndexOf ("/") + 1;
SRequestedFile = sRequest. Substring (iStartPos );

// Obtain the request file directory
SDirName = sRequest. Substring (sRequest. IndexOf ("/"), sRequest. LastIndexOf ("/")-3 );

// Obtain the physical path of the virtual directory
SLocalDir = sMyWebServerRoot;

Console. WriteLine ("request file directory:" + sLocalDir );

If (sLocalDir. Length = 0)
{
SErrorMessage = "<H2> Error !! Requested Directory does not exists </H2> <Br> ";
SendHeader (sHttpVersion, "", sErrorMessage. Length, "404 Not Found", ref mySocket );
SendToBrowser (sErrorMessage, ref mySocket );
MySocket. Close ();
Continue;
}

If (sRequestedFile. Length = 0)
{
// Get the request file name
SRequestedFile = "index.html ";
}

//////////////////////////////////////// /////////////////////////////
// Obtain the request file type (set to text/html)
//////////////////////////////////////// /////////////////////////////

String sMimeType = "text/html ";

SPhysicalFilePath = sLocalDir + sRequestedFile;
Console. WriteLine ("request file:" + sPhysicalFilePath );

If (File. Exists (sPhysicalFilePath) = false)
{

SErrorMessage = "<H2> 404 Error! File Does Not Exists... </H2> ";
SendHeader (sHttpVersion, "", sErrorMessage. Length, "404 Not Found", ref mySocket );
SendToBrowser (sErrorMessage, ref mySocket );

Console. WriteLine (sFormattedMessage );
}

Else
{
Int iTotBytes = 0;

SResponse = "";

FileStream fs = new FileStream (sPhysicalFilePath, FileMode. Open, FileAccess. Read, FileShare. Read );

BinaryReader reader = new BinaryReader (fs );
Byte [] bytes = new byte [fs. Length];
Int read;
While (read = reader. Read (bytes, 0, bytes. Length ))! = 0)
{
SResponse = sResponse + Encoding. ASCII. GetString (bytes, 0, read );

ITotBytes = iTotBytes + read;

}
Reader. Close ();
Fs. Close ();

SendHeader (sHttpVersion, sMimeType, iTotBytes, "200 OK", ref mySocket );
SendToBrowser (bytes, ref mySocket );
// MySocket. Send (bytes, bytes. Length, 0 );

}
MySocket. Close ();
}
}
}

}

}

/// // End ////////////////

Compile the file into an EXE file to implement a simple web server function!
You can set a virtual directory for testing!
Asp.net provides methods to host Asp.net. For more information, see this example: http://www.asp.net/Projects/Cassini/Download/Default.aspx? Tabindex = 0 & Tabid = 1;

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.