58
Dr.NNCE MCA/IV Sem Network Programming -LM MC9247 NETWORK PROGRAMMING LAB LABORATORY MANUAL FOR FOURTH SEMESTER MCA (FOR PRIVATE CIRCULATION ONLY) ANNA UNIVERSITY ,CHENNAI DEPARTMENT OF MASTER OF COMPUTER APPLICATIONS DR. NAVALAR NEDUNCHEZHIYAN COLLEGE OF ENGINEERING, THOLUDUR- 606 303, CUDDALORE DISTRICT 1

MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

  • Upload
    lamdan

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

Page 1: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV Sem Network Programming -LM

MC9247 NETWORK PROGRAMMING LAB

LABORATORY MANUAL FOR

FOURTH SEMESTER MCA

(FOR PRIVATE CIRCULATION ONLY)

ANNA UNIVERSITY ,CHENNAI

DEPARTMENT OF MASTER OF COMPUTER APPLICATIONS DR.

NAVALAR NEDUNCHEZHIYAN COLLEGE OF ENGINEERING,

THOLUDUR- 606 303, CUDDALORE DISTRICT

1

Page 2: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

2

GENERAL INSTRUCTIONS FOR LABORATORY CLASSES o Enter the Lab with CLOSED FOOTWEAR.

o Boys should “TUCK IN” the shirts.

o Students should wear UNIFORM ONLY.

o LONG HAIR should be protected, let it not be loose especially near ROTATING

MACHINERY.

o Any other machines / equipments should not be operated other than the prescribed one for that day.

o POWER SUPPLY to your test table should be obtained only through the LAB

TECHNICIAN.

o Do not LEAN and do not be CLOSE to the rotating components.

o TOOLS,APPARATUS and GUAGE sets are to be returned before leaving the lab.

o HEADINGS and DETAILS should be neatly written i. Aim of the experiment

ii. Apparatus / Tools / Instruments required

iii. Procedure / Theory / Algorithm / Program

iv. Model Calculations

v. Neat Diagram / Flow charts

vi. Specifications / Designs Details

vii. Tabulations

viii. Graph

ix. Result / discussions

x. Before doing the experiment, the student should get the Circuit / Program approval by

the FACULTY – IN – CHARGE.

xi. Experiment date should be written in the appropriate place.

xii. After completing the experiment, the answer to the viva-voice questions should be

neatly written in the workbook.

xiii. Be PATIENT, STEADY, SYSTEMATIC AND REGULAR.

Page 3: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

3

HARDWARE REQUIREMENTS

Intel Pentium 915 GV 80GB SATA II 512 MB DDR

SOFTWARE REQUIREMENTS:

Unix server

UNIVERSITY PRACTICAL EXAMINATION ALLOTMENT OF MARKS

Internal assessment = 20 Marks Practical assessment = 80 Marks

---------------- Total 100 Marks

---------------- INTERNAL ASSESSMENT(20 MARKS)

Staff should maintain the assessment Register and the Head of the Department should monitor it .

SPLIT UP INTERNAL MARKS Observation note = 3 Marks Record note = 7 Marks CIA-I = 2 Marks CIA-II = 2 Marks Model Exam = 3 Marks Attendance = 3 Marks

---------------- Total 20 Marks

--------------- UNIVERSITY EXAMINATION

The exam will be conducted for 100 marks. Then the marks will be calculated to 80 marks.

SPLIT UP OF PRACTICAL EXAMINATION MARKS

Aim and Algorithm = 20 Marks Program = 40 Marks Output = 20 Marks Result = 10 Marks Viva-voce = 10 Marks

----------------- Total 100 Marks

-----------------

Page 4: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

4

MC9247 NETWORK PROGRAMMING LAB (ANNA UNIVERSITY ,CHENNAI)

Regulation : 2009 BATCH : 2012 - 2015

LIST OF EXPERIMENTS

1. Socket Programming

a. TCP Sockets

b. UDP Sockets

c. Applications using Sockets

2. Simulation of Sliding Window Protocol

3. Simulation of Routing Protocols

4. RPC

5. Development of applications such as DNS/ HTTP/ E – mail/ Multi - user Chat

Page 5: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

5

CONTENTS

S.No

LIST OF EXPERIMENTS

Page. No.

1. Program using basic network commands 6 2.

(A)Socket Program For Echo Command 11

(B)Socket Program For Talk Command 13

(C) Socket Program For Ping Command 16 3.

A)Program using TCP sockets

(client and server)

19

B)Program using UDP sockets

(client and server)

23

4. Simulation of Sliding Window Protocol 26

5. Simulation of Routing Protocol 29

6. Program to perform RPC

( Remote Procedure Call)

32

7. Application of DNS 35

8. HTTP server for web page download 40

9. Application of e-mail 44

10 Multi-user chat application 48

BEYOND THE SYLLABUS

11. Cyclic Redundancy Check 51

12. Implementation of UDP client server communication using bind

system call

54

Page 6: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

6

Exercise Number: 1

Title of the Exercise : BASIC NETWORK COMMANDS

Date of the Exercise :

STEP 1: OBJECTIVE (AIM) OF THE EXPERIMENT

To execute the various unix commands STEP 2: FACILITIES REQUIRED AND PROCEDURE

a) Facilities Required To Do The Experiment:

Sl.No Facilities Required Quantity

1 Unix server 1

b) Procedure for doing the experiment:

Step no. Details of the Step

1 Enter some Unix commands

2 Result is display

3 Change the options

4 Get the corresponding results

c) Program & d) Output: 1.WHO AM I:-

This command is used to display the users of the system presently worked as login name, terminal, and time login. Syntax:-$who am I

Ex:- $ who am I

2. MKDIR:- This command is used to create a new directory or sub directory.

Syntax:- $Mkdir<file name> Ex:- $mkdir stars

3. CD:- 4. CAT:-

This command is used to change the Directory. Syntax:- $cd<dir name> Ex:- $cd stars

This command is used to create a New file and Store the Information, If we want to save Ctrl+D keys.

Syntax:- Cat>pverb There is no way to success except hardwork. Hardwork is a best key to success.

If you maintain self descipline, devotion self Confidence means

You will be the first and best . ^d

Page 7: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

7

5.CP:-

This command is used to

1.Copy one file to another file. 2. Copy one or more file to another drive with same file or different file. Syntax:- Cp<source file> <target file> Ex:-$cp pverb pro

6.DATE:- This command displays the current date. Syntax:- Date[options] Options:- %D- displays date as mm\dd\yy.

%a - abbreviated week day. %d - display only date. %m – display only month. %r – display the time as hh.mm.ss. %V – display the time.

Ex:- $date

mon mar 03 12:00:33 IST 2008

7.RM:-

$date+%d 03

$date+%D 03/03/08

$date+%a mon

$date+%m 03

$date+%r 12:10:44 pm

$date+%V 38

This command is used to remove the file.

Syntax:- rm<file name>

Ex:- rm pro 8.RMDIR:-

This command is used to Remove the directory. This command can be valid only when the following conditions are fulfilled.

There is no file in that directory when we can remove the directory. We should be out of directory,which we want to delete.

Syntax:- rmdir<dir name>

Ex:- $ rmdir stars 9.MV:-

file.

This command is used to move the information from one file to another

Syntax:-

Mv<source file><target file> Ex:- $ mv pverb pro

10.LS or DIR:-

Page 8: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

8

This command is display list all the files and sub directories. Syntax:-

Ex:-

Ls or dir $ ls

a.out fork.c odd.c queue.c ipc1.c ipc2.c server.c client.c shm.c mutex.c msg.c snd.c rcv.c stack.c fcfs.c sjf.c for.c poly.c stud.c fib.c

11.ECHO:- This command is used to print the message. Syntax:-

Echo”message” Ex:-

$ echo Welcome to 2006 to 2009 MCA Batch. 12.SORT:-

This command is used to sort the given names or files. Syntax:-

Sort<file name> Ex:-

Rose Babu

Cat>names

Kannan Sakthi Arul

Arul Babu Kannan Rose Sakthi

13.DIFF:-

$ sort names

To compare whether the contents are of equal in two files. Syntax:-

Diff<file name1><file name2> Ex:-

$ cat>mca1 God is great

$ cat>mca2 God great

$ diff mca1 mca2 lcl

14.CLEAR:-

<God is great -----

? God great

This command is used to clear the screen. Syntax:- Clear Ex:-

$ Clear

Page 9: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

9

15.MAN COMMAND OR HELP COMMAND: -

This command is used to help for any command. 16.WAIT: -

This command is used to wait until background process. 17.WAIT 1234: -

This command is used to wait till PID 1234 is completed. 18.KILL [PID]: -

This command is used to kill the process whose PID is mentioned. 19.CMP:-

This command is used to compare the two files. Options:-

a) Cmp file1, file2 - To compare two files byte by byte. b) Cmp-| file1 file2 - To list all difference in octal value.

20.HEAD:- This command is used to display the lines from the top of the file. Options:-

a) Head-n filename - To display n lines from the top of the file. b) Head-nc filename – To display the number of character.

21. TAIL:- This command is used to display the lines from the bottom of the file.

Options:- a) Tail-n filename - To display n lines from the bottom of the file. b) Tail+n filename – To display from the nth line to end of the file..

22.CUT:- Options:-

a) cut-c 1,4,7 filename – To cut and display fields 1,4,7 only if fields are separate by space or tab.

b) cut-c 1-20 filename - To cut and display 1 to 20 character in a file. 23.PASTE:-

Options:- a) paste file1 file2 – To paste and display from file1 to file2.

24.GREP:- This command is used to search a word from the file.

Options:- a) grep “billu” filename - To search a word “billu” in the file and display

that line. b) grep-v “rock” | filename - To search except the word rock in the file. c) grep “e$”| filename - To search for a line ending with e. d) grep”lf”| filename - To search for a line beginning with f.

25.SLEEP:- Options:-

a) sleep 10 – To sleep for 10 seconds. 26.PWD:-

This command is used to present working directory. 27.WHO:-

This command is used to details of all login. 28.TTY:-

This command is used to know your terminal type. 29.BANNER[TEXT]:-

Page 10: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

10

c) tput rmso - This command is used to opposite of smso. d) tput blink - This command is used to blinking output.

This command is used to display banner in a line. 30.DATE;WHO:-

This command is used to display date and details of terminal. 31.TPUT:

Options:- a) tput clear or clear - This command is used to clear the screen. b) tput smso - This command is used to set screen color foreground to

background.

32.MORE:- This command is used to display screen wise.

33.SPLIT: - This command is used to split by 10 times in a file.

34.BE: -

Options: - This command is used to as calculator.

Ctrl +d - Break of the calculator. 35.CD..: -

This command is used to return to previous directory. 36.UNAME: -

This command is used to display the version of Unix. Options:- a) uname_r - To display the version of Unix.

37.LOCK: -

This command is used to lock for some certain minutes. Options:- a) lock_10 - To lock for 10 minutes.

38.CAL: - This command is used to displays the calendar of specified month or year. Syntax: -

Cal [mmm][yyy] Ex:- $ Cal

e)Result:

Thus the above Program was successfully executed.

STEP 3: QUESTIONS AND ANSWERS: 1. How we get current date?

$ date 2. What is the use of echo command?

It display the given message 3. What is the use of short command?

The content of the file is display in the sorted order 4. How we can remove a directory?

$ rm dir_name 5. What is the use of who am I command?

It display the details about the current user

Page 11: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

11

Exercise Number: 2(A)

Title of the Exercise : SOCKET PROGRAM FOR ECHO COMMAND Date of the Exercise :

STEP 1: OBJECTIVE (AIM) OF THE EXPERIMENT To write a program in java to implement the echo concept

STEP 2: FACILITIES REQUIRED AND PROCEDURE

a) Facilities Required To Do The Experiment:

Sl.No Facilities Required Quantity

1 UNIX Server 1

b) Procedure for doing the experiment:

Step no. Details of the Step

1 Start the process

2 Create a file named output

3 Compile and run the server program

4 The client message will be displayed

5 Compile and run the client program

6 Enter the message

7 The message will be echoed on the server

8 Stop the process

c) Program: //echoclient.java import java.io.*; import java.net.*; public class echoclient { public static void main(String arg[])throws Exception

{ Socket c=null; DataInputStream usr_inp=null; DataInputStream din = new DataInputStream(System.in); DataOutputStream dout=null; try {

c=new Socket("10.201.140.198",1234); usr_inp = new DataInputStream(c.getInputStream()); dout = new DataOutputStream(c.getOutputStream()); }

catch(UnknownHostException e) { } catch(IOException f) { } if(c!=null || usr_inp!=null || dout!=null) { String unip;

while((unip= din.readLine())!=null)

Page 12: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

12

{ dout.writeBytes(""+unip); dout.writeBytes("\n"); System.out.println("The Echoed Message"); System.out.println(usr_inp.readLine()); System.out.println("Enter Your Message:"); }

System.exit(0);}din.close(); usr_inp.close();c.close(); } } //echoserver.java import java.io.*; import java.net.*; public class echoserver { public static void main(String args[])throws Exception

{ ServerSocket m=null; Socket c=null; DataInputStream usr_inp=null; DataInputStream din = new DataInputStream(System.in); DataOutputStream dout=null; try { m= new ServerSocket(1234); c=m.accept();

usr_inp= new DataInputStream(c.getInputStream()); dout= new DataOutputStream(c.getOutputStream()); }

catch(UnknownHostException e) { } catch(IOException g) { } if(c!=null || usr_inp!=null) { String unip; while(true)

{ System.out.println("Message From Client.."); String m1=usr_inp.readLine();System.out.println(m1);

dout.writeBytes(""+m1);dout.writeBytes("\n"); } } dout.close(); usr_inp.close(); c.close(); } }

d) Output: Enter the message WELCOME

Message from the client WELCOME

e) Result:

Thus the above Program was successfully executed STEP 3: QUESTIONS AND ANSWERS:

1. What is the use of echo command?

It is used to display the same message

2. What is syntax of the echo command Echo”message”

3. What is the use of socket?

Socket is used to connect client with the server. Using socket and echo command we can display the message which is print in the client in the server.

Page 13: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

13

Exercise Number: 2(B)

Title of the Exercise : SOCKET PROGRAM FOR TALK COMMAND Date of the Exercise :

STEP 1: OBJECTIVE (AIM) OF THE EXPERIMENT

To write a program in java to communicate client and server

STEP 2: FACILITIES REQUIRED AND PROCEDURE

a) Facilities Required To Do The Experiment:

Sl.No Facilities Required Quantity

1 UNIX Server 1

b) Procedure for doing the experiment:

Step no. Details of the Step

1 Start the process

2 Create a file named output

3 Compile and run the server program

4 The typed message will be displayed on client and the client will reply for it

5 Compile and run the client program

6 Enter the message in the client window

7 The client message will be displayed on the server & the server will reply for it

8 Stop the process

c) Program:

//TalkServer.java import java.io.*; import java.net.*; public class TalkServer{ public static void main(String args[]) throws Exception {

ServerSocket m=null; Socket c=null; DataInputStream usr_inp=null; DataInputStream din=new DataInputStream(System.in); DataOutputStream dout=null; try {

m=new ServerSocket(1234); c=m.accept(); usr_inp=new DataInputStream(c.getInputStream());

Page 14: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

14

dout=new DataOutputStream(c.getOutputStream()); } catch(UnknownHostException e){ } catch(IOException e ){} if(c!=null||usr_inp!=null){ String unip;

while(true) { System.out.println("Message from Client:");

String m1=usr_inp.readLine(); System.out.println(m1); System.out.println("Enter u'r Message:"); unip=din.readLine(); dout.writeBytes(""+unip); dout.writeBytes("\n"); } }

dout.close(); usr_inp.close(); c.close(); } }

//TalkClient.java import java.io.*; import java.net.*; public class TalkClient { public static void main(String args[]) throws Exception

{ Socket c=null; DataInputStream usr_inp=null; DataInputStream din=new DataInputStream(System.in); DataOutputStream dout=null; try { c=new Socket(InetAddress.getLocalHost(),1234);

usr_inp=new DataInputStream(c.getInputStream()); dout=new DataOutputStream(c.getOutputStream()); }

catch(UnknownHostException e){ } catch(IOException e ){} if(c!=null||usr_inp!=null||dout!=null) { String unip;

System.out.println("Enter the Message for The Server........."); while((unip=din.readLine())!=null) { dout.writeBytes(""+unip);

dout.writeBytes("\n"); System.out.println("Reply:");

System.out.println(usr_inp.readLine()); System.out.println("Enter your Message:");

} System.exit(0); } din.close(); usr_inp.close(); c.close(); }}

Page 15: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

15

d)Output:

SERVER

Message from the client

Welcome

Enter your message

INDIA CILENT

Enter the message for the server

Welcome Message from

the server INDIA

e)Result: Thus the above program was executed successfully.

STEP 3: QUESTIONS AND ANSWERS:

1. What is the use of talk command?

It is uaed to display or talk with the client/server.

2. What is the use of socket? Socket is used to connect client with the server. Using socket and talk command we can display the message which is print in the client in the server and also display the message which is print in the server in the client .

Page 16: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

16

Exercise Number: 2(C)

Title of the Exercise : SOCKET PROGRAM FOR PING COMMAND

Date of the Exercise :

STEP 1: OBJECTIVE (AIM) OF THE EXPERIMENT

To write a java program to execute the PING command STEP 2 :FACILITIES REQUIRED AND PROCEDURE

a) Facilities Required To Do The Experiment:

Sl.No Facilities Required Quantity

1 UNIX Server 1

b) Procedure for doing the experiment:

Step no. Details of the Step

Client Side: 1 Start the process

2 Establish the connection between server and client

3 Get the address of the client

4 Get the input message until the message is received

5 Print the number of packets sent and received

6 Stop the process

Server Side: 1 Start the process

2 Check whether the connection is accepted

3 Using the PrintWriter object print the message

4 Using BufferedReader store the packets

5 Read the message from the buffer

6 Print the message from PrintWriter

7 Stop the process

c) Program:

//pingser.java import java.io.*; import java.net.*; import java.util.*; import java.text.*;

Page 17: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

17

class pingser { public static void main(String a[])throws Exception { ServerSocket ss=new ServerSocket(5555);

Socket s=ss.accept(); int c=0; while(c<4) { DataInputStream dis=new DataInputStream(s.getInputStream());

PrintStream out=new PrintStream(s.getOutputStream()); String str=dis.readLine(); out.println("Reply

from"+ InetAddress.getLocalHost()+";Length"+str.length());

c++; }

s.close(); } } //pingcl.java import java.io.*; import java.net.*; import java.util.Calendar; class pingcl { public static void main(String a[])throws Exception

{ String str; int c=0; long t1,t2; Socket s=new Socket("localhost",5555);

DataInputStream dis=new DataInputStream(s.getInputStream()); PrintStream out=new PrintStream(s.getOutputStream()); while(c<4) { t1=System.currentTimeMillis();

str="welcome to network programming world"; out.println(str); System.out.println(dis.readLine()); t2=System.currentTimeMillis(); System.out.println(";TTL="+(t2-t1)+"ms"); c++;

} s.close();

} }

d) Output gcc myping.c –o myping /myping /myping 1.2.3.4 203.106.93.88 10000

Page 18: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

18

sendto() is OK sendto() is OK ----- ---- sendto() is OK sendto() is OK Sending to 203.106.93.88 from spoofed 1.2.3.4 sendto() is OK

e) Result:

Thus the above program was successfully executed. STEP 3: VIVA QUESTIONS AND ANSWERS:

1. What is the purpose of connect and bind function.

The connect function is used by a TCP client to establish a connection with a TCP

server. The bind function assigns a local protocol address to a socket. 2. What are the actions performed by listen function.

The listen function is called by TCP server and it performs the following actions .It

converts the unconnected socket into a passive socket, indicating that the kernel

should accept incoming connection requests directed to this socket .It specifies the

maximum number of connections that the kernel should queue for this socket. 3. Define concurrent servers and iterative servers.

The servers that can handle multiple clients simultaneously are called concurrent

servers. The servers that can handle multiple clients serially are called concurrent

servers.

Page 19: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

19

Exercise Number: 3(A)

Title of the Exercise : PROGRAM USING TCP SOCKETS

Date of the Exercise :

STEP 1: OBJECTIVE (AIM) OF THE EXPERIMENT

To create client server communication using TCP sockets

STEP 2: FACILITIES REQUIRED AND PROCEDURE

a) Facilities Required To Do The Experiment:

Sl.No Facilities Required Quantity

1 UNIX Server 1

b) Procedure for doing the experiment:

Step no. Details of the Step

Server Side: 1 Start the process

2 Create a TCP socket for sever.

3 Listen to the port for any connection request, if there is a valid connection accept the incoming connection request.

4 Server receive the message send by the client and transfer the same message to client.

5 Close the server socket.

6 Stop the process.

Client Side: 1 Start the process

2 Create a TCP socket for Client.

3 Make a connection with the server address.

4 Send the message to the server.

5 Receive the message from the server & print the message.

6 Stop the process.

c) Program: SERVER: #include<stdio.h>

Page 20: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

20

#include<unistd.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<netdb.h> #include<sys/time.h> #include<sys/shm.h> #include<sys/ipc.h> #define SERV_PORT 9854 #define MAXLINE 10000 #define LISTENQ 1024 void str_echo(); int main(int argc,char **argv) { int errno,listenfd,connfd; pid_t childpid; int *opt; socklen_t clilen; struct sockaddr_in cliaddr,servaddr; if((listenfd=socket(AF_INET,SOCK_STREAM,0))<0) perror("socket error"); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family =AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(SERV_PORT); setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,opt,sizeof(opt)); if(errno=bind(listenfd,(struct sockaddr_in*)&servaddr,sizeof(servaddr))<0) perror("bind error"); listen (listenfd,LISTENQ); for(;;) { clilen=sizeof(cliaddr); connfd=accept(listenfd,(struct sockaddr*)&cliaddr,&clilen); if((childpid=fork())==0) { close(listenfd); str_echo(connfd); exit(0);}}} void str_echo(int sockfd) { size_t n; char line[MAXLINE]; for(;;) { if((n=read(sockfd,line,MAXLINE))==0) return; write(sockfd,line,n);the name ".tcpserver.c.swp" write(sockfd,"message received\n",18); } }

CLIENT:

Page 21: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

21

#include<sys/types.h> #include<netdb.h> #include<unistd.h> #include<string.h> #include<sys/ipc.h> #include<stdio.h> #include<sys/socket.h> #include<sys/shm.h> #include<netinet/in.h> #define SERV_PORT 9854 #define MAXLINE 10000 #define LISTENQ 1024 void str_cli(); int main(int argc,char **argv) { int sockfd; struct sockaddr_in servaddr; if(argc!=2) perror("usage:tcpclient<IPaddress>"); sockfd=socket(AF_INET,SOCK_STREAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_port=htons(SERV_PORT); inet_pton(AF_INET,argv[1],&servaddr.sin_addr); connect(sockfd,(struct sockaddr_in*)&servaddr,sizeof(servaddr)); str_cli(stdin,sockfd); exit(0); } void str_cli(FILE *fp,int sockfd) { char sendline[MAXLINE],recvline[MAXLINE]; while(fgets(sendline,MAXLINE,fp)!=NULL) { write(sockfd,sendline,strlen(sendline)); if(read(sockfd,recvline,MAXLINE)==0) perror("server terminated prematurely"); fputs(recvline,stdout);}}

d) Output:

SERVER CLIENT cc server.c cc client.c ./a.out ./a.out SOCKET DESCRIPTOR 3 192.168.1.248 BINDING 0 SOCKET DESCRIPTOR 3 LISTENING 0 CONNECTING 0 BEFORE CONNECT -1 AFTER CONNECT 4 CLIENT CLIENT

Hai Hai

Page 22: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

22

SERVER SERVER Hello Hello CLIENT CLIENT Bye Bye CLIENT TERMINATED

e) Result:

Thus the above program was executes successfully. STEP 3: VIVA QUESTIONS AND ANSWERS:

1. What is the purpose of connect and bind function.

The connect function is used by a TCP client to establish a connection with a TCP

server. The bind function assigns a local protocol address to a socket. 2. What are the actions performed by listen function.

The listen function is called by TCP server and it performs the following actions .It

converts the unconnected socket into a passive socket, indicating that the kernel

should accept incoming connection requests directed to this socket 3. Define concurrent servers and iterative servers.

The servers that can handle multiple clients simultaneously are called concurrent

servers. The servers that can handle multiple clients serially are called concurrent

servers.

Page 23: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

23

Exercise Number: 3(B)

Title of the Exercise : PROGRAM USING UDP SOCKETS

Date of the Exercise :

STEP 1: OBJECTIVE (AIM) OF THE EXPERIMENT To create client server communication using UDP sockets STEP 2: FACILITIES REQUIRED AND PROCEDURE

a) Facilities Required To Do The Experiment:

Sl.No Facilities Required Quantity

1 UNIX Server 1

b) Procedure for doing the experiment:

Step no. Details of the Step

Server Side: 1 Start the process

2 Create a UDP socket for sever.

3 Listen to the port & receive the message send by the client and transfer the same message to client.

4 Close the server socket.

5 Stop the process.

Client Side: 1 Start the process

2 Create a UDP socket for Client.

3 Send the message to the server.

4 Receive the message from the server & print the message.

5 Stop the process.

c) Program:

UDPSERVER #include<sys/socket.h> #include<arpa/inet.h> #include<unistd.h> #define MAXLINE 4096 #define SERV_PORT 9877 #define SA struct sockaddr void dg_echo(int sockfd, SA *pcliaddr, int clilen)

Page 24: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

24

{ int n, len; char mesg[MAXLINE]; for(;;) { len=clilen; n=recvfrom(sockfd,mesg,MAXLINE,0,pcliaddr,&len); sendto(sockfd,mesg,n,0,pcliaddr,len); } } int main(int argc,char **argv) { int sockfd; struct sockaddr_in servaddr,cliaddr; sockfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(SERV_PORT); bind(sockfd,(SA *)&servaddr,sizeof(servaddr)); dg_echo(sockfd,(SA *)&cliaddr,sizeof(cliaddr)); }

UDP CLIENT //udpclient.c #include<sys/socket.h> #include<arpa/inet.h> #include<stdio.h> #include<string.h> #define MAXLINE 4096 #define SERV_PORT 9877 #define SA struct sockaddr void dg_cli(FILE *fp,int sockfd,const SA *pservaddr,socklen_t servlen) { int n; char sendline[MAXLINE],recvline[MAXLINE+1]; printf("\nEnter a Message : "); while(fgets(sendline,MAXLINE,fp)!=NULL) { sendto(sockfd,sendline,strlen(sendline),0,pservaddr,servlen); n=recvfrom(sockfd,recvline,MAXLINE,0,NULL,NULL); recvline[n]=0; printf("Echo From the Server : "); fputs(recvline,stdout); printf("\nEnter a Message (^D to Stop): "); }printf("\n"); } int main(int argc, char **argv) { int sockfd; struct sockaddr_in servaddr;

Page 25: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

25

if(argc!=2) { printf("\nInsufficient No of Parameters\n");

exit(0); } bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_port=htons(SERV_PORT); inet_pton(AF_INET,argv[1],&servaddr.sin_addr); sockfd=socket(AF_INET,SOCK_DGRAM,0); dg_cli(stdin,sockfd,(SA*)&servaddr,sizeof(servaddr)); exit(0);}

d) Output:

cc udpserver.c cc udpclient.c ./a.out ./a.out

Enter a message Enter a message WELCOME I LOVE INDIA

Echo from the cliente Echo from the server I LOVE INDIA WELCOME

e) Result:

Thus the above program was executed successfully. STEP 3: VIVA QUESTIONS AND ANSWERS:

1. Write down any 8 generic socket options

SO_BROADCAST, SO_DEBUG, SO_ERROR, SO_DONTROUTE,

SO_KEEPALIVE, SO_LINGER, SO_RCVBUF, SO_SNDBUF 2. Define UDP sockets.

UDP is a connection less unreliable, datagram protocol. In UDP the client does not

establish connection with a server. Instead the client just sends a datagram to the

server.

Page 26: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

26

Exercise Number: 4

Title of the Exercise : SIMULATION OF SLIDING WINDOW

PROTOCOL

Date of the Exercise : STEP 1: OBJECTIVE (AIM) OF THE EXPERIMENT

To write a java program for Simple implementation of Sliding window protocol (Go-Back-N) STEP 2: FACILITIES REQUIRED AND PROCEDURE

a) Facilities Required To Do The Experiment:

Sl.No Facilities Required Quantity

1 UNIX Server 1

b) Procedure for doing the experiment:

Step no. Details of the Step

1 Start the process

2 Create a file named sender and receiver

3 Compile and run the server program

4 Read the number of frames and read messages according to the frame number

5 The messages are sent to the receiver.

6 The receiver receives the message and sent acknowledgement to the sender.

7 Now he sender receives the ACK message from receiver and send message till the size of the window

8 Stop the process

c) Program: Sender:

import java.io.*; import java.net.*; class sender { public static void main(String a[]) throws IOException

{ ServerSocket ss=new ServerSocket(10); Socket s=ss.accept(); DataInputStream dis=new DataInputStream(System.in); DataInputStream inp=new DataInputStream(s.getInputStream()); String buf[]=new String[8]; PrintStream p; int sptr=0,sws=8,nf,ackno,i;

Page 27: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

27

String ch; do { p=new PrintStream(s.getOutputStream());

System.out.println("Enter the no. of frames\n"); nf=Integer.parseInt(dis.readLine()); p.println(nf); if(nf<=sws-1) { System.out.println("Enter"+nf+"Messages\n");

for(i=1;i<=nf;i++) { buf[sptr]=dis.readLine();

p.println(buf[sptr]); sptr=++sptr%8;

} sws=sws-nf; System.out.println("Acknowledgement received\n");

ackno=Integer.parseInt(inp.readLine()); System.out.println("ACK"+ackno); sws=sws+nf;

} else { System.out.println("Exceeds window size");

break; }

System.out.println("Do you want to send more frames\n"); ch=dis.readLine(); p.println(ch); } while(ch.equals("y")); s.close();

} }

Receiver: import java.net.*; import java.io.*; class receiver { public static void main(String a[]) throws IOException

{ Socket s=new Socket(InetAddress.getLocalHost(),10); DataInputStream dis=new DataInputStream(s.getInputStream());

PrintStream p=new PrintStream(s.getOutputStream()); int i=0,rptr=-1,nf,rws=8; String buf[]=new String[8]; String ch; do { nf=Integer.parseInt(dis.readLine());

if(nf<=rws-1) { for(i=1;i<=nf;i++)

{ rptr=++rptr%8; buf[rptr]=dis.readLine();

System.out.println("frame"+rptr+""+buf[rptr]); } rws=rws-nf;

Page 28: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

28

d) Output:

System.out.println("Acknowledge sent"); p.println(rptr+1); rws=rws+nf; }else break;

ch=dis.readLine();} while(ch.equals("y")); }}

cc sender.java cc receiver.java ./a.out ./a.out Enter the number of frames Enter message frame a

Acknowledge sent Acknowledge received

Enter message frame b

Acknowledge sent Acknowledge received Enter message Exceeds window size

e) Result:

Thus the above program was successfully executed.

STEP 3: VIVA QUESTIONS AND ANSWERS: 1. What is the use of ACK message?

ACK is use to identify the safe receiving of frame

2. What is frame?

In data link layer datas in the form of frame

3. What is flow control?

When the rate of receiving is slower than the rate of sender is said to be flow control

4. What is error control? When the frame is said to be error we want to clear that error it is said to be error control

5. Sliding window protocol is used for which purpose?

Sliding window protocol is used to avoid flow control mechanism.

Page 29: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

29

Exercise Number: 5

Title of the Exercise : SIMULATION OF ROUTING PROTOCOL

Date of the Exercise :

STEP 1: OBJECTIVE (AIM) OF THE EXPERIMENT

To write a java program for Simulation of Routing Protocol

STEP 2 : FACILITIES REQUIRED AND PROCEDURE a) Facilities Required To Do The Experiment:

Sl.No Facilities Required Quantity

1 UNIX Server 1

b) Procedure for doing the experiment:

Step no. Details of the Step

1 Start the process

2 Create a file named sender and receiver

3 Compile and run the server program

4 Compile and run the client program

5 Find the route from the server to the client using OSPF.

6 Stop the process

c) Program:

#include<stdio.h> main() { int d[10][10],inter[10][10]; int i,j,k,m; printf("ENTER THE DISTANCE BETWEEN THE HOSTS\n"); for(i=1;i<=4;i++) { for(j=1;j<=4;j++) { inter[i][j]=0; } } for(i=1;i<=4;i++) { for(j=1;j<=4;j++) {

Page 30: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

30

printf("ENTER THE DISTANCE BETWEEN %d AND %d:",i,j); scanf("%d",&d[i][j]); } } printf("THE DISTANCE VECTOR:\n"); printf("\n\tH1\tH2\tH3\tH4 \n"); for(i=1;i<=4;i++) { printf("\nHOST %d:",i); for(j=1;j<=4;j++)

{ printf("%d\t",d[i][j]); } }

for(i=1;i<=4;i++) { for(j=1;j<=4;j++) { m=0; for(k=1;k<=4;k++) { if(d[i][j]>(d[i][k]+d[k][j])) {

inter[j][m]=k; } m=m+1;

} } }

d[i][j]=d[i][k]+d[k][j];

printf("MINIMUM DISTANCE BETWEEN HOST 1 AND OTHER HOSTS\n"); i=1; printf("\nH1\tH2\tH3\tH4\n"); for(j=1;j<=4;j++) { printf("%d\t",d[i][j]); } printf("INTERMEDIATE MODE\n"); for(j=2;j<=4;j++) { printf("TO REACH HOSTS %d:",j); for(k=1;k<4;k++) { if(inter[j][k]!=0) { printf("%d",inter[j][k]); } } } }

Page 31: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

31

d) Output: Enter the distance between the HOSTS Enter the Distance between the hosts 1 and 1:7 Enter the Distance between the hosts 1 and 2:6 Enter the Distance between the hosts 1 and 3:5 Enter the Distance between the hosts 1 and 4:4

Enter the Distance between the hosts 2 and 1:1 Enter the Distance between the hosts 2 and 2:2 Enter the Distance between the hosts 2 and 3:3 Enter the Distance between the hosts 2 and 4:4

Enter the Distance between the hosts 3 and 1:2 Enter the Distance between the hosts 3 and 2:3 Enter the Distance between the hosts 3 and 3:4 Enter the Distance between the hosts 3 and 4:5

Enter the Distance between the hosts 4 and 1:1 Enter the Distance between the hosts 4 and 2:2 Enter the Distance between the hosts 4 and 3:4 Enter the Distance between the hosts 4 and 4:3

The distance vector

H1 H2 H3 H4 Host 1 7 6 5 4 Host 2 1 2 3 4 Host 3 2 3 4 5 Host 4 1 2 4 3 Minimum Distance between Host 1 and other Hosts

H1 H2 H3 H4 7 6 5 4

Intermediate mode Reach Host 2 : Reach Host 3 : Reach Host 4:

e) Result:

Thus the above program was successfully executed. STEP 3: VIVA QUESTIONS AND ANSWERS: 1.What is routing protocol? It is used for find the route from source to destination

2.What is OSPF? Open Shortest Path Finding algorithm

3.Why OSPF is the best one for routing? It is a routing from source to destination and it is also gives the shortest path

Page 32: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

32

Exercise Number: 6 Title of the Exercise : PROGRAM TO PERFORM RPC

Date of the Exercise :

STEP 1: OBJECTIVE (AIM) OF THE EXPERIMENT

To write a program to perform the remote procedure call STEP 2: FACILITIES REQUIRED AND PROCEDURE

a) Facilities Required To Do The Experiment:

Sl.No Facilities Required Quantity

1 UNIX Server 1

b) Procedure for doing the experiment:

Step no. Details of the Step

CLIENT 1 Start the program, declare the variables 2 Create a socket using the socket structure socket(AF_INET,

SOCK_STREAM,0) 3 Set the socket family, IP address and the port using the server address 4 Set the socket address of 8 bytes to zero using the memset() function 5 Establish the connection to the server

SERVER 1 Start the program, declare the variables 2 Create a socket using the socket structure socket(AF_INET,

SOCK_STREAM,0) 3 Set the socket family, IP address and the port using the server address 4 Set the socket address of 8 bytes to zero using the memset() function 5 Bind and listen the socket structure 6 Accept the client connection using the socket descriptor and the

server address 7 Get the command and write to the client 8 Terminate the connection

c) Program:

Server:

#include<netinet/in.h>

Page 33: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

33

#include<string.h> #include<sys/socket.h> #include<stdio.h> int main() { char a[10],b[10], buffer[10];

int i,sd,cd,size; struct sockaddr_in serv; struct sockaddr cli; sd=socket(AF_INET,SOCK_STREAM,0); serv.sin_addr.s_addr=INADDR_ANY; serv.sin_port=htons(9059); serv.sin_family=AF_INET; bzero(&(serv.sin_zero),8); bind(sd,(struct sockaddr *)&serv,sizeof(serv)); listen(sd,5); size=sizeof(struct sockaddr_in); cd=accept(sd,&cli,&size); printf("Enter the comand : "); fgets(buffer,10,stdin); write(cd,buffer,strlen(buffer)+1); close(sd); close(cd);

} Client:

#include<netinet/in.h> #include<string.h> #include<sys/socket.h> #include<stdio.h> int main() { int i,sd,cd,j,size;

char buffer[10]; struct sockaddr_in serv; struct sockaddr cli; sd=socket(AF_INET,SOCK_STREAM,0); serv.sin_addr.s_addr=INADDR_ANY; serv.sin_port=htons(9059); serv.sin_family=AF_INET; bzero(&(serv.sin_zero),8); if((connect(sd,(struct sockaddr*)&serv,sizeof(struct sockaddr)))<0) { printf("No connection to server.... Try after some time..");

return 0; } else { printf("Got command from Server ...");

read(sd,buffer,10); puts(buffer); system(buffer);

}

Page 34: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

34

close(sd); }

d) Output:

CLIENT cc rpcclient.c ./a.out Got command from Server ...date Thu Nov 11 02:09:32 IST 2010

e) RESULT:

SERVER cc rpcserver.c ./a.out Enter the command : date

Thus the above program was successfully executed. STEP 3: VIVA QUESTIONS AND ANSWERS: 1.Define RPC?

Remote Procedure Call (RPC) provides a different paradigm for accessing network services. Instead of accessing remote services by sending and receiving messages, a client invokes services by making a local procedure call. The local procedure hides the details of the network communication.

2. What is meant by remote procedure call?

If one program accessing a procedure of another program then it is called

remote procedure call.

Page 35: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

35

Exercise Number: 7 Title of the Exercise : APPLICATION OF DNS

Date of the Exercise :

STEP 1: OBJECTIVE (AIM) OF THE EXPERIMENT To write a program to perform domain name system

STEP 2: FACILITIES REQUIRED AND PROCEDURE

a) Facilities Required To Do The Experiment:

Sl.No Facilities Required Quantity

1 UNIX Server 1

b) Procedure for doing the experiment: Step no. Details of the Step

CLIENT 1 Start the program, declare the variables 2 Create a socket using the socket structure

socket(AF_INET, SOCK_STREAM,0) 3 Create a socket using the socket structure

socket(AF_INET, SOCK_STREAM,0) 4 Set the socket address of 8 bytes to zero using the

memset() function 5 Establish the connection to the server 6 Read the web address for the IP address and send it

to the server as a request message and receive the message from the server

7 Display the IP address for the corresponding web address

SERVER 1 Start the program, declare the variables 2 Create a socket using the socket structure

socket(AF_INET, SOCK_STREAM,0) 3 Set the socket family, IP address and the port using

the server address 4 Set the socket address of 8 bytes to zero using the

memset() function 5 Bind and listen the socket structure, Accept the

client connection using the socket descriptor and the server address

6 Get the domain name from the client, Open the document in read mode and make a string comparison to retrieve a domain name

Page 36: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

36

7 Send the corresponding IP address for the client requested web address, Compile and execute the program. Terminate the socket connection

c) Program:

Server:

#include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<stdio.h> #include<stdlib.h> int main() { struct sockaddr_in local; int s,s1,rc,i=0; FILE *fin,*fout; char buf[100],chec[100]; local.sin_family=AF_INET; local.sin_port=htons(9500); local.sin_addr.s_addr=inet_addr("127.0.0.1"); s=socket(AF_INET,SOCK_STREAM,0); if(s<0) { printf("SOCKET CALL FAILED\n"); exit(1); } rc=bind(s,(struct sockaddr*)&local,sizeof(local)); if(rc<0) { printf("BIND CALL FAILED\n"); exit(1); } rc=listen(s,5); if(rc) { printf("LISTEN CALL FAILED\n"); exit(1); } s1=accept(s,NULL,NULL); if(s1<0) { printf("ACCEPT CALL FAILED\n"); exit(1); } rc=recv(s1,buf,100,0); if(rc<=0) { printf("RECEIVE CALL FAILED\n"); exit(1);

Page 37: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

37

} fin=fopen("dns.doc","r"); while(!feof(fin)) { fscanf(fin,"%s",chec); if(!(strcmp(buf,chec))) { break; } } if(!feof(fin)) { fscanf(fin,"%s",chec); rc=send(s1,chec,100,0); fclose(fin); } else { fclose(fin); fin=fopen("dns.doc","r"); while(!feof(fin)) { fscanf(fin,"%s",chec); if(!(strcmp(buf,chec))) { break; } } if(!feof(fin)) { fscanf(fin,"%s",chec); rc=send(s1,chec,100,0); fclose(fin); } else { fclose(fin); fin=fopen("dns.doc","r"); while(!feof(fin)) { fscanf(fin,"%s",chec); if(!(strcmp(buf,chec))) { break; } } if(feof(fin)) { fscanf(fin,"%s",chec); rc=send(s1,chec,100,0); fclose(fin);

Page 38: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

38

} else { fclose(fin); rc=send(s1,"failure",100,0); } } } if(rc<=0) printf("SEND CALL FAILED\n"); exit(0); } Client: #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<stdio.h> #include<stdlib.h> int main() { struct sockaddr_in peer; int s,rc,i=0; char buf[100],sc[100]; peer.sin_family=AF_INET; peer.sin_port=htons(9500); peer.sin_addr.s_addr=inet_addr("127.0.0.1"); s=socket(AF_INET,SOCK_STREAM,0); if(s<0) { printf("SOCKET CALL FAILED\n"); exit(1); } rc=connect(s,(struct sockaddr*)&peer,sizeof(peer)); if(rc) { printf("CONNECT CALL FAILED\n"); exit(1); } printf("ENTER THE WEB ADDRESS\n"); scanf("%s",sc); rc=send(s,sc,100,0); if(rc<=0) { printf("SEND CALL FAILED\n"); exit(1); } rc=recv(s,buf,100,0); if(rc<=0) { printf("RECEIVE CALL FAILED\n");

Page 39: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

39

} if(!strcmp(buf,"failure")) { printf("INVALID WEB\n"); } printf("IP ADDRESS IS: %s\n",buf); exit(0); }

d) Output:

INPUT FILE

Cat>dns.doc www.yahoomail.com 172.15.1.04 www.google.com 172.15.1.05 www.rediffmail.com 172.15.1.06 www.gmail.com 172.15.1.08

cc dnsserver.c ./a.out

cc dnsclient.c ./a.out

SERVER CLIENT

ENTER THE WEB ADDRESS www.yahoomail.com IP ADDRESS IS: 172.15.1.04

e) Result: Thus the above program was successfully executed.

STEP 3: VIVA QUESTIONS AND ANSWERS: 1. What is Domain Name System?

The Domain Name System, or DNS, is used to map between hostnames and IP addresses. A host name can either be a simple name, or a FQDN (Fully Qualified Domain Name).

2. What is the use of ‘socket’ function?

Socket function is used to create a socket that is bound to a specific transport service provide. It returns a file descriptor that can be used in later function calls that operate on sockets.

Page 40: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

40

Exercise Number: 8

Title of the Exercise : HTTP SERVER FOR WEB PAGE DOWNLOAD

Date of the Exercise :

STEP 1: OBJECTIVE (AIM) OF THE EXPERIMENT To write a client program to download a file from a HTTP server

STEP 2: FACILITIES REQUIRED AND PROCEDURE

a) Facilities Required To Do The Experiment:

Sl.No Facilities Required Quantity

1 UNIX Server 1

b) Procedure for doing the experiment:

Step no. Details of the Step

Server Side: 1 Start the program, declare the variables

2 Create a socket using the socket structure socket(AF_INET, SOCK_STREAM,0)

3 Set the socket family, IP address and the port using the server address

4 Set the socket address of 8 bytes to zero using the memset() function

5 Bind and listen the socket structure

6 Accept the client connection using the socket descriptor and the server address

7 Open a file and make a string comparison of the file

8 Send the corresponding result to the client

9 Terminate the connection and compile and execute

Client Side: 1 Start the program, declare the variables

2 Create a socket using the socket structure socket(AF_INET, SOCK_STREAM,0)

3 Set the socket family, IP address and the port using the server address

4 Set the socket address of 8 bytes to zero using the memset() function

5 Establish the connection to the server

6 Read web address to be downloaded and send it to the server as a request

7 Receive the message from the server for a search result

8 Display the file which is downloaded

9 Terminate the connection and compile and execute

Page 41: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

41

c) Program: SERVER: #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<stdlib.h> int main() { struct sockaddr_in local; int s,s1,rc,l=0; FILE *fin,*fout; char buf[2000],chec[2000]; local.sin_family=AF_INET; local.sin_port=htons(14000); local.sin_addr.s_addr=inet_addr("127.0.0.1"); s=socket(AF_INET,SOCK_STREAM,0); if(s<0) { printf("SOCKET CALL FAILURE\n");

exit(1);} rc=bind(s,(struct sockaddr*)&local,sizeof(local)); if(rc<0) { printf("BIND CALL FAILURE\n");

exit(1);} rc=listen(s,5); if(rc) { printf("LISTEN CALL FAILED\n");

exit(1);} s1=accept(s,NULL,NULL); if(s1<0) { printf("ACCEPT CALL FAILED\n");

exit(1);} rc=recv(s1,buf,2000,0); if(rc<0) { printf("RECEIVE CALL FAILED\n");

exit(1);} fin=fopen("http.doc","r"); while(!feof(fin)) { fscanf(fin,"%s",chec);

if(!(strcmp(buf,chec))) { break; }

} if(!feof(fin)) { fscanf(fin,"%s",chec);

rc=send(s1,chec,2000,0); fclose(fin);

} else { fclose(fin);

rc=send(s1,"failure",2000,0); }

Page 42: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

42

fout=fopen(chec,"r"); if(fout<0) { strcpy(buf,"failure"); } else { while(!feof(fout)) {

fscanf(fout,"%s",chec); printf("SUCCESS\n"); send(s1,chec,2000,0); fclose(fout); } } rc=send(s1,"over",2000,0); if(rc<=0) printf("SEND CALL FAILED\n"); exit(0);}

CLIENT:

#include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<stdio.h> #include<stdlib.h> int main() { struct sockaddr_in peer; int s,rc,i=0; FILE *fin; char buf[2000],se[2000]; peer.sin_family=AF_INET; peer.sin_port=htons(14000); peer.sin_addr.s_addr=inet_addr("127.0.0.1");

s=socket(AF_INET,SOCK_STREAM,0); if(s<0) { printf("SOCKET CALL FAILED\n"); exit(1);} rc=connect(s,(struct sockaddr*)&peer,sizeof(peer)); if(rc) { printf("CONNECTION CALL FAILED\n"); exit(1);} printf("ENTER THE WEB ADDRESS\n"); scanf("%s",se); rc=send(s,se,2000,0); if(rc<=0) { printf("SEND CALL FAILED\n"); exit(1);} { rc=recv(s,buf,2000,0); if(rc<=0) { printf("RECEIVE CALL FAILED\n"); } if(!strcmp(buf,"failure")) { printf("INVALID WEB\n"); }

Page 43: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

43

else { fin=fopen("http.html","a"); rewind(fin); fputs(buf,fin); fclose(fin); puts("FILE IS SUCCESSFULLY OBTAINED\n"); } } exit(0); }

d) Output:

INPUT FILE cat>http.doc www.yahoomail.com www.google.com www.sify.com www.rediffmail.com www.gmail.com

SERVER cc httpserver.c ./a.out

CLIENT cc httpclient.c ./a.out ENTER THE WEB ADDRESS www.gmail.com FILE IS SUCCESSFULLY OBTAINED

e) Result: Thus the above program was successfully executed.

STEP 3: VIVA QUESTIONS AND ANSWERS:

1. What is HTTP? Hiper Text Transfer protocol

2. How we can download a web page from the internet? Using relative URL address we can download the web page

3. What is the use of socket descriptors?

It is used for create connection in internet

Page 44: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

44

Exercise Number: 9 Title of the Exercise : APPLICATION OF E-MAIL

Date of the Exercise :

STEP 1: OBJECTIVE (AIM) OF THE EXPERIMENT To write a program to send a e-mail to other client.

STEP 2: FACILITIES REQUIRED AND PROCEDURE

a) Facilities Required To Do The Experiment:

Sl.No Facilities Required Quantity

1 UNIX Server 1

b) Procedure for doing the experiment: Step no. Details of the Step

CLIENT 1 Start the program, declare variables

2 create a socket with address family

3 Initialize the socket and set its attributes set the required port no.

4 Type AF_INET socket with default protocol.

5 Connect to server using connect function to initiate the request.

6 Receive the acknowledgement from the server.

7 Stop.

SERVER: 1 Start the program, declare variables

2 Create a socket with address family AF_INET type SOCK_STREAM and default protocol.

3 Initialize a socket and set its attributes. 4 Bind the server to the socket using bind function.

5 Wait for the client request, on request establish a connection using accept function.

6 Fork a child process, perform step 7-10 for each process.

7 Read the message from the client thro the connection. 8 Display the client I message. 9 Send an acknowledgement message to the client .

10 Exit the child process& Close the connection and go to step 5.

Page 45: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

45

c) Program: SERVER:- #include<iostream.h> #include<stdio.h> #include<sys/socket.h> include<netinet/in.h> #include<stdlib.h> #include<string.h> #include<netdb.h> #include<arpa/inet.h> #include<sys/types.h> #include<time.h> #defineMAX 100 int cwork(int); intmain() { int sockmain,sockclient,child; struct sockaddr_in serv; char str[100]; int port=4056; if((sockmain=socket(AF_INET,SOCK_STREAM,0))<0) { cout<<"server cannot open main socket!"; exit(0); } bzero(&serv,sizeof(serv)); serv.sin_family=AF_INET; serv.sin_addr.s_addr=htonl(INADDR_ANY); serv.sin_port=htons(port); if((bind(sockmain,struct sockaddr*)&serv,sizeof(serv)))<0) { cout<<"server bind failed!", exit(0); listen(sockmain,15); for(;;) { if((sockclient=accept(sockmain,struct sockaddr*)NULL,0))<0) { cout<<"bad client socket"; exit(1); } if((child=fork())<0) { cout<<"failed to create child"; exit(1); } else if (child==0) { close(sockmain); cwork(sockclient);

Page 46: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

46

close(sockclient); exit(0); } } #define LEN 81 int cwork(int sockclient) { char buf[LEN]; int msglen; bzero(buf,LEN); if(msglen=recv(sockclient,buf,LEN,0))<0) { cout<<"Bad recv by chile"; exit(1); } cout<<"\nsocket used:"<<sockclinet; buf[msglen]='\0':; cout<<"\nmessage:"<<buf; strcpy(buf,"message received..."); send(sockclient,buf,strlen(buf),0); } CLIENT:- #include<iostream.h> #include<stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<stdlib.h> #include<unistd.h> #include<string.h> #include<netdb.h> #include<arpa/inet.h> #include<sys/types.h> #include<time.h> #include<errno.h> #define MAX 100 main(int argc,char**argv) { int sockfd,n; char str[100]; struct sockaddr_inserv; if(argc!=3) { printf("\nError! usage:./a.out<IP ADDRESS><PORT>\n"); exit(0);} if(sockfd=socket(AF_INET<SOCK_STREAM,0))<0) { printf("\nERROR! socket not created...\n"); exit(0);} bzero(&serv,sizeof(serv)); serv.sin_family=AF_INET; serv.sin_port=htons(atoi(argv[2])); if(inet_pton(AF_INET,argv[1],&serv.sin_addr<=0){

Page 47: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

47

printf("\nError in conversion of IP address from string to number\n"); exit(0);} if(connect(sockfd,(struct sockaddr*)&serv,sizeof(serv))<0{ printf("\nError conexion not established...\n"); exit(0);} printf("\nconnected\n"); cout<<"enter message to server:'; gets(str); write(sockfd,str,sizeof(str)); n=read(sockfd,str,100); str[n]='()'; cout<<"\n from server:"<<str; cout<<"n"; }

d) OUTPUT:

Server Side server.cpp ./a.out socket used:4 message:Helloserver

Client Side: client.cpp ./a.out 127.0.0.1 5852 connected enter message to server:Helloserver from server:message received...

e) Result: Thus the above program was successfully executed.

STEP 3: VIVA QUESTIONS AND ANSWERS:

1. What are sockets • A socket is an endpoint for communication • Socket is an abstraction for an endpoint of communication that can be

manipulated with a file descriptor. • Scope of a socket is the communication domain in which it exists.

2. How do you create a raw socket The socket function creates a raw socket when the second argument is SOCK_RAW. The third argument (the protocol)is normally nonzero.sockfd= socket(AF_INET, SOCK_RAW, protocol);where protocol is one of the constants, IPPROTO_xxx defined by including the <netinet/in.h> header such as IPPROTO_ICMP. Only the superuser can create a raw socket.

Page 48: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

48

Exercise Number: 10

Title of the Exercise : MULTI-USER CHAT APPLICATION

Date of the Experiment:

STEP 1: OBJECTIVE (AIM) OF THE EXPERIMENT To write a java program to create a simple mobile game.

STEP 2: FACILITIES REQUIRED AND PROCEDURE

1) Facilities Required To Do The Experiment:

Sl.No Facilities Required Quantity

1 UNIX Server 1

2) Procedure for doing the experiment:

Step no. Details of the Step

CLIENT 1 Start the program

2 Create a socket using socket(AF_INET, SOCK_STREAM,0)

3

Set the 8 bytes address to be zero using memset() function set the socket address family and port using servaddr

4 Establish connection with the server, read the filename to be retrieved from server

5 Receive the message from the server

6 Terminate the connection

SERVER 1 Start the program, declare variables 2 Create a socket using the socket structure

socket(AF_INET, SOCK_STREAM,0) 3 Set the socket family, IP address and the port using

the server address 4 Set the socket address of 8 bytes to zero using the

memset() function 5 Bind and listen the socket structure 6 Accept the client connection using the socket

descriptor and the server address 7 Enter the message and port number to which we

want to send the message 8 Terminate the connection

Page 49: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

49

c) Program: SERVER:

#include<netinet/in.h> #include<string.h> #include<sys/socket.h> #include<stdio.h> int main() { char a[10],b[10];

int i,sd,cd,size; char buffer[10]; struct sockaddr_in serv; struct sockaddr cli; sd=socket(AF_INET,SOCK_STREAM,0); serv.sin_addr.s_addr=INADDR_ANY; printf("Enter the message : "); scanf("%s",a); printf("enter the port no. to be passed : "); scanf("%d",&i); serv.sin_port=htons(i); serv.sin_family=AF_INET; bzero(&(serv.sin_zero),8); bind(sd,(struct sockaddr *)&serv,sizeof(serv)); listen(sd,5); size=sizeof(struct sockaddr_in); cd=accept(sd,&cli,&size); send(cd,&a,sizeof(a),0); close(sd); close(cd);}

CLIENT 1: #include<netinet/in.h> #include<string.h> #include<sys/socket.h> #include<stdio.h> int main() { int i,sd,cd,j,size; char a[10];

struct sockaddr_in serv; struct sockaddr cli; sd=socket(AF_INET,SOCK_STREAM,0); serv.sin_addr.s_addr=INADDR_ANY; serv.sin_port=htons(9656); serv.sin_family=AF_INET; bzero(&(serv.sin_zero),8); if((connect(sd,(struct sockaddr*)&serv,sizeof(struct sockaddr)))<0) { printf("No connection to server.... Try after some time.."); } else {

CLIENT 2:

recv(sd,&a,sizeof(a),0); puts(a); }}

#include<netinet/in.h> #include<string.h> #include<sys/socket.h> #include<stdio.h> int main() {

int i,sd,cd,j,size; char a[10]; struct sockaddr_in serv; struct sockaddr cli; sd=socket(AF_INET,SOCK_STREAM,0); serv.sin_addr.s_addr=INADDR_ANY; serv.sin_port=htons(9657); serv.sin_family=AF_INET;

Page 50: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

50

bzero(&(serv.sin_zero),8); if((connect(sd,(struct sockaddr*)&serv,sizeof(struct sockaddr)))<0) {

printf("No connection to server.... Try after some time.."); } else {

recv(sd,&a,sizeof(a),0); puts(a); }}

d) Output:

SERVER: cc multiserver.c ./a.out Enter the message : hai enter the port no. to be passed : 9656

CLIENT 1: cc multiclient1.c ./a.out hai

SERVER: cc multiserver.c ./a.out Enter the message : welcome enter the port no. to be passed : 9657

CLIENT 2: cc multiclient1.c ./a.out welcome

e) Result:

Thus the above program was successfully executed. STEP 3: VIVA QUESTIONS AND ANSWERS:

1.Define multiplexed TCP?

A technique for improving Web-server performance, by consolidating incomingTCP/IP requests to reduce the number of times server connections have to be made. 2.What is meant by multiplexed TCP server?

If one server process responses to more than one client process, then it is

called as multiplexed server.

Page 51: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

51

Exercise Number: 11 Title of the Exercise : CYCLIC REDUNDANCY CHECK

Date of the Experiment:

STEP 1: OBJECTIVE (AIM) OF THE EXPERIMENT To write a program to compute the Cyclic Redundancy check using UNIX

STEP 2: FACILITIES REQUIRED AND PROCEDURE

a) Facilities Required To Do The Experiment:

Sl.No Facilities Required Quantity

1 UNIX Server 1

b) Procedure for doing the experiment:

Step no. Details of the Step

1 Start the program.

2 Declare the variables and array of variables.

3 Get the number of bits to be Transmitted and appended.

4 Read the transmitted bits and store the bits to the arrays using for loop

5 Get the divisor bits for cyclic redundancy calculation and display it

6 Calculate the bits for cyclic redundancy check, make a result array of cyclic redundancy check and display it.

7 Compile and execute the program.

c) Program: #include<stdio.h> main() { int a[50],d[50],c[50],res[50],crc[3],i,j,k,l,m,n,h,na; printf("SENDER PROCESS\n"); printf("ENTER THE NUMBER OF BITS TO BE TRANSMITTED\n"); scanf("%d",&n); printf("NUMBER OF BITS TO BE APPENDED\n"); scanf("%d",&na); printf("Enter the bits to be Transmitted\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=n;i<n+na;i++) a[i]=0; for(i=0;i<n+na;i++) printf("%d",a[i]); printf("Enter the Divisor\n");

Page 52: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

52

for(j=0;j<na+1;j++) scanf("%d",&d[j]); printf("The Divisor is\n"); for(j=0;j<na+1;j++) printf("%d",d[j]); for(i=0;i<4;i++) { if((a[i]==1)&&(d[i]==1)) { c[i]=0; } else if(((a[i]==0)&&(d[i]==1))||((a[i]==1)&&(d[i]==0))) { c[i]=1; } else { c[i]=0;

} } printf("CALCULATION OF CRC\n");

k=0; for(i=4;i<=n+na;i++) { k=k+1;

c[i]=a[i]; printf("RESULT ARRAY\n"); for(h=0;h<n+na;h++) { res[h]=c[h]; printf("%d\n",res[h]); } printf("The Result value\n"); for(l=k;l<=i;l++) { printf("%d\n",c[l]);

} printf("\n");

m=0; for(j=k;j<=i;j++) { if(res[k]==l)

{ if((c[j]==i)&&(d[m]==l)) c[j]=0; else if(((c[j]==0)&&(d[m]==l))||((c[j]==l)&&(d[m]==0))) c[j]=1; else c[j]=0; } else { if((c[j]==l)&&(d[m]==0))

c[j]=1; else c[j]=0;

} m=m+1; } } printf("The Result Array\n"); for(i=0;i<n+na;i++) { printf("%d",res[i]); } printf("\n");

Page 53: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

53

}

d) Output:

Enter the bit 100100100 Resulted bit is 100100101 Error in the 9th bit

e) Result:

Thus the above program was successfully executed. STEP 3: VIVA QUESTIONS AND ANSWERS:

1.Write short notes on CRC.

The third and most powerful of the redundancy checking techniques is the cyclic redundancy checks (CRC) CRC is based on binary division. Here a sequence of redundant bits, called the CRC remainder is appended to the end of data unit. 2.Write short notes on CRC generator.

A CRC generator uses a modulo-2 division. a) In the first step, the 4 bit divisor is subtracted from the first 4 bit of the

dividend. b) Each bit of the divisor is subtracted from the corresponding bit of the

dividend without disturbing the next higher bit. 3. Write short notes on CRC checker.

A CRC checker functions exactly like a generator. After receiving the data appended with the CRC it does the same modulo-2 division. If the remainder is all 0’s the CRC is dropped and the data accepted. Otherwise, the received stream of bits is discarded and the dates are resent. 4.Give the essential properties for polynomial.

A polynomial should be selected to have at least the following properties. a) It should not be b) It should be divisible by(x+1).

5.Define checksum. The error detection method used by the higher layer protocol is called

checksum. Checksum is based on the concept of redundancy. 6.What are the steps followed in checksum generator?

The sender follows these steps a) The units are divided into k sections each of n bits. b) All sections are added together using 2’s complement to get the sum. c) The sum is complemented and become the checksum. d) The checksum is sent with the data.

7.List out the steps followed is checksum checker side. The receiver must follow these steps a) The unit is divided into k section each of n bits. b) All sections are added together using 1’s complement to get the sum. c) The sum is complemented.d) If the result is zero.

Page 54: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

54

Exercise Number: 12 Title of the Exercise : IMPLEMENTATION OF UDP CLIENT SERVER

COMMUNICATION USING BIND SYSTEM CALL

Date of the Experiment: STEP 1: OBJECTIVE (AIM) OF THE EXPERIMENT To write a program to compute the Cyclic Redundancy check using UNIX

STEP 2: FACILITIES REQUIRED AND PROCEDURE

a) Facilities Required To Do The Experiment:

Sl.No Facilities Required Quantity

1 UNIX Server 1

b) Procedure for doing the experiment:

Step no. Details of the Step

CLIENT 1 Start the program, declare variables

2 create a socket with address family

3 Inside a loop, type a message to be send to server.

4 Send the message using sendto() system call.

5 Receive the message from server using recvfrom() system call.

6 If the message received or send is ‘bye’ then stop.

7 Close the socket.

8 Stop

SERVER 1 Start the program, declare variables 2 Create a socket with address family AF_INET type

SOCK_STREAM and default protocol. 3 Initialize a socket and set its attributes. 4 Bind the server to the socket using bind function. 5 Inside a loop wait for the client to send some

message. 6 Receive a message using recvfrom() system call. 7 If received message is ‘bye’, send message ‘bye’

and stop the chat. If not, send some message using sendto() system call.

Page 55: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

55

c) Program: UDP SERVER PROGRAM: #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<string.h> int main(int argc, char **argv) { int sockfd; struct sockaddr_in mysock, newsock; if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0) { perror(“\nNo socket”); exit(0); } size=sizeof(struct sockaddr); socklen_t len=sizeof(newsock); bzero(&mysock, size); mysock.sin_family=AF_INET; mysock.sin_port=htons(4001); mysock.sin_addr.s_add=htonl(INADDR_ANY); if((bind(sockfd,(struct sockaddr *)&mysock, size))<0) { perror(“\n No bind”); exit(0); } printf(“\nUDP socket created……..listening port 4001”); while(strcmp(msg,”bye”)!=0) { if((val=recvfrom(sockfd, msg, 1024,0,(struct sockaddr *)$newsock, $len))<0) { perror(“\nNot created”); exit(0); } else printf(“\nServer received the message : %s”, msg); printf(“\nFrom client:”); printf(“%s”,msg); if(strcmp(msg,”bye”)==0) { printf(“\nServer:”); scanf(“%s”,msg); exit(0); } printf(“\nServer:”); scanf(“%s”,msg); if((sendto(sockfd, msg, val, 0, (struct sockaddr*)&newsock,len))<0) { perror(“\nNo message”); exit(0);

Page 56: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

56

} } close(sockfd); return 0; } Client:

#include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<arpa/inet.h> #include<netinet/in.h> #include<string.h> #include<unistd.h> #include<sys/times.h> #define SERV_IP 192.168.1.248 #define SERV_PORT 9059 void dg_cli(FILE *fp,int sockfd); int main(int argc,char *argv[]) { int sockfd,connfd; struct sockaddr_in servaddr; bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_port=htons(SERV_PORT); inet_pton(AF_INET,argv[1],&servaddr.sin_addr); sockfd=socket(AF_INET,SOCK_STREAM,0); printf("SOCKET DESCRIPTOR %d\n",sockfd); connfd=connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr)); printf("CONNECTING %d\n",connfd); dg_cli(stdin,sockfd); close(sockfd); exit(0); } void dg_cli(FILE *fp,int sockfd) { int n; char sendline[500],recvline[500],msg2[]={'b','y','e','\0'}; for(;;) { printf("CLIENT\n"); sendline[0]='\0'; fgets(sendline,500,stdin); send(so ckfd,sendline,strlen(sendline),0); if((sendline[0]=='b')&&(sendline[1]=='y')&&(sendline[2]=='e')) return; recvline[0]='\0'; n=recv(sockfd,recvline,500,0); recvline[n]=0; if((recvline[0]=='b')&&(recvline[1]=='y')&&(recvline[2]=='e'))

Page 57: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

57

return; recvline[n]=0; printf("SERVER\n"); fputs(recvline,stdout); printf("\n"); } }

d) OUTPUT:

Server Side cc udpser.c $./a.out UDP socket created……...listening port 4001 Server received the message: hello From client: hello Server: hey Server received the message: HowRU? From client: HowRU? Server: fine. Server received the message: bye From client: bye Server: bye

e) Result:

Thus the above program was successfully executed. STEP 3: VIVA QUESTIONS AND ANSWERS:

1. Give the syntax of sendto( ) systemcall in UDP

sendto( int sockfd, char *buffer, int nbytes, int flags, struct sockaddr *toaddr, int toaddr_len); sockfd => socket on which to send datagram buffer => address of the buffer to send nbytes => number of bytes to send flags => special send options toaddr => internet address and port to use toaddr_len => length in bytes of toaddr structure

2. Differentiate system call and a function • The system call is an entry point into the kernel Function is used to

describe what appears to the user to the functions, even though on some

Page 58: MC9247 NETWORK PROGRAMMING LAB … labmanual -new .pdf · Dr.NNCE MCA/IV SEM Network Programming -LM 2 ... the answer to the viva-voice questions should be neatly written in the workbook

Dr.NNCE MCA/IV SEM Network Programming -LM

58

LIST OF MODEL QUESTIONS

1. Write some UNIX command and get the appropriate result if need use

options.

2. Write a socket program to perform echo command.

3. Write a socket program to perform talk command.

4. Write a socket program to perform ping command.

5. Write a program for perform TCP socket.

6. Write a program for perform UDP socket.

7. Write a program for performing simulation of sliding window protocol.

8. Write a program to perform routing protocol in order to find shortest path

from the host to other hosts.

9. Write a program to perform RPC(Remote Procedure Call).

10. Write a program for use DNS(Domain Name System).

11. Write a client program to download a file from the HTTP server.

12. Write a program to send e-mail from one host to another host.

13. Write a program using socket for multi client chat server application.

14. Write a program to compute the Cyclic Redundancy check.

15. Write a program to implement UDP client server communication using

BIND system call.

INTERNAL EXAMINER EXTERNAL EXAMINER