Docsity

Interprocess Communication By Sockets 1-Networking-Lab Report, Exercises of Computer Networks

Partial preview of the text

Download Interprocess Communication By Sockets 1-Networking-Lab Report and more Exercises Computer Networks in PDF only on Docsity!

LAB

REPORT

INTERPROCESS

COMMUNICATION BY

SOCKETS

Contents

  • Interprocess Communication........................................................................................................................
  • Tasks Assigned
  • Task 1: Server program should be able to receive and store a text file in its local directory
  • Task 2: Server program should be able to respond with an HTML based welcome message......................
    • HTTP
      • Server Client Specifications
      • Working Principle
    • Simulation Results.....................................................................................................................................
  • Task 3: Server Program Should Be Able To Log Its Activity...........................................................................
    • Methodology.............................................................................................................................................
    • Important Functions Used In The Above Mentioned Steps And Declaration For Filing
  • five minutes. Task 4: Server should display "Waiting for a client, current time is hh:mm:ss, dd/mm/yyyy" after every
    • accept() function
    • Problem
    • Proposed Solutions
    • Chosen solution
    • Threading
    • library function........................................................................................................................................
  • Simulation results of Task 3 & Task
    • Server side...............................................................................................................................................
    • Client Side
    • Log file

Task 2: Server program should be able to respond with an HTML based

welcome message

HTTP

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.

Simulation Results

After typing the web address http://172.30.10.142:53945/ we get the request from the client in

following form:

And after receiving request we send following html file to the html client (internet explorer)

HTTP/1.1 200 OK

Date: Fri, 31 Feb 2012 23:59:59 GMT

Content-Type: text/html

Content-Length: 99

WELCOME MESSAGE BY GROUP1

Following figure show the corresponding result of sending the file to the html client.

Task 3: Server Program Should Be Able To Log Its Activity

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.

Methodology

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

Important Functions Used In The Above Mentioned Steps And Declaration For

Filing

 *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.

Proposed Solutions

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

Chosen solution

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

  1. with the new current time in the above mentioned format.

Threading

Two functions that we used in the accomplishment of the new thread are as follows

  1. void * threadFunc (void *arg) This is the own made function in which we write the code to update the time statement.
  2. pthread_create (&pth,NULL,threadFunc,"processing...") This function is library function and it is used to create a thread which purpose is to execute the function given in the third argument. Here we have given the third argument as ‘threadFunc’ ,which is basically the above mentioned function.

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;

library function

#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

Simulation results of Task 3 & Task 4

In the simulation we executed server on one shell and then three clients iteratively made a connection with server and log file is created

Server side

Client Side

Log file