Assignment 10 15 October, 2013 Gautam Kumar 2013JTM2520 Indian Institute of Technology, Delhi Contents 1 PROBLEM STATEMENT 4 1.1 Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 2 ASSUMPTIONS 5 3 LOGIC USED 6 3.1 Establishing Connection between Server and Client: General Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 3.1.1 Client Side . . . . . . . . . . . . . . . . . . . . . . . . . 6 3.1.2 Server Side . . . . . . . . . . . . . . . . . . . . . . . . 6 3.2 Implementation of the operations for PART 1: Problem Specic 7 3.3 Modication for PART 2: Single server Multiple Clients . . . . 9 4 OUTPUT SNAPSHOTS 11 4.1 Starting the server . . . . . . . . . . . . . . . . . . . . . . . . 11 4.2 Starting the client . . . . . . . . . . . . . . . . . . . . . . . . . 12 4.3 Sample Output . . . . . . . . . . . . . . . . . . . . . . . . . . 13 EEP773 Telecom Software Lab: Assignment 10. Gautam Kumar. 15 October, 2013. List of Figures 3.1 Sequence of steps occuring in client-server communication. . . 10 4.1 Running the server program . . . . . . . . . . . . . . . . . . . 11 4.2 Running the client program . . . . . . . . . . . . . . . . . . . 12 4.3 Sample Output . . . . . . . . . . . . . . . . . . . . . . . . . . 13 EEP773 Telecom Software Lab: Assignment 10. Gautam Kumar. 15 October, 2013. List of Tables 4.1 Running the server program . . . . . . . . . . . . . . . . . . . 11 4.2 Running the client program . . . . . . . . . . . . . . . . . . . 12 EEP773 Telecom Software Lab: Assignment 10. Gautam Kumar. 15 October, 2013. Chapter 1 PROBLEM STATEMENT 1.1 Statement Write a server client application where the following task will be fullled - 1. Server will run the arithmetic unit which will process the binary streams for the operation listed as addition(ADD), subtraction(SUB), 2s com- plement(NOT2), 1s 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. Chapter 2 ASSUMPTIONS The assumptions used in writing the program for the given problem are as follows: 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 time the 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. 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 and server 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 the simplest 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 server socket on the Internet, an address consists of a port number on the host machine. EEP773 Telecom Software Lab: Assignment 10. Gautam Kumar. 15 October, 2013. 3.2 Implementation of the operations for PART 1: Problem Specic 7 Listen for connections with the listen() system call Accept a connection with the accept() system call. This call typically blocks until a client connects with the server. Send and receive data. 3.2 Implementation of the operations for PART 1: Problem Specic 1. Once the communication channel has been established, the server sends the 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 command and writes the same to the server. Note that no processing is done of any kind at the client side. Whatever the user inputs is sent to the 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, the number of inputs must be 2 and otherwise, it must be three. Operands They must be stream of 0s and 1s only. (b) Converts the stream of 0s and 1s from char format to boolean format. Also, 0s are padded at the MSB side to make the length of streams the same for ease in further operations. (c) Performs the arithmetic or logical operation as per the Operation input by user. ADD A function for adding two one bit booleans is written which takes carry also. Another function for calculating the carry upon addition is written. These are called for each pair of input bits to calculate the sum of binary stream. EEP773 Telecom Software Lab: Assignment 10. Gautam Kumar. 15 October, 2013. 3.2 Implementation of the operations for PART 1: Problem Specic 8 SUB TO calculate A - B, we rst calculate the 2s complement of B and add it to A. To calculate 2s complement, 1s comple- ment is calculated rst by ipping the boolean bits and then adding one to it. NOT Simply the bits are ipped. NOT2 The bits are ipped rst and then one is added to the result to obtain the 2s complement. AND The logical operator && is applied on each bit pair of the binary 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 pair of 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 bit to decide the sign of the result. The value stored in carry is 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 bit to decide the sign of the result. Result is the ipped value of carry. So, when A>B, then 0 is displayed, otherwise 1 is displayed. EXIT If exit is input by the user, then the client program termi- nates while server keeps on running. This means that a client can again connect to the server and access it for operations. (d) Once the result has been obtained, it is written back to the client and the server then waits in the read state for any new data sent from the client. EEP773 Telecom Software Lab: Assignment 10. Gautam Kumar. 15 October, 2013. 3.3 Modication 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 getting it, sends the same to the server. This cycle goes on forever. 3.3 Modication 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 and returns a process ID. The purpose of fork() is to create a new process, which becomes the child process of the caller. After a new child process is created, both processes will execute the next instruction following the fork() system call. 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 was unsuccessful. fork() returns a zero to the newly created child process. fork() returns a positive value, the process ID of the child process, to the parent. The returned process ID is of type pid t dened in sys/types.h. Normally, the process ID is an integer. Moreover, a process can use function 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() to start a new process and then call a function in the child process that handles all the communication and operation requests from that client. EEP773 Telecom Software Lab: Assignment 10. Gautam Kumar. 15 October, 2013. 3.3 Modication 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. Chapter 4 OUTPUT SNAPSHOTS In this chapter, an example is shown demonstrating the way the client-server program 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. 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. 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.