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
58
Embed
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.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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
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.
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
-----------------
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
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
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
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:-
Dr.NNCE MCA/IV SEM Network Programming -LM
8
This command is display list all the files and sub directories. Syntax:-
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
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]:-
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
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()); }
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 .
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
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");
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.
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++) {
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++)
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
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
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>
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];
#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);
}
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.
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
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
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.
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.
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
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() {
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.
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");
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.
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.
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
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