Preach Wisdom Podcast C + + Public lesson Note--linux The core code of network streaming media server

Source: Internet
Author: User
Tags strcmp

Xinetd from the daemon concept, it can be seen that for each service that the system is going through, it must run a daemon that listens to a port connection.


This usually means a waste of resources.


To solve this problem, Linux introduces the concept of "Network Daemon Service Program". xinted (Extended InterNET daemon)


XINETD listens to multiple specified ports at the same time, accepting user requests, initiating different network service processes to handle these user requests, depending on the request port.


You can think of xinetd as a Management server that manages the startup service, decides which program to hand a client request to, and then initiates the appropriate daemon.


XINETD does not run and listens for services on all ports it manages.


When a request arrives for a service to which it manages, XINETD initiates the appropriate server for the service.


Install and configure the XINETD1. sudo apt-get install xinetd (sudo aptitude show/install xinetd)


2. Sudo vi/etc/xinetd.d/myhttpd (note three unified)
Service MYHTTPD
{
Socket_type = Stream
protocol = TCP
wait = no
user = Nobody
Server =/HOME/WUYINGQIANG/HTTPTEST/MYHTTPD
Server_args =/home/wuyingqiang/httptest/dir
Disable = no
Flags = IPV4
}
Socket_type: Network socket type, stream, or packet.
PROTOCOL:IP protocol, usually TCP or UDP
Wait: Take value yes/no
User: Run the process ID
Server: Full path of execution
Server_args: The value passed to the server
Disable: Used to disable the service in the default {}
Flags: The Internet protocol used


3. Sudo vi/etc/services to which the port number is added, such as: 2222

4. Restart the XINETD server sudo service xinetd restart



URL encoding extension problem: You can view Unicode encoding for Chinese.


#include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <dirent.h> #include <ctype.h> #include <stdio.h> #include <time.h># Define SERVER_NAME "XHTTPD" #define PROTOCOL "http/1.1" #define Server_url "http://www.itcast.cn/" #defineFORMAT_DATE "% A,%d%b%Y%h:%m:%s GMT "#define N 4096#ifdef debug#define log (info, str) do{fprintf (fp_tmp,"%s%s ", info, str); Fflush (fp_t MP);} while (0)/* Put them in the right place.//fp_tmp = fopen (/home/akaedu/dir/log.text, "a");//log ("argv[1]", argv[1]);//log ( "Line:", line); */#endif//Set HTTP protocol header section static void send_headers (int status, Char *title, Char *extra_header, Char *mime_ Type, off_t length, time_t mod);//return error page static void send_error (int status, Char *title, Char *extra_header, char *text);// For file name encoding, corresponding strdecodestatic void Strencode (char *to, size_t tosize, const char *from);//Output file related property details static void File_infos (Char *dir, char *name);//url decoding static void StrdecoDe (Char *to, char *from);//Infer file Mime_typestatic char *get_mime_type (char *name) by file name;//16 binary converted to decimal number static int Hexit ( char c); int main (int argc, char **argv) {char line[n*2], method[n*2], path[n*2], protocol[n*2], idx[n*4], location[n*4]    ;    Char *file;    size_t Len;    int ich, I, n;    struct STAT sb;    FILE *FP;    struct Dirent **dl; if (argc! = 2)//xinetd in the Server_argssend_error ("Internal error", NULL, "Config error-no dir specified."); XINETD the root path (root directory of the XHTTPD server document) to this program at startup, this step is very important if (ChDir (argv[1]) < 0)//Change Working directory Send_error ("Internal error", NU    LL, "Config error-couldn ' t chdir."); HTTP protocol first line such as: get/hello.c http/1.1 if (fgets (line, sizeof (line), stdin) = = NULL)//Fetch request line, including request method/path/protocol Send_error (4    XX, "Bad Request", NULL, "No Request found.");  /* "%[^]%[^]%[^]" Cut a line of string by a space to three substrings application example: Get method: When you enter a Web page in the address bar of the browser, the browser uses the Get method to get the resource to the server, eg:get/form.html http/1.1 METHOD = GET Path =/form.html protocol = http/1.1 return value: Successfully returns the number of fields actually read,Failed to return -1*/if (sscanf (line, "%[^]%[^]%[^]", method, Path, protocol)! = 3)//Extraction method/Path/protocol Send_error ("Bad Request", NULL, "Can ' t parse request."); /Read to request header end while (Fgets (lines, sizeof (line), stdin) = NULL) {//Note stdin is dup2 to xinetd pipe read if (strcmp (line, "\ n") = = 0 | | str CMP (line, "\ r \ n") = = 0) break;  Only a GET request is supported, strcasecmp, ignoring case if (strcasecmp (method, "GET")! = 0) Send_error (501, "not implemented", NULL, "This method is Not implemented. ");    The/path must start with '/'.    if (path[0]! = '/') Send_error ("Bad Request", NULL, "bad filename.");    File = & (Path[1]);//file = path+1//decode path name, get rid of things like%20 strdecode (file, file);    if (file[0] = = ' + ') file = "./";//detection is a valid file format to prevent cross-border access to the root ancestor directory len = strlen (file); if (file[0] = = '/' | | strcmp (file, "...") = = 0 | | strncmp (file, "... /", 3) = = 0 | | Strstr (file, "/.. /")! = NULL | |    strcmp (& (File[len-3]), "/..") = = 0) {send_error ("Bad Request", (char*) 0, "illegal filename.");}    Test for a file (or directory) with no request, stat for Get the file function for the specified directory location files, SB for outgoing parameters. if (Stat (file, &AMP;SB) < 0) Send_error (404, "Not Found", (char*) 0, "File not Found."); * *404 page when the user enters the wrong link, the page returned, the user cannot resolve the error * Standardized configurable HTTP protocol error, located between 400 and 505 * 1, unable to access Web site on the requested port * 2, Web service extension lockout policy block this request * 3, MIME Map measure Block this request * *///request path is directory if (S_isdir (Sb.st_mode)) {//pathname corresponds to directory if (file[len-1]! = '/') {//Pathname to "/" End S    nprintf (location, sizeof, "Location:%s/", path);    Send_error (302, "Found", location, "directories must end with a slash.");    }//have index.html to handle the snprintf (IDX, sizeof (IDX), "%sindex.html", file), if (Stat (idx, &AMP;SB) >= 0) {file = idx; Goto do_file;//If there is index.html jump to Do_file:}//Display the list of files in the request directory Send_headers ($, "OK", NULL, "text/html",-1, Sb.st_mtime );p rintf ("

Preach Wisdom Podcast C + + Public lesson Note--linux The core code of network streaming media server

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.