Question: Extend the HTTP _ Server by adding code to the connection _ handler ( ) function that breaks down the HTTP request from the client
Extend the HTTPServer by adding code to the connectionhandler function that breaks down the HTTP request from the client into method, url, and protocol variables. Write a concatenated response that includes the hello, method, url, and protocol variables.
The output should look something like:
HTTP OK
ContentType: texthtml
ContentLength:
Method: s
URL: s
Protocol: s
Hello, world!
End the output with two new lines
Your buffer variable, what you read from the client, should be characters long. Your method variable should be characters long. Your url variable should be characters long, and your protocol variable should be characters long. Use the sscanf function to extract information pieces from the buffer variable.
Server.c
#include
#include
#include
#include
#include
#include
#define PORT
void connectionhandlervoid socketdesc
int sock intsocketdesc;
char hello HTTP OK
ContentType: texthtml
ContentLength:
Hello, world!";
writesock hello, strlenhello;
printfResponse sent
;
closesock;
freesocketdesc;
return NULL;
int main
int serverfd newsocket, newsock;
struct sockaddrin address;
int addrlen sizeofaddress;
Creating socket file descriptor
if serverfd socketAFINET, SOCKSTREAM,
perrorsocket failed";
exitEXITFAILURE;
address.sinfamily AFINET;
address.sinaddr.saddr INADDRANY;
address.sinport htons PORT ;
Forcefully attaching socket to the port
if bindserverfdstruct sockaddr &address, sizeofaddress
perrorbind failed";
exitEXITFAILURE;
if listenserverfd
perrorlisten;
exitEXITFAILURE;
while
if newsocket acceptserverfdstruct sockaddr &address, socklent&addrlen
perroraccept;
exitEXITFAILURE;
pthreadt thread;
newsock mallocsizeofint;
newsock newsocket;
if pthreadcreate&thread, NULL, connectionhandler, voidnewsock
perrorcould not create thread";
exitEXITFAILURE;
Detach the thread so it doesn't need to be joined
pthreaddetachthread;
return ;
Client.c
#include
#include
#include
#include
#include
#include
#define PORT
int main
int sock valread;
struct sockaddrin servaddr;
char hello "GET HTTP
Host: localhost
;
char buffer;
if sock socketAFINET, SOCKSTREAM,
printf
Socket creation error
;
return ;
servaddr.sinfamily AFINET;
servaddr.sinport htonsPORT;
ifinetptonAFINET, &servaddr.sinaddr
printf
Invalid address Address not supported
;
return ;
if connectsockstruct sockaddr &servaddr, sizeofservaddr
printf
Connection Failed
;
return ;
sendsock hello strlenhello;
printfGET message sent
;
valread read sock buffer, ;
printfs
buffer ;
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
