Use fork to create a sub-process for persistent connection. I am waiting now. Urgent

Source: Internet
Author: User
Tags strtok
Use fork to create a sub-process for persistent connection. I am waiting now. Urgent: Linux general technology-Linux technology and application information. The following is a detailed description. All the code on the server side. Waiting is always displayed during connection...
Where is it. There is no error during running. However, the webpage cannot be opened.
Is it waitpid? Or is fork () used incorrectly?

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

# Define LINESIZE 1024
# Define MAXPENDING 20
# Define MAXDIRLEN 256

Void * service_thread (void * arg );
Void do_HTTPservice (int clnt_sock );
Void send_file (int clnt_sock, char * file_name );
Void send_error400 (int clnt_sock );
Void send_error404 (int clnt_sock );
Void exit_error (char * error_message );
Void zombie_collector (int cpid );
Int read_line (int clnt_sock, char * line );

Int * csock;
Char base_dir [] = "/home/class10/acyang/http /";

Int main (int argc, char ** argv)
{
Int cpid;
Int serv_sock;
Int clnt_sock;

Struct sockaddr_in serv_addr;
Struct sockaddr_in clnt_addr;
Struct sigaction sig_handler;

Int caddr_len;
Unsigned short serv_port;
Pthread_t tid;

If (argc! = 2 ){
Printf ("Usage: % s \ N ", argv [0]);
Exit (0 );
}

Serv_port = atoi (argv [1]);

If (serv_sock = socket (AF_INET, SOCK_STREAM, 0) <0)
Exit_error ("socket () failed ");

Memset (& serv_addr, 0, sizeof (serv_addr ));
Serv_addr.sin_family = AF_INET;
Serv_addr.sin_addr.s_addr = htonl (INADDR_ANY );
Serv_addr.sin_port = htons (serv_port );

If (bind (serv_sock, (struct sockaddr *) & serv_addr, sizeof (serv_addr) =-1)
Exit_error ("bind () error ");
If (listen (serv_sock, MAXPENDING) =-1)
Exit_error ("listen () error ");

// Sig_handler.sa_handler = zombie_collector;
Sig_handler.sa_flags-SA_RESTART;


While (1 ){
Caddr_len = sizeof (clnt_addr );
If (clnt_sock = accept (serv_sock, (struct sockaddr *)
& Clnt_addr, & caddr_len) <0)
Exit_error ("accept () failed ");
Clnt_sock = accept (serv_sock, (struct sockaddr *) & clnt_addr, & caddr_len );

If (cpid = fork () <0)
Exit_error ("fork () failed ");
If (cpid = 0)
{
If (csock = (int *) malloc (sizeof (csock) = NULL)
Exit_error ("malloc () error ");

Close (serv_sock );
* Csock = clnt_sock;
If (pthread_create (& tid, NULL, service_thread, (void *) csock ))! = 0)
Exit_error ("pthread_create () failed ");
Exit (0 );
}
Close (clnt_sock );
}
Return 0;
}
Void zombie_collector (int cpid ){
Int zid;
While (zid = waitpid (cpid, NULL, 0)> 0 );
If (zid =-1 & errno! = ECHILD)
Exit_error ("waitpid () failed ");
}


Void * service_thread (void * arg)
{
Int csock;

Pthread_detach (pthread_self ());
Csock = * (int *) arg;
Free (arg );

Do_HTTPservice (csock );
Close (csock );
}

Void do_HTTPservice (int clnt_sock)
{
Int line_len;
Char req_line [LINESIZE];
Char method [5];
Char file_name [2, 128];
Char path_name [MAXDIRLEN];

While (line_len = read_line (clnt_sock, req_line )){
If (strstr (req_line, "HTTP ")){
Strcpy (method, strtok (req_line ,""));
Strcpy (file_name, strtok (NULL ,""));
}
}
If (strcmp (method, "GET ")! = 0 ){
Send_error400 (clnt_sock );
}
Strcpy (path_name, base_dir );
If (file_name [0] = '/')
Strcat (path_name, file_name + 1 );
Else
Strcat (path_name, file_name );

Send_file (clnt_sock, path_name );
}


Int read_line (int sock, char * line)
{
Char buf [LINESIZE];
Int len, pos;
Char ch;

Pos = 0;

While (1 ){
Ch = '\ 0 ';
Len = recv (sock, & ch, 1, 0 );

If (len <0)
Exit_error ("recv () error in read_line () \ n ");

If (len = 0 ){
Strcpy (line, buf );
Return pos;
}
Buf [pos ++] = ch;
If (pos> = 1) & (buf [pos-1] = '\ n ')){
If (pos> = 2) & (buf [pos-2] = '\ R ')){
Buf [pos-2] = '\ 0 ';
Pos-= 2;
Break;
}
Else {
Buf [pos-1] = '\ 0 ';
Pos-= 1;
Break;
}
}
}
Strcpy (line, buf );
Return (pos );
}


Void send_file (int clnt_sock, char * path_name)
{
FILE * fp;
Char status [] = "HTTP/1.1 200 OK \ r \ n ";
Char server [] = "server: My HTTP Server \ r \ n ";
Char conn [] = "Connection: Close \ r \ n ";
Char clength [] = "Content-length: 1024 \ r \ n ";
Char ctype [] = "Content-type: text/html \ r \ n ";
Char buf [LINESIZE];
If (fp = fopen (path_name, "r") = NULL ){
Send_error404 (clnt_sock );
Return;
}
Send (clnt_sock, status, sizeof (status), 0 );
Send (clnt_sock, server, sizeof (server), 0 );
Send (clnt_sock, conn, sizeof (conn), 0 );
Send (clnt_sock, clength, sizeof (clength), 0 );
Send (clnt_sock, ctype, sizeof (ctype), 0 );

While (fgets (buf, LINESIZE, fp )! = NULL)
{
Strtok (buf, "\ n ");
Send (clnt_sock, buf, strlen (buf), 0 );
}
Fclose (fp );
}


Void send_error400 (int clnt_sock)
{
Char protocol [] = "http/1.1 400 Bad request \ r \ n ";
Char server [] = "server: My HTTP Server \ r \ n ";
Char conn [] = "Connection: Close \ r \ n ";
Char clength [] = "Content-length: 1024 \ r \ n ";
Char ctype [] = "Content-type: text/html \ r \ n ";
Char message [] ="400 :\
Bad Request Method!";
Send (clnt_sock, protocol, sizeof (protocol), 0 );
Send (clnt_sock, server, sizeof (server), 0 );
Send (clnt_sock, conn, sizeof (conn), 0 );
Send (clnt_sock, clength, sizeof (clength), 0 );
Send (clnt_sock, ctype, sizeof (ctype), 0 );
Send (clnt_sock, message, sizeof (message), 0 );
}

Void send_error404 (int clnt_sock)
{
Char protocol [] = "http/1.1 400 Not Found \ r \ n ";
Char server [] = "server: My HTTP Server \ r \ n ";
Char conn [] = "Connection: Close \ r \ n ";
Char clength [] = "Content-length: 1024 \ r \ n ";
Char ctype [] = "Content-type: text/html \ r \ n ";
Char message [] ="404 FIle :\
Not found!";

Send (clnt_sock, protocol, sizeof (protocol), 0 );
Send (clnt_sock, server, sizeof (server), 0 );
Send (clnt_sock, conn, sizeof (conn), 0 );
Send (clnt_sock, clength, sizeof (clength), 0 );
Send (clnt_sock, ctype, sizeof (ctype), 0 );
Send (clnt_sock, message, sizeof (message), 0 );
}
Void exit_error (char * error_message)
{
Perror (error_message );
Exit (1 );
}

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.