You are on page 1of 55

MC9247– NETWORK PROGRAMMING LAB

Department of Computer Applications
Lab Manual MC 9247 Network Programming Lab (IV Semester) Prepared By N.M. Kavitha B.U.Anu Barathi

RAJALAKSHMI ENGINEERING COLLEGE

Rajalakshmi Nagar, Thandalam, Chennai - 602105
Page 1

MC9247– NETWORK PROGRAMMING LAB

TABLE OF CONTENTS
Exer. No 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. Unix Network commands Program to implement Message Queues Program to implement Pipes Program to implement Shared Memory Implementing byte ordering functions Implementation of address conversion routines Program to obtain the local and remote socket address Program to manipulate IP address and test its characteristics Program to obtain information about (A) Host (B) Network (C)Protocols (D) Domains Program to create TCP sockets for sending and receiving data Program to create UDP sockets for sending and receiving data Program to implement a chat server using TCP/IP protocol Program to implement FTP Simulation of Sliding Window protocol Simulation of Routing protocols Implementation of RPC Implementation of DNS
Page 2

Program

MC9247– NETWORK PROGRAMMING LAB

18. 19. 20. 21. I 22. 23. 24.

Implementation of HTTP Implementation of E-mail Implementation of Multi-user chat Program to create sockets for handling multiple connections Perform Socket Programming Program to trace the port of a particular host Ping Program

Page 3

MC9247– NETWORK PROGRAMMING LAB Exercise No : 1 Date

Unix Commands for Network

Aim To understand how to use and the need to use the Unix network commands. Description 1. arp This command is used when there are problems in connecting to devices. The arp command is used to see the IP to MAC address translation table. It is a dynamic cache which updates every 120 seconds. ARP stands for Address Resolution Protocol (ARP). Usage: [mcastaff@proxyserver ~]$ arp -a Address stugate6 plum.baylor.edu spud.baylor.edu michaela.baylor.edu GL09.baylor.edu 129.62.148.38 burro.baylor.edu GL14.baylor.edu GL15.baylor.edu HWtype ether ether ether ether HWaddress Flags Mask Iface C C C C C C C C C C * * * * * * * * * * eth0 eth0 eth0 eth0 eth0 eth0 eth0 eth0 eth0 eth0

00:90:AB:C2:A0:00 00:A0:C9:D7:5A:C5 00:A0:C9:D7:2C:06 00:20:AF:E8:EE:25 00:00:94:7B:09:3C

st-csi-a-dyn-01.baylor ether

ether 00:A0:C9:67:F4:8B ether 00:A0:C9:D7:5A:E4 ether 00:00:94:75:45:13 ether ether 00:A0:24:0C:42:B9 00:A0:24:0C:41:F5

Page 4

MC9247– NETWORK PROGRAMMING LAB GL16.baylor.edu lisa.baylor.edu ether ether 00:A0:C9:67:F5:63 00:A0:C9:D7:2C:07 C C * * eth0 eth0

2. ping The ping command is the most versatile network troubleshooting command in UNIX. It is used to verify whether the TCP/IP network services are operating correctly. The ping command determines the: o TCP/IP stack is configured properly o Network interface card is configured properly o Default gateway and subnet mask is configured properly o Domain name service is configured properly. Usage : [mcastaff@proxyserver ~]$ ping 127.0.0.1 PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.037 ms 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.024 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.023 ms 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.022 ms 64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.024 ms [mcastaff@proxyserver ~]$ ping 172.16.2.215 PING 172.16.2.215 (172.16.2.215) 56(84) bytes of data. 64 bytes from 172.16.2.215: icmp_seq=0 ttl=64 time=0.042 ms 64 bytes from 172.16.2.215: icmp_seq=1 ttl=64 time=0.029 ms 64 bytes from 172.16.2.215: icmp_seq=2 ttl=64 time=0.035 ms 64 bytes from 172.16.2.215: icmp_seq=3 ttl=64 time=0.024 ms 64 bytes from 172.16.2.215: icmp_seq=4 ttl=64 time=0.028 ms [mcastaff@proxyserver ~]$ ping pgserver

Page 5

16.16.215:squid 0 172.16.16.com (217.136.2.215:squid 0 172.btopenworld.215:squid 0 172.btopenworld.16.in-addr.16.16.141): icmp_ seq=0 ttl=110 time=209 ms 64 bytes from host217-40-136-141.141): icmp_ seq=1 ttl=110 time=221 ms 64 bytes from host217-40-136-141.btopenworld.3:1067 172.15.215:squid Page 6 172.com (217.136.com (217.btopenworld.15. 64 bytes from host217-40-136-141.40.141): icmp_ seq=3 ttl=110 time=223 ms 64 bytes from host217-40-136-141.2.16.1.141) 56(84) bytes of data.15.1.3:1064 172.46:1290 State TIME_WAIT ESTABLISHED ESTABLISHED TIME_WAIT ESTABLISHED TIME_WAIT TIME_WAIT .in-addr.215:squid 0 172.2.16.46:1291 172.40.46:1289 172.2.16.136.3:1065 172.136.15. netstat The network status command netstat displays status information about the network interfaces on the host machine and it can display routing table information.btopenworld.16.2.MC9247– NETWORK PROGRAMMING LAB PING pgserver.136.2.16.in-addr.in-addr.40. Usage : [mcastaff@proxyserver ~]$ netstat Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address tcp tcp tcp tcp tcp tcp tcp 0 0 0 0 0 0 0 0 172.40.136.3:1066 172.16.40.com (217.141): icmp_ seq=2 ttl=110 time=222 ms 64 bytes from host217-40-136-141.141): icmp_ seq=4 ttl=110 time=203 ms 3.com (217.40.215:squid 0 172.2.in-addr.215:squid 0 172.16.1.com (217.

nslookup <machine name> This command is used to Query name server to map names (e.edu Server: ccis03.50:38326 0 172.2.3:1068 ESTABLISHED 203.16.1.46:1281 172.200.46:1285 TIME_WAIT TIME_WAIT TIME_WAIT TIME_ WAIT 0 1042 172.16.conf file Usage : $ nslookup burro. burro.50:38417 0 220.2:http ESTABLISHED 172.46:1282 172.2.227.62.1.30.16.2:http ESTABLISHED 203.50:38489 0 172.MC9247– NETWORK PROGRAMMING LAB tcp tcp tcp tcp tcp tcp tcp tcp tcp tcp tcp tcp tcp 0 0 0 0 0 0 0 0 0 0 0 0 0 220.8 S nslookup 129.16.16.46:1293 172.200.4 Name: BURRO.62.50:38418 0 220.235.2:http ESTABLISHED 203.73.16.215:squid 0 172.200.1.50:38416 0 220.215:squid 0 220.227.46:1280 172.22:http ESTABLISHED 172.148.edu Address: 129.8 Server: ccis03.edu Address: 129.200.30.30.2.2.2.g.2.215:squid 203.227.15.27.30.215:squid 4.215:squid 0 172.baylor.2.16.baylor.30.edu <= Name of queried name server <= IP address of queried name server <= Name of machine <= IP address of machine <= Reverse lookup Address: 129. Under UNIX.17:http ESTABLISHED 172.4 Page 7 .baylor.199.227.148.215:squid 0 220.16.1.baylor.84.2:http ESTABLISHED 203.16.62.199.16.1.baylor.199.16.1.16.16.edu) to IP address and IP addresses to names (the reverse).16.30. the default name server for a machine can be found in the /etc/resolv.16.227.46:1292 TIME_WAIT TIME_WAIT 216.62.199.227.16.215:squid 0 172.50:38412 0 172.

ifconfig The ifconfig command is used to display the local interface configuration (winipcfg for Windows) and to modify the configuration.16.2. 30 hops max. Local interfaces can be Ethernet network cards. 6.215).16.148.026 ms BASIC SHELL COMMANDS GENERAL PURPOSE COMMANDS:  Command  Command  Command  Command  Command  Command 1.048 ms 0.16.8 5. etc.069 ms 0.215 traceroute to 172.MC9247– NETWORK PROGRAMMING LAB Name: burro..2.62. Usage : [mcastaff@proxyserver ~]$ traceroute 172.215 (172. TASK : Date : cal : echo : who am i : who : clear : To display the system date and time. 38 byte packets 1 172. traceroute Traceroute displays the routers that are passed through to reach the destination.2.215) 0.edu Address: 129.16.2. Page 8 . modems.16.215 (172.baylor.2.

: date. : date. 4. 6.MC9247– NETWORK PROGRAMMING LAB COMMAND SYNTAX EXPLANATION : date. : date +%h. 5. the screen. 3. : date. Page 9 EXPLANATION : This command displays the current month on EXPLANATION : This command displays the name of the . TASK COMMAND SYNTAX : To display the current system time. : date. : date +%m. : This command displays the current system date on the screen. : This command displays the current system date and time on the screen. TASK COMMAND SYNTAX EXPLANATION : To display the current system date (year). TASK COMMAND SYNTAX : To display the name of the current month. : date +%y. : date +%d. : This command displays the current year on the screen. : date +%H. TASK COMMAND SYNTAX EXPLANATION : To display the current system date. 2. : date. : date. current month on the screen. TASK COMMAND SYNTAX : To display the current month.

: echo. : cal. 9. : This command displays the calendar of the current month on the screen. Page 10 . : This command displays the current system time (in seconds) on the screen. : This command displays the current system time (in minutes) on the screen. TASK COMMAND SYNTAX EXPLANATION : To display the current system time. : date +%M. TASK COMMAND SYNTAX EXPLANATION : To display the current system time. 7. 11. : date. : date +%S. TASK COMMAND SYNTAX EXPLANATION : To display the calendar of the current month.MC9247– NETWORK PROGRAMMING LAB EXPLANATION : This command displays the current system time (in hours) on the screen. : This command displays on the screen the argument of the echo command. : date. 8. TASK COMMAND SYNTAX EXPLANATION : To display user-defined message. : echo “message”. TASK : To display the details of all users. 10. : calendar.

: This command lists the information about all the users who have logged on to that system. 13. : who. TASK COMMAND SYNTAX EXPLANATION : To clear the working terminal. TASK COMMAND SYNTAX EXPLANATION : To display the user detail. : whoami.MC9247– NETWORK PROGRAMMING LAB COMMAND SYNTAX EXPLANATION : who. : This command displays information about the current user of the system on the screen. 12. : clear. : This command clears the contents in the working terminal. : who. : clear. FILE AND DIRECTORY COMMANDS  Command  Command  Command  Command  Command  Command  Command  Command : mkdir : rmdir : cat : pwd : cd : cp : mv : rm Page 11 .

: make directory. TASK COMMAND SYNTAX : To list all the files and directories. directory to another. : list. TASK COMMAND SYNTAX : To delete a directory. 2. 3. : cd . : rmdir directory name specified directory. TASK COMMAND SYNTAX : To change directory. : cd directory name. directory with the specified name. : change directory. : mkdir.MC9247– NETWORK PROGRAMMING LAB  Command  Command 1. 4. Page 12 EXPLANATION : This command is used to create a new EXPLANATION : This command is used to switch from one EXPLANATION : This command is used to delete the EXPLANATION : This command helps in switching to the EXPLANATION : This command displays all the files and .. TASK COMMAND SYNTAX : To come out of a sub-directory. main directory. 5. : ls. TASK COMMAND SYNTAX : ls : wc : To create a directory. : remove directory. directories of the system. : change directory.

: This command leads to the creation of a new file with the specified file name and contents. Page 13 . : remove. TASK COMMAND SYNTAX EXPLANATION : To view a file. : cat. TASK COMMAND SYNTAX EXPLANATION : To copy a file. 10. : cp sourcefile destinationfile. : cat. TASK COMMAND SYNTAX EXPLANATION : To create a file. : cat> file name. TASK COMMAND SYNTAX : To delete a file. : rm file name. : cat file name.MC9247– NETWORK PROGRAMMING LAB 6. : mv sourcefile destinationfile. TASK COMMAND SYNTAX EXPLANATION : To move a file. : This command produces a copy of the source file and is stored in specified destination file by overwriting its previous contents. : This command displays the contents of the specified file. the source file is deleted. the 9. : After moving the contents of the source file into destination file. 7. : move. : copy. 8.

: print working directory. 11.words.characters in a file. : pwd. 12.MC9247– NETWORK PROGRAMMING LAB EXPLANATION : This command deletes the specified file from the directory. Page 14 . TASK COMMAND SYNTAX EXPLANATION FILTERS AND PIPES  Command  Command  Command  Command  Command  Command  Command  Command 1. : This command prints the current working directory. of rows form the top of the specified file. : To print the current working directory. EXPLANATION: This command displays the specified no. TASK COMMAND SYNTAX : head : tail : more : grep : sort : cut : paste : tr : To retrieve a part of a file. : head -no. of rows file name. TASK COMMAND SYNTAX EXPLANATION : To count the file content : word count : wc [-l –w –c] filename. : This command displays the number of lines. : head.

TASK COMMAND SYNTAX : To sort the contents of a file. TASK COMMAND SYNTAX : To search the particular sting in a file. TASK COMMAND SYNTAX : To retrieve a file. TASK COMMAND SYNTAX : To read a text file. : tail -no. of rows form the bottom of the specified file. EXPLANATION : This command helps in sorting the contents of a file in ascending order. : tail. file name. : cut. EXPLANATION : This command displays the characters of a particular column in the specified file. : sort. EXPLANATION: This command displays the specified no. file name. 5. : grep.MC9247– NETWORK PROGRAMMING LAB 2. : sort file name. of rows file name. EXPLANATION : This command finds the particular string and displays if found in the specified file. 3. EXPLANATION : More is a command used to read text files. 4. COMMUNICATION THROUGH UNIX COMMANDS: Page 15 . : more : more text-filename. 6 . TASK COMMAND SYNTAX : To display / cut a column from a file. : grep “string to find”. : cut –c no.

MC9247– NETWORK PROGRAMMING LAB  Command  Command  Command  Command  Command : mesg : write : wall : mail : reply Page 16 .

MC9247– NETWORK PROGRAMMING LAB Exercise No : 2 Date Program to implement Message Queues Aim : To write a C Program to perform message passing using Message Queues Description • • • Message queues provide a way of sending a block of data from one process to another. a program uses the msgget() system call . • • • To allocate a queue.h> #include<unistd.h> #include<string.h> struct msg Page 17 . The message queue itself contains a series of data structures. Other operations related to managing a given message queue are performed by the msgctl() system call int msgget(key_t key. each of which identifies the address.h> #include<sys/ipc. int msgflg). type.h> #include<sys/types. and size of the message plus a pointer to the next message in the queue.h> #include<sys/stat.h> #include<fcntl. Program: MESSAGE SEND #include<stdio. Message queues are implemented as linked lists of data stored in shared memory. Messages are placed in the queue by msgsnd() system calls and retrieved by msgrcv(). one for each message.

f.h> #include<sys/types. printf("\n%d".type=1.1.0). printf("\nThe Msg Received is : %s".m. }m. char ms[20].h> #include<unistd.h> #include<sys/stat."Welcome").h> struct msg { long type. }m. char ms[20].&m. int main() { int id. int main() { int id.IPC_NOWAIT). id=msgget(202. id=msgget(201. if(f==0) printf("\nMessage Send Suffully"). f=msgsnd(id.24.h> #include<sys/ipc.ms.IPC_CREAT|S_IRWXU).MC9247– NETWORK PROGRAMMING LAB { long type. return 0.S_IRWXU). } MESSAGE RECEIVE #include<stdio. return 0. printf("\n%d".id).m. m. strcpy(m.ms. msgrcv(id.ms).h> #include<sys/msg.f. } OUTPUT Page 18 .h> #include<string.24. else printf("\nMessage not send").h> #include<fcntl.id).

c [exam24@proxyserver 2mca]$ .MC9247– NETWORK PROGRAMMING LAB [exam24@proxyserver 2mca]$ cc msgsnd.out The msg received is WELCOME Page 19 ./a.out Message Send Suffully [exam24@proxyserver 2mca]$ cc msgrcv.c [exam24@proxyserver 2mca]$ ./a.

• A pipe can be explicitly created in Unix using the pipe system call. it is common practice to assign the pipe write device descriptor to stdout in the first process and assign the pipe read device descriptor to stdin in the second process. write(p[1]. if(pid>0) { close(p[0]).h> #include<sys/types.h> #include<sys/ipc.16). Program: #include<stdio. char in[20]. This is elaborated below in the discussion of multiple command pipelines. A read from fildes[0] accesses the data written to fildes[1] on a first-infirst-out (FIFO) basis and a read from fildes[1] accesses the data written to fildes[0] also on a FIFO basis.h> void main() { char *msg1="Hello".h> #include<fcntl.msg1.pid. pipe(p). Description • A UNIX pipe provides a one-way flow of data. Two file descriptors are returned--fildes[0] and fildes[1]. • When a pipe is used in a Unix command line. int p[2]. pid=fork(). the first process is assumed to be writing to stdout and the second is assumed to be reading from stdin.MC9247– NETWORK PROGRAMMING LAB Exercise No :3 Date Program to implement Pipes Aim : To write a C Program to implement pipes. Page 20 . and they are both open for reading and writing. So.

read(p[0].c [exam24@proxyserver 2mca]$ . printf("%s".in).MC9247– NETWORK PROGRAMMING LAB } if(pid==0) { close(p[1])./a.in.out MESSAGE : Hello Page 21 . } } OUTPUT [exam24@proxyserver 2mca]$ cc pipe.16).

MC9247– NETWORK PROGRAMMING LAB Exercise No : 4 Date Aim : Program to implement Shared Memory To write a C Program to implement the concept of Shared Memory Description • The shared memory is a mechanism that allows processes to exchange data. if granted permissions. shmid. This is useful for a process that calls the fork() and the exec() functions. One process creates the memory area. In System V. and prototypes are specified in the <sys/shm. A unique number held in the system variable. The structure definitions for the shared memory area control structures. The ownership permission can be modified using the shmctl() system function. identifies the segment. After the segment is attached. may perform control functions on the shared memory by using this system function. A shared memory is a portion of the memory that is shared between processes. Other processes.h> system header file. it may be granted the read or write permission. • A single process may attach a segment many times. • If the child process tries to update any page from the table. and the other process. After the shared memory is created. The shared segment can be removed by calling the shmdt() function. Page 22 . this page is specifically copied and then the copy of the child process modified. • A shared memory segment is created by using a shmget() system function. a shared location can be attached to the address space of a process by using the shmat() system function. the shared memory allows several processes to attach a segment of physical memory to their virtual addresses. which has appropriate permissions accesses the memory.

} #include<stdio. else { strcpy(c.MC9247– NETWORK PROGRAMMING LAB Program: #include<stdio.h> #include<unistd. else { printf("\n Shared memory is created").10.0))==-1) printf("error in operating shared memory"). int shmid.h> #include<sys/stat.h> #include<unistd.IPC_CREAT|S_IRWXU))==-1) printf("\n error in creating shared memory").0))==-1) printf("\n error in attaching address").h> #include<string.h> #include<string.h> #include<sys/types. printf("\n The value is=%s".h> #include<sys/shm.h> int main() { char *c.h> int main() { int shmid.h> #include<sys/types. shmdt(c). if((shmid=shmget(666. } } return 0. if((shmid=shmget(666.10. char *c.h> #include<sys/ipc.h> #include<sys/ipc.c)."welcome").h> #include<sys/shm. if((c=shmat(shmid.NULL.h> #include<sys/stat. Page 23 .h> #include<fcntl.

0))==-1) printf("\n memory not removed").IPC_RMID. if((shmctl(shmid.out Shared memory is created The value is=welcome [exam24@proxyserver 2mca]$ cc shrec./a.c [exam24@proxyserver 2mca]$ . else printf("\n memory is removed")./a.0). } OUTPUT [exam24@proxyserver 2mca]$ cc shsend.MC9247– NETWORK PROGRAMMING LAB else { c=shmat(shmid. } return 0.c).NULL.out received msg is:welcome memory is removed Page 24 .c [exam24@proxyserver 2mca]$ . printf("received msg is:%s". shmdt(c).

MC9247– NETWORK PROGRAMMING LAB Exercise No : 4 Date Implementing byte ordering functions Page 25 .

MC9247– NETWORK PROGRAMMING LAB Exercise No : 5 Date Implementation of address conversion routines Page 26 .

MC9247– NETWORK PROGRAMMING LAB Exercise No : 6 Date Implementation of address conversion routines Page 27 .

MC9247– NETWORK PROGRAMMING LAB Exercise No : 7 Date Program to obtain the local and remote socket address Page 28 .

MC9247– NETWORK PROGRAMMING LAB Exercise No : 8 Date Program to manipulate IP address and test its characteristics Page 29 .

MC9247– NETWORK PROGRAMMING LAB Exercise No : 9 Date Program to manipulate IP address and test its characteristics Page 30 .

MC9247– NETWORK PROGRAMMING LAB Exercise No : 10 Date Program to obtain information about (A) Host (B) Network (C)Protocols (D) Domains Page 31 .

This requires a connection to be established between the sender and receiver. One of the sockets listens for a connection request (the server). using the bzero function Step 9 : Initialize the values of sin_family and sin_port of the serveraddress Step 10 : get the h_addr of the hname and type cast it to struct in_addr pointer and assign this value to the sin_addr as follows Page 32 . the other socket asks for a connection (the client). When the server accepts the connection request from the client. TCP is also a reliable protocol. In TCP there is no limit on the amount of data that can be transmitted. data can then be sent between the server and client. Client Step 1 : include the necessary header files Step 2: declare the two integer values : numbytes. Step 7: call the socket function and check if it is successful or not and store the return value in sockfd Step 8:initialize the values of serveraddr to 0. in that data is received in the same order in which it was sent.MC9247– NETWORK PROGRAMMING LAB Exercise No : 11 Date Program to create TCP sockets for sending and receiving data Aim Write a C program to demonstrate TCP network communication Description • • • • • The stream protocol. also known as TCP. sockfd Step 3 : declare a char buff of size 100 Step 4 : declare a pointer variable hname of type struct hostent Step 5: declare a variable serveraddr of type sockaddr_in Step 6: use the gethostbyname() function to lookup the server name and check if it was successful or not . is connection orientated.

Check if it is successful or not Step 10 : use the send function to send some data to the client and check if it was successfully sent or not ( note that you must use the newsockfd to send the data and not the sockfd) Program: Page 33 . Step 4: using the socket function create a socket and check if it was successful or not Step 5: initialize the values of the myaddress to zero using bzero function Step 6: Initialize the values of sin_family. Step 3:declare a variable clientaddress of type structure sockaddr_in.MC9247– NETWORK PROGRAMMING LAB Step 11 : use the connect function to establish the connection with the server and check if it successful or not Step 12 : use recv() function to receive the data sent by the server and check if it was successfully received or not Step 13: Print the data that was received on the screen Server Step 1: declare two integer values – sockfd and newsockfd Step 2: declare a variable myaddress of type structure sockaddr_in. sin_port and in_addr (INADDR_ANY) Step 7: Use the bind() function to get the local protocol address of the server and check if it is successful or not Step 8: Use the listen() function to wait for a connection and check if it is successful or not Step 9:use the accept () function to accept a client connection and store the return value in the newsockfd variable .

socklen_t *addrlen) Algorithm Client Step 1 : include the necessary header files Step 2: declare the two integer values : numbytes. also known as UDP. the socket and destination computers address must be included. is connectionless. This means that each time a datagram (a packet of data to a destination) is sent. Page 34 . the client just sends a datagram to the server using sendto() Similarly the server does not accept a connection from the client. size_t nbytes. There is a limit of 64KB for datagrams sent to a specific location. int flags. Instead it just calls a recvfrom() Recvfrom(int sockfd. const void *buff. socklen_t *addrlen) Sendto(int sockfd. as there is no guarantee that the datagrams sent will arrive in the same order at the destination. void *buff. sockfd Step 3 : declare a char buff of size 100 Step 4 : declare a pointer variable hname of type struct hostent Step 5: declare a variable serveraddr of type sockaddr_in Step 6: use the gethostbyname() function to lookup the server name and check if it was successful or not . struct sockaddr *from . size_t nbytes.MC9247– NETWORK PROGRAMMING LAB Exercise No : 12 Date Program to create UDP sockets for sending and receiving data Aim Write a C program to demonstrate UDP network communication Description • • • • • • • The datagram protocol. struct sockaddr *to. UDP is also unreliable. Client does not establish a connection with the server Instead. int flags.

Step 3:declare a variable clientaddress of type structure sockaddr_in. using the bzero function Step 9 : Initialize the values of sin_family and sin_port of the serveraddress Step 10 : get the h_addr of the hname and type cast it to struct in_addr pointer and assign this value to the sin_addr as follows Step 11 : use the connect function to establish the connection with the server and check if it successful or not Step 12 : use recv() function to receive the data sent by the server and check if it was successfully received or not Step 13: Print the data that was received on the screen Server Step 1: declare two integer values – sockfd and newsockfd Step 2: declare a variable myaddress of type structure sockaddr_in. sin_port and in_addr (INADDR_ANY) Step 7: Use the bind() function to get the local protocol address of the server and check if it is successful or not Step 8: Use the listen() function to wait for a connection and check if it is successful or not Step 9:use the accept () function to accept a client connection and store the return value in the newsockfd variable . Step 4: using the socket function create a socket and check if it was successful or not Step 5: initialize the values of the myaddress to zero using bzero function Step 6: Initialize the values of sin_family. Check if it is successful or not Step 10 : use the send function to send some data to the client and check if it was successfully sent or not ( note that you must use the newsockfd to send the data and not the sockfd) Program: Page 35 .MC9247– NETWORK PROGRAMMING LAB Step 7: call the socket function and check if it is successful or not and store the return value in sockfd Step 8:initialize the values of serveraddr to 0.

MC9247– NETWORK PROGRAMMING LAB Exercise No : 13 Date Program to implement a chat server using TCP/IP protocol Aim To write a program for implementing Client-Server Chat using TCP. Step3: Call with parameters AF_INET as domain and SOCK_STREAM as type. Client Step1: Start the program. Step2:Create an unnamed socket for client using socket() system. Step5: Accept the connection using accept() system call when client requests for connection. Step7: If the message is not equal to‘Bye’ then write the message to the client and goto step6. Algorithm Server Step1:Start the program. Step2: Create an unnamed socket for the server using the Parameters AF_INET as domain and the SOCK_STREAM as type. Step3: Name the socket using bind() system call with the parameters server_sockfd and the server address(sin_addr and sin_sport). Step6: Get the message which has to be sent to the client and check that it is not equal to ‘Bye’. Step4: Name the socket using bind() system call. Step8: If the message is ‘Bye’ then terminate the Process. Step4: Create a connection queue and wait for clients using the listen() system call with number of clients request as parameters. Page 36 . Step9: Stop the program execution.

Step6: Read the message from the server socket and compare it with ‘Bye’. Program: Page 37 . Step8: Get the message from the client side.MC9247– NETWORK PROGRAMMING LAB Step5: Now connect the socket to server using connect() system call. Step11: Stop the program. Step10: If the message is equal to ‘Bye’ then print good bye message and terminate the process. Step9: Write the message is equal to server sockfd and goto step4. Step7: If the message is not equal to ‘Bye’ then print the message to the serverm output device and repeat the steps 6&7.

Step 5: wait for the client request on request establish a connection using accept() function. Step 10: Repeat steps 8&9 until the end of the source file. Step 4: Bind the server to socket using bind function. Page 38 . Step 7: Open the source and destination files. Step 9: Receive the line back from the client. Client: Step 1: start Step 2: Create a socket with address family AEINET type SOCK_STREAM and default protocol. Step 6: Read the source and destination files names from the client. Step 8: Read one line of source file and send it to client. Step 11: close the source and destination files.MC9247– NETWORK PROGRAMMING LAB Exercise No : 14 Date Program to implement FTP Aim : To write a C program to implement File transfer Algorithm : Server: Step 1: Start Step 2: create a socket with address family AEINET. type SOCK_STERAM and default protocol. Step 12: close the connection and goto step5. Step 3: Initialize the socket and set its attributes.

Step 9: stop. Step 7: send the string to the server. Step 6: Recive the string from the server and print it at the console. Step 8: Repeat step6&7 until the server terminates and connection. Step 4: Connect to server using connect () function to initiate the request. Step 5: send the source and destination file names to server.MC9247– NETWORK PROGRAMMING LAB Step 3: Initialize the socket and set its attribute set required port no. Program: Page 39 .

For the highest possible throughput. The limit on the amount of data that it can send before stopping to wait for an acknowledgment should be larger than the bandwidth-delay product of the communications link. discarding duplicate packets and identifying missing ones.MC9247– NETWORK PROGRAMMING LAB Exercise No : 15 Date Simulation of Sliding Window protocol Aim : To write a C program to implement Sliding window protocol Description : A sliding window protocol is a feature of packet-based data transmission protocols. and the receiver uses the numbers to place received packets in the correct order. Algorithm : Step 1: start the program. it is important that the transmitter is not forced to stop sending by the sliding window protocol earlier than one round-trip delay time (RTT). Conceptually. The problem with this is that there is no limit of the size of the sequence numbers that can be required. such as in the Data Link Layer (OSI model) as well as in the Transmission Control Protocol (TCP). Step 2: Open the input file in read mode. each portion of the transmission (packets in most data link layers. a sliding window protocol allows an unlimited number of packets to be communicated using fixed-size sequence numbers. If it is not. Sliding window protocols are used where reliable in-order delivery of packets is required. Page 40 . Step 3: Read the size of the window Step 4: Select randomly the number of packets is to be transferred. the protocol will limit the effective bandwidth of the link. but bytes in TCP) is assigned a unique consecutive sequence number. By placing limits on the number of packets that can be transmitted or received at any given time.

Step 8: Close the file. Step 6: Transfer the packet until it reaches the maximum defined size. Step 9: Stop the program. Program: Page 41 . Step 7: Resume the window size and repeat the above two steps until packets in.MC9247– NETWORK PROGRAMMING LAB Step 5: Read the content of the input file.

5. BGP v4 is the routing protocol used by the public Internet. Assign the distance from a node to itself as zero. which similarly disseminates topology information between routers. such as OSPF and IS-IS . Interior gateway routing via path vector or distance vector protocols. routers gain knowledge of the topology of the network. the choice of the route being done by routing algorithms. 7. disseminating information that enables them to select routes then any two nodes on a computer network. 2. such as RIP. A routing protocol shares this information first among immediate neighbors. Stop. Read the distance between one node to the other for every node. Page 42 . Algorithm 1 . Each router has a priori knowledge only of networks attached to it directly.MC9247– NETWORK PROGRAMMING LAB Exercise No : 15 Date Simulation of Routing protocols Aim To write a C program to implement BGP/OSPF Routing Protocol Description : A routing protocol is a protocol that specifies how routers communicate with each other. and then throughout the network. 3. This way. of nodes.Start. Although there are many types of routing protocols. 6. Exterior gateway routing. 4. Read the no. Print the distance from one node to every other node in form of a matrix. Find the shortest path from the given node to other node and assign the same as distance. three major classes are in widespread use on IP networks: Interior gateway routing via linkstate routing protocols. IGRP and EIGRP . The term routing protocol may refer specifically to one operating at layer three of the OSI model.

MC9247– NETWORK PROGRAMMING LAB Program: Page 43 .

MC9247– NETWORK PROGRAMMING LAB Exercise No : 16 Date Implementation of RPC Page 44 .

Step2: Create the socket for the server. the IP address for the given host eventually arrives at the resolver. that in turn completes the request over Internet Protocol. The DNS database resides on a hierarchy of special database servers. Algorithm Server Step1: Start the program.MC9247– NETWORK PROGRAMMING LAB Exercise No : 17 Date Implementation of DNS Aim To write a program for the simulation of Domain Name System. Description : The DNS translates Internet domain and host names to IP addresses. DNS implements a distributed database to store this name and address information for all public hosts on the Internet. a piece of software called the DNS resolver (usually built into the network operating system) first contacts a DNS server to determine the server's IP address. it will in turn forward the request to a different DNS server at the next higher level in the hierarchy. Page 45 . After potentially several forwarding and delegation messages are sent within the DNS hierarchy. Step3: Bind the socket to the port. DNS automatically converts the names we type in our Web browser address bar to the IP addresses of Web servers hosting those sites. If the DNS server does not contain the needed mapping. When clients like Web browsers issue requests involving Internet host names. DNS assumes IP addresses do not change (are statically assigned rather than dynamically assigned).

Program: Page 46 . Step8: If domain matches then send the corresponding address to the client. Step2: Create the socket for the client. Step5: Receive the IP address from the client to be resolved. Step7: Check the existence of the domain in the server. Step3: Connect the socket to the Server. Step9: Stop the program execution. Client Step1: Start the Program.MC9247– NETWORK PROGRAMMING LAB Step4: Listen for the incoming client connection. Step4: Send the host name to the server to be resolved. Step5: If the server corresponds then print the address and terminate the process. Step6: Get the domain name for the client.

MC9247– NETWORK PROGRAMMING LAB Exercise No : 18 Date Implementation of HTTP Page 47 .

MC9247– NETWORK PROGRAMMING LAB Exercise No : 19 Date Implementation of E-mail Page 48 .

MC9247– NETWORK PROGRAMMING LAB Exercise No : 20 Date Implementation of Multi-user chat Page 49 .

MC9247– NETWORK PROGRAMMING LAB Exercise No : 21 Date Program to create sockets for handling multiple connections Page 50 .

MC9247– NETWORK PROGRAMMING LAB Exercise No : 22 Date Perform Socket Programming Page 51 .

MC9247– NETWORK PROGRAMMING LAB Exercise No : 23 Date Program to trace the port of a particular host Page 52 .

MC9247– NETWORK PROGRAMMING LAB Exercise No : 23 Date Ping Program Page 53 .

MC9247– NETWORK PROGRAMMING LAB Page 54 .

MC9247– NETWORK PROGRAMMING LAB Page 55 .