Write your own Web server one (browser access information)

Source: Internet
Author: User

to protocol a Web server, you need to understand the HTTP protocol, let's take a look at when the browser requests the Web server to send data to the website, I use Ubuntu telent show a next process.       I need a simple website to demonstrate that I have installed an Apache. To complete the process. The following shows the process. In   fact, access to a Web site is the browser Web server specifies the port to send information, and then the Web server to the browser to send the content of the page, 1. Connect to a Web site using Telnet

2. Enter the requested page. get/http/1.0 or get/index.html http/1.0

Get: Represents the Get method used to get the contents of the Web site

/: Represents the requested root directory, which is the default file (the configuration file for the site is determined). /index.html represents the index.html file under the root directory of the Web site

http/1.0: Indicates that the protocol used is HTTP1.0

Three parameters separated by a space

Results returned by 3.apache

Above is an HTTP protocol an interactive protocol, if you want to learn more please go online to find information.

Let's write a program ourselves to see the information sent to the Web server when the browser is connected.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>


#define MAX 1024x768
//Listen for the specified IP port
int socket_listen (char *ip, unsigned short int port)
{
 int Res_ Socket, res, on;
 struct sockaddr_in address;
 struct in_addr in_ip;
 res = Res_socket = Socket (af_inet, sock_stream, 0);
 setsockopt (Res_socket, Sol_socket, so_reuseaddr, &on, sizeof (on));
 memset (&address, 0, sizeof (address));
 address.sin_family = af_inet;
 address.sin_port =htons (port);
 address.sin_addr.s_addr = htonl (Inaddr_any);//inet_addr ("127.0.0.1");
 res = Bind (Res_socket, (struct sockaddr *) &address, sizeof (address));
 if (res) {printf ("Port is used, repeat bind\n"); exit (101);};
 res = Listen (res_socket,5);
 if (res) {printf ("Listen port is error; \ n"); exit (102); };
 return Res_socket;
}


int main (int argc, char * argv[])
{
int Res_socket, Conn_socket;
int tmp;
struct sockaddr_in client_addr;
Char Buf[max];
int len = sizeof (CLIENT_ADDR);
Res_socket = Socket_listen ("127.0.0.1", 1024);
while (1)
{
Conn_socket = Accept (Res_socket, (struct sockaddr *) &client_addr, &len);
printf ("Webbrower information\n\n");
Accept information from the browser
while (0 = = (tmp = Read (Conn_socket, buf, MAX-1)) | | tmp! = EOF)
{
buf[max-1]=0;
printf ("%s\n", buf);
Break
}
Close (Conn_socket);
}

}

The following information is received.


Reprinted from: http://m.blog.csdn.net/blog/rentiansheng/8068960

Write your own Web server one (browser access information)

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.