Implement a simple Web server in Ubuntu

Source: Internet
Author: User
Requirement: implement a simple Web server in Ubuntu, and read the path of the configuration file when the server starts. If the file requested by the browser is executable, it is called the CGI program, the server does not send the file to the browser, but executes the program on the server side and sends its standard output to the browser, the server does not send a complete HTTP header, and the CGI program itself is responsible for outputting part of the HTTP header. Configuration File (config.txt): PORT = 8000 Directory =./peiz

Requirements:

Implement a simple Web server in Ubuntu and read the path of the configuration file when the server starts. If the file requested by the browser is executable, it is called the CGI program, the server does not send the file to the browser, but executes the program on the server side and sends its standard output to the browser, the server does not send a complete HTTP header, and the CGI program itself is responsible for outputting part of the HTTP header.

Configuration File (config.txt ):

PORT = 8000
Directory =./peizhi

HTML file (index_html ):


First name:



Last name:

Server code:

# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include

# Define MAXLINE 80

Void deal_config (int * port_number, char * path );

Int main (void)
{
Struct sockaddr_in servaddr, cliaddr;
Socklen_t cliaddr_len;
Int listenfd, connfd;
Char buf [MAXLINE];
Char str [INET_ADDRSTRLEN];
Int I, n; // n descript the side number
Int pd_index, ret;
 
Int td;

Int port_number;
Char * path = NULL;

Listenfd = socket (AF_INET, SOCK_STREAM, 0 );

Int opt = 1;
Setsockopt (listenfd, SOL_SOCKET, SO_REUSEADDR, & opt, sizeof (opt ));

 
Bzero (& servaddr, sizeof (servaddr ));
Servaddr. sin_family = AF_INET;

Deal_config (& port_number, path );
Servaddr. sin_addr.s_addr = htonl (INADDR_ANY );
Servaddr. sin_port = htons (port_number );

Bind (listenfd, (struct sockaddr *) & servaddr, sizeof (servaddr ));
 
Listen (listenfd, 20 );

Printf ("Accepting connections... \ n ");
While (1 ){
Cliaddr_len = sizeof (cliaddr );
Connfd = accept (listenfd,
(Struct sockaddr *) & cliaddr, & cliaddr_len );

While (1 ){
N = read (connfd, buf, MAXLINE );
If (n = 0 ){
Printf ("the other side has been closed. \ n ");
Break;
}
Printf ("received File from % s at PORT % d \ n ",
Inet_ntop (AF_INET, & cliaddr. sin_addr, str, sizeof (str )),
Ntohs (cliaddr. sin_port ));
I = 0;
Char title [15];
While (buf [I]! = '\ R '){
Title [I] = buf [I];
I ++;
}
Title [I] = '\ 0 ';
If (0 = (strcmp (title, "GET/HTTP/1.1 "))){
Bzero (buf, MAXLINE );
Strcpy (buf, "HTTP/1.1 200 0K \ r \ n ");
Write (connfd, buf, strlen (buf ));
Bzero (buf, MAXLINE );
Strcpy (buf, "Conent_Type: peizhi/html \ r \ n ");
Write (connfd, buf, strlen (buf ));
Bzero (buf, MAXLINE );
Buf [0] = '\ R ';
Buf [1] = '\ n ';
Write (connfd, buf, 2 );
Pd_index = open ("./peizhi/index.html", O_RDONLY );
If (pd_index <0 ){
Perror ("open file failed. \ n ");
Exit (1 );
}
Bzero (buf, MAXLINE );
While (ret = read (pd_index, buf, MAXLINE)> 0 ){
Write (connfd, buf, ret );
Printf ("send index data... \ n ");
Bzero (buf, MAXLINE );
}
 Close (pd_index );
}
}
}
}
Void deal_config (int * port_number, char * path)
{
FILE * fd;

Fd = fopen ("./peizhi/config.txt", "r ");
If (fd <0 ){
Perror ("open config failed. \ n ");
Exit (1 );
}
Fscanf (fd, "PORT = % d \ nDirectory = % s", port_number, path );

}

Start the server and open the browser and enter 127.0.0.1: 8000 to access the following interface:

The program is not very complete and will be improved slowly. I hope you will criticize and correct it.

Recommended reading:

Build your own wiki and Forum under Ubuntu (figure) http://www.linuxidc.com/Linux/2008-02/11085.htm

Install the LAMP \ Vsftpd \ Webmin \ phpMyAdmin service and settings in Ubuntu 13.04Http://www.linuxidc.com/Linux/2013-06/86250.htm

Install LAMP and phpMyAdmin http://www.linuxidc.com/Linux/2012-09/71192.htm in Ubuntu

Ubuntu 12.04 LAMP installation configuration http://www.linuxidc.com/Linux/2012-05/61079.htm

For more information about Ubuntu, see Ubuntu special page http://www.linuxidc.com/topicnews.aspx? Tid = 2

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.