







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








HTTP is the network protocol of the Web. It is both simple and powerful. Knowing HTTP enables you to write Web browsers, Web servers, automatic page downloaders, link-checkers, and other useful tools.
HTTP stands for Hypertext Transfer Protocol. It's the network protocol used to deliver virtually all files and other data (collectively called resources) on the World Wide Web, whether they're HTML files, image files, query results, or anything else. Usually, HTTP takes place through TCP/IP sockets.
HTTP is used to transmit resources, not just files. A resource is some chunk of information that can be identified by a URL (it's the R in URL). The most common kind of resource is a file, but a resource may also be a dynamically-generated query result, the output of a CGI script, a document that is available in several languages, or something else.
Server Client Specifications A browser is an HTTP client because it sends requests to an HTTP server (Web server), which then sends responses back to the client. The standard (and default) port for HTTP servers to listen on is 80, though they can use any port. Here in our case we are using random port from 40000 to 65535.
While learning HTTP, it may help to think of a resource as similar to a file, but more general. As a practical matter, almost all HTTP resources are currently either files or server-side script output.
Working Principle Like most network protocols, HTTP uses the client-server model: An HTTP client opens a connection and sends a request message to an HTTP server; the server then returns a response message, usually containing the resource that was requested. After delivering the response, the server closes the connection (making HTTP a stateless protocol, i.e. not maintaining any connection information between transactions).
To retrieve the file at the URL
http://www.somehost.com/index.html
first open a socket to the host www.somehost.com, port 80 (use the default port of 80 because none is specified in the URL). Then, send something like the following through the socket:
Following is the way how browser rend request to the server. GET index.html HTTP/1. From: [email protected] User-Agent: HTTPTool/1.
The server should respond with something like the following, sent back through the same socket: HTTP/1.0 200 OK Date: Fri, 31 Feb 2012 23:59:59 GMT Content-Type: text/html Content-Length: 1354
Happy New Millennium! (more file contents) . . .
After sending the response, the server closes the socket.
In the server code, a file name ‘logfile’ is also created which purpose is to store the major activities of the server which are as follows
Update the file regularly after some specific time duration with a time statement generated in task 4. Record the client information i.e. its ip address, port number etc and also the record of service which has been provided to the client.
Filing is used for this purpose. We create a file which is stored in the hard disk for record purposes.
Main Steps involved in this task are as follows
A file pointer is created. A text file is created with the name ‘logfile’ , each time the server is executed a new file with the mentioned title is created , if there is already a text file with the same title it will be replaced by this new empty file so that we may have the recent history of the server. each time we want to update the ‘logfile’ ,it undergoes the following three steps o it is opened in the binary format o data is written in the file o the file is closed
*FILE out; 1 st^ we have to declare a file pointer as written above.
To create or open a file out=fopen("log_file.txt","wb");
The first argument of this function is the name of the file which is to be created or opened (if already created) and the second argument is the purpose of opening.
To write in the file fprintf(out,”text to be written %s”,string);
This function is used to write the data to the file. 1st^ argument is referring to the location where data is to be stored i.e. the pointer to the corresponding file, 2nd^ and 3rd^ arguments are referring to the data which is to be stored
fclose(out); This statement closes the pointer and saves the record.
1. Exit from the accept() function after that specified fixed duration and update the time statement and then again comes back to the accept() function and remains in the blocking state where it was. 2. Another solution is to create a new thread, which purpose is to manage the time statement where as the server task will not be disturbed, it will be executing in parallel
We chose the 2nd^ solution ( Threading ) i.e. create a new thread and assign it the time statement task , which will be doing the polling mechanism and after some fixed duration it will update the log file(task
Two functions that we used in the accomplishment of the new thread are as follows
As soon as the new thread is created this ‘threadFunc’ will start to execute.
Its 1st^ argument is the thread identifier. It must be declared in the start by defining it as follows :
pthread_t pth;
#include
Note: to compile the server file now u have to write the command as follows
Command gcc server.c – o server -lpthread
This – lpthread is mentiond here to link the header file pthread.h
In the simulation we executed server on one shell and then three clients iteratively made a connection with server and log file is created