Report Assign 10

14
EEP773 Telecom Software Lab Assignment 10 15 October, 2013 Gautam Kumar 2013JTM2520 Indian Institute of Technology, Delhi

description

Report assignment

Transcript of Report Assign 10

Page 1: Report Assign 10

EEP773

Telecom Software Lab

Assignment 10

15 October, 2013

Gautam Kumar

2013JTM2520

Indian Institute of Technology, Delhi

Page 2: Report Assign 10

Contents

1 PROBLEM STATEMENT 41.1 Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

2 ASSUMPTIONS 5

3 LOGIC USED 63.1 Establishing Connection between Server and Client: General

Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63.1.1 Client Side . . . . . . . . . . . . . . . . . . . . . . . . . 63.1.2 Server Side . . . . . . . . . . . . . . . . . . . . . . . . 6

3.2 Implementation of the operations for PART 1: Problem Specific 73.3 Modification for PART 2: Single server Multiple Clients . . . . 9

4 OUTPUT SNAPSHOTS 114.1 Starting the server . . . . . . . . . . . . . . . . . . . . . . . . 114.2 Starting the client . . . . . . . . . . . . . . . . . . . . . . . . . 124.3 Sample Output . . . . . . . . . . . . . . . . . . . . . . . . . . 13

EEP773 Telecom Software Lab: Assignment 10. Gautam Kumar. 15 October, 2013.

Page 3: Report Assign 10

List of Figures

3.1 Sequence of steps occuring in client-server communication. . . 10

4.1 Running the server program . . . . . . . . . . . . . . . . . . . 114.2 Running the client program . . . . . . . . . . . . . . . . . . . 124.3 Sample Output . . . . . . . . . . . . . . . . . . . . . . . . . . 13

EEP773 Telecom Software Lab: Assignment 10. Gautam Kumar. 15 October, 2013.

Page 4: Report Assign 10

List of Tables

4.1 Running the server program . . . . . . . . . . . . . . . . . . . 114.2 Running the client program . . . . . . . . . . . . . . . . . . . 12

EEP773 Telecom Software Lab: Assignment 10. Gautam Kumar. 15 October, 2013.

Page 5: Report Assign 10

Chapter 1

PROBLEM STATEMENT

1.1 Statement

Write a server client application where the following task will be fulfilled -

1. Server will run the arithmetic unit which will process the binary streamsfor the operation listed as addition(ADD), subtraction(SUB), 2’s com-plement(NOT2), 1’s complement(NOT), bitwise ’and’(AND), bitwise’or’(OR), bitwise ’xor’(XOR), compare for greater (CMPG) and com-pare for lesser (CMPL).

2. Input to client in the form of string -<operator> <1st i/p binary stream> <2nd i/p binary stream>

3. Output will be in the form of string -<answer o/p binary stream>

4. Binary string should not have leading zeros in input and output.

5. Client will facilitate the input and output of streams.

6. Arithmetic operation will be done at server.

7. Server handles one operation in one session.

PART 1: Write a code for single server serving to single client.

PART 2: Write a code for single server serving to multiple client.

EEP773 Telecom Software Lab: Assignment 10. Gautam Kumar. 15 October, 2013.

Page 6: Report Assign 10

Chapter 2

ASSUMPTIONS

The assumptions used in writing the program for the given problem are asfollows:

• The length of binary streams input as operands should not exceed 1000.This is as per the problem statement given to us.

• The mode of communication between server and client is half-duplex.It means that only one can speak at any point of time and at that timethe other must be listening.

• Address format used is of “Internet domain”.

• Socket type implemented is “Stream Socket”.

EEP773 Telecom Software Lab: Assignment 10. Gautam Kumar. 15 October, 2013.

Page 7: Report Assign 10

Chapter 3

LOGIC USED

The logic used in writing the program is described below

3.1 Establishing Connection between Server

and Client: General Flow

The sequence of steps followed during communication between client andserver is described below and also depicted in Figure 3.1.

3.1.1 Client Side

The steps involved in establishing a socket on the client side are as follows:

• Create a socket with the socket() system call

• Connect the socket to the address of the server using the connect()system call

• Send and receive data. There are a number of ways to do this, but thesimplest is to use the read() and write() system calls.

3.1.2 Server Side

The steps involved in establishing a socket on theserverside are as follows:

• Create a socket with the socket() system call

• Bind the socket to an address using the bind() system call. For a serversocket on the Internet, an address consists of a port number on the hostmachine.

EEP773 Telecom Software Lab: Assignment 10. Gautam Kumar. 15 October, 2013.

Page 8: Report Assign 10

3.2 Implementation of the operations for PART 1: Problem Specific 7

• Listen for connections with the listen() system call

• Accept a connection with the accept() system call. This call typicallyblocks until a client connects with the server.

• Send and receive data.

3.2 Implementation of the operations for PART

1: Problem Specific

1. Once the communication channel has been established, the server sendsthe options menu to the client.

2. The client reads the data and displays the same on its display.

3. Then the user inputs any command. The client reads the commandand writes the same to the server. Note that no processing is doneof any kind at the client side. Whatever the user inputs is sent tothe server.

4. The server reads the data and do the following:

(a) Validates the data received as follows:

Operator It must be among one of the 10 options supported:ADD, SUB, NOT, NOT2, AND, OR, XOR, CMPG, CMPL,EXIT.

Number of Inputs In case of NOT and NOT2 operators, thenumber of inputs must be 2 and otherwise, it must be three.

Operands They must be stream of 0’s and 1’s only.

(b) Converts the stream of 0’s and 1’s from char format to booleanformat. Also, 0’s are padded at the MSB side to make the lengthof streams the same for ease in further operations.

(c) Performs the arithmetic or logical operation as per the Operationinput by user.

ADD A function for adding two one bit booleans is written whichtakes carry also. Another function for calculating the carryupon addition is written. These are called for each pair ofinput bits to calculate the sum of binary stream.

EEP773 Telecom Software Lab: Assignment 10. Gautam Kumar. 15 October, 2013.

Page 9: Report Assign 10

3.2 Implementation of the operations for PART 1: Problem Specific 8

SUB TO calculate A - B, we first calculate the 2’s complement ofB and add it to A. To calculate 2’s complement, 1’s comple-ment is calculated first by flipping the boolean bits and thenadding one to it.

NOT Simply the bits are flipped.

NOT2 The bits are flipped first and then one is added to theresult to obtain the 2’s complement.

AND The logical operator && is applied on each bit pair of thebinary streams.

OR The logical operator || is applied on each bit pair of the bi-nary streams.

XOR The logical operator && and || are applied on each bit pairof the binary streams as per the realtion given in Equation 3.1.

result[i] = (operandA[i] && !operandB[i]) ||(!operandA[i] && operandB[i])

(3.1)

CMPG In this case, we calculate A - B and check the carry bitto decide the sign of the result. The value stored in carryis basically the result. So, when A>B, then 1 is displayed,otherwise 0 is displayed.

CMPL In this case, we calculate A - B and check the carry bitto decide the sign of the result. Result is the flipped valueof carry. So, when A>B, then 0 is displayed, otherwise 1 isdisplayed.

EXIT If exit is input by the user, then the client program termi-nates while server keeps on running. This means that a clientcan again connect to the server and access it for operations.

(d) Once the result has been obtained, it is written back to the clientand the server then waits in the read state for any new data sentfrom the client.

EEP773 Telecom Software Lab: Assignment 10. Gautam Kumar. 15 October, 2013.

Page 10: Report Assign 10

3.3 Modification for PART 2: Single server Multiple Clients 9

5. The client receives the data and displays this result on its terminal.

6. The client then waits for any new input from the user and upon gettingit, sends the same to the server. This cycle goes on forever.

3.3 Modification for PART 2: Single server

Multiple Clients

For handling multiple clients, the function fork() has been used whose work-ing can be summarized as following.

System call fork() is used to create processes. It takes no arguments andreturns a process ID. The purpose of fork() is to create a new process, whichbecomes the child process of the caller. After a new child process is created,both processes will execute the next instruction following the fork() systemcall. Therefore, we have to distinguish the parent from the child.

This can be done by testing the returned value of fork():

• If fork() returns a negative value, the creation of a child process wasunsuccessful.

• fork() returns a zero to the newly created child process.

• fork() returns a positive value, the process ID of the child process, to theparent. The returned process ID is of type pid t defined in sys/types.h.Normally, the process ID is an integer. Moreover, a process can usefunction getpid() to retrieve the process ID assigned to this process.

In our problem, we do the following:

• We put the accept() system call in a loop.

• Whenever a clent connects to the server, we use system call fork() tostart a new process and then call a function in the child process thathandles all the communication and operation requests from that client.

EEP773 Telecom Software Lab: Assignment 10. Gautam Kumar. 15 October, 2013.

Page 11: Report Assign 10

3.3 Modification for PART 2: Single server Multiple Clients 10

Figure 3.1: Sequence of steps occuring in client-server communication.

EEP773 Telecom Software Lab: Assignment 10. Gautam Kumar. 15 October, 2013.

Page 12: Report Assign 10

Chapter 4

OUTPUT SNAPSHOTS

In this chapter, an example is shown demonstrating the way the client-serverprogram works.

4.1 Starting the server

S. N. Command Line Argument Value

1. Port number 55555

Table 4.1: Running the server program

Figure 4.1: Running the server program

EEP773 Telecom Software Lab: Assignment 10. Gautam Kumar. 15 October, 2013.

Page 13: Report Assign 10

4.2 Starting the client 12

S. N. Command Line Argument Value

1. Server name localhost

2. Port number 55555

Table 4.2: Running the client program

4.2 Starting the client

Figure 4.2: Running the client program

EEP773 Telecom Software Lab: Assignment 10. Gautam Kumar. 15 October, 2013.

Page 14: Report Assign 10

4.3 Sample Output 13

4.3 Sample Output

Figure 4.3: Sample Output

EEP773 Telecom Software Lab: Assignment 10. Gautam Kumar. 15 October, 2013.