C creates a small Web Server (Socket implementation)

  • 2021-12-05 07:04:00
  • OfStack

To implement Web Server, the accessed data can be obtained through the following code browser access.


Socket socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socketWatch.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 81));
socketWatch.Listen(20); //  Parameter represents the maximum number of incoming connections that can be accommodated waiting to be accepted, excluding those that have already established a connection. 

Thread thread = new Thread(delegate(object obj)
{
 Socket socketListen = (Socket)obj;
 while (true)
 {
  Socket socket = socketListen.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 visit  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 only the principle, and it will be very complicated in practice, but even if we want to do a simple Web Server, we still need to solve two problems:

1. Output HTTP header


byte[] body = Encoding.UTF8.GetBytes(" Welcome to visit  www.cftea.com\r\n" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
byte[] head = Encoding.UTF8.GetBytes(@"HTTP/1.1 200 OK
Content-Length: " + body.Length + @"
Content-Type: text/plain
Date: " + 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 by browsers as HTTP headers. The format of the HTTP header is:

Line 1: HTTP/1. x + Spaces + Status Code + Spaces + Description Middle Line: Name + Colon + Space (can also be omitted) + Value Last line: Blank line

Format 1 must be correct, otherwise it will affect the browser's recognition of HTTP header and HTTP body.

2. Request the 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, so we only need to get it and disassemble it to get URL.

Disassembly is not difficult, let's talk about how to get it. Isn't the front data and length useless? As follows:


string requestText = Encoding.UTF8.GetString(data, 0, length);

Complete code


Socket socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socketWatch.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 81));
socketWatch.Listen(20); //  Parameter represents the maximum number of incoming connections that can be accommodated waiting to be accepted, excluding those that have already established a connection. 

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]; //  Data from 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 200 OK
Content-Length: " + body.Length + @"
Content-Type: text/plain
Date: " + 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);

Summarize

The above is the whole content of this article. I hope the content of this article can bring 1 certain help to everyone's study or work. If you have any questions, you can leave a message for communication.


Related articles: