68
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 Page 1

Mc9247 Network Programming Lab Manual(1)

Embed Size (px)

Citation preview

Page 1: Mc9247 Network Programming Lab Manual(1)

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

Page 2: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

TABLE OF CONTENTS

Exer.

NoProgram

1. Unix Network commands

2. Program to implement Message Queues

3. Program to implement Pipes

4. Program to implement Shared Memory

5. Implementing byte ordering functions

6. Implementation of address conversion routines

7. Program to obtain the local and remote socket address

8. Program to manipulate IP address and test its characteristics

9. Program to obtain information about

(A) Host (B) Network (C)Protocols (D) Domains

10. Program to create TCP sockets for sending and receiving data

11. Program to create UDP sockets for sending and receiving data

12. Program to implement a chat server using TCP/IP protocol

13. Program to implement FTP

14. Simulation of Sliding Window protocol

15. Simulation of Routing protocols

16. Implementation of RPC

17. Implementation of DNS

Page 2

Page 3: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

18. Implementation of HTTP

19. Implementation of E-mail

20.

I

Implementation of Multi-user chat

21. Program to create sockets for handling multiple connections

22. Perform Socket Programming

23. Program to trace the port of a particular host

24. Ping Program

Page 3

Page 4: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Exercise No : 1

Unix Commands for NetworkDate

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                  HWtype  HWaddress           Flags Mask    Iface

stugate6                ether   00:90:AB:C2:A0:00   C     *               eth0

st-csi-a-dyn-01.baylor ether   00:A0:C9:D7:5A:C5   C     *               eth0

plum.baylor.edu         ether   00:A0:C9:D7:2C:06   C     *               eth0

spud.baylor.edu         ether   00:20:AF:E8:EE:25   C     *               eth0

michaela.baylor.edu    ether   00:00:94:7B:09:3C   C     *               eth0

GL09.baylor.edu         ether   00:A0:C9:67:F4:8B   C     *               eth0

129.62.148.38           ether   00:A0:C9:D7:5A:E4   C     *               eth0

burro.baylor.edu        ether   00:00:94:75:45:13   C     *               eth0

GL14.baylor.edu         ether   00:A0:24:0C:42:B9   C     *               eth0

GL15.baylor.edu         ether   00:A0:24:0C:41:F5   C     *               eth0

Page 4

Page 5: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

GL16.baylor.edu         ether   00:A0:C9:67:F5:63   C     *               eth0

lisa.baylor.edu         ether   00:A0:C9:D7:2C:07   C     *               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

Page 6: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

PING pgserver.com (217.40.136.141) 56(84) bytes of data.

64 bytes from host217-40-136-141.in-addr.btopenworld.com

(217.40.136.141): icmp_

seq=0 ttl=110 time=209 ms

64 bytes from host217-40-136-141.in-addr.btopenworld.com

(217.40.136.141): icmp_

seq=1 ttl=110 time=221 ms

64 bytes from host217-40-136-141.in-addr.btopenworld.com

(217.40.136.141): icmp_

seq=2 ttl=110 time=222 ms

64 bytes from host217-40-136-141.in-addr.btopenworld.com

(217.40.136.141): icmp_

seq=3 ttl=110 time=223 ms

64 bytes from host217-40-136-141.in-addr.btopenworld.com

(217.40.136.141): icmp_

seq=4 ttl=110 time=203 ms

3. netstat

The network status command netstat displays status information about the

network interfaces on the host machine and it can display routing table

information.

Usage :

[mcastaff@proxyserver ~]$ netstat

Active Internet connections (w/o servers)

Proto Recv-Q Send-Q Local Address Foreign Address State

tcp 0 0 172.16.2.215:squid 172.16.1.46:1289 TIME_WAIT

tcp 0 0 172.16.2.215:squid 172.16.15.3:1066 ESTABLISHED

tcp 0 0 172.16.2.215:squid 172.16.15.3:1067 ESTABLISHED

tcp 0 0 172.16.2.215:squid 172.16.1.46:1291 TIME_WAIT

tcp 0 0 172.16.2.215:squid 172.16.15.3:1064 ESTABLISHED

tcp 0 0 172.16.2.215:squid 172.16.15.3:1065 TIME_WAIT

tcp 0 0 172.16.2.215:squid 172.16.1.46:1290 TIME_WAIT

Page 6

Page 7: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

tcp 0 0 220.227.30.50:38418 203.199.200.2:http ESTABLISHED

tcp 0 0 220.227.30.50:38417 203.199.200.2:http ESTABLISHED

tcp 0 0 220.227.30.50:38416 203.199.200.2:http ESTABLISHED

tcp 0 0 220.227.30.50:38412 203.199.200.2:http ESTABLISHED

tcp 0 0 172.16.2.215:squid 172.16.1.46:1293 TIME_WAIT

tcp 0 0 220.227.30.50:38489 216.73.84.17:http ESTABLISHED

tcp 0 0 172.16.2.215:squid 172.16.1.46:1292 TIME_WAIT

tcp 0 1042 172.16.2.215:squid 172.16.15.3:1068 ESTABLISHED

tcp 0 0 220.227.30.50:38326 203.27.235.22:http ESTABLISHED

tcp 0 0 172.16.2.215:squid 172.16.1.46:1281 TIME_WAIT

tcp 0 0 172.16.2.215:squid 172.16.1.46:1280 TIME_WAIT

tcp 0 0 172.16.2.215:squid 172.16.1.46:1282 TIME_WAIT

tcp 0 0 172.16.2.215:squid 172.16.1.46:1285 TIME_ WAIT

4. nslookup <machine name>

This command is used to Query name server to map names (e.g.

burro.baylor.edu) to IP address and IP addresses to names (the reverse). 

Under UNIX, the default name server for a machine can be found in the

/etc/resolv.conf file

Usage :

$ nslookup burro.baylor.edu

Server:  ccis03.baylor.edu               <= Name of queried name server

Address:  129.62.16.4                    <= IP address of queried name server

Name:    BURRO.baylor.edu                <= Name of machine

Address:  129.62.148.8                   <= IP address of machine

S nslookup 129.62.148.8                  <= Reverse lookup

Server:  ccis03.baylor.edu

Address:  129.62.16.4

Page 7

Page 8: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Name:    burro.baylor.edu

Address:  129.62.148.8

5. ifconfig

The ifconfig command is used to display the local interface configuration

(winipcfg for Windows) and to modify the configuration. Local interfaces can

be Ethernet network cards, modems, etc..

6. traceroute

Traceroute displays the routers that are passed through to reach the

destination.

Usage :

[mcastaff@proxyserver ~]$ traceroute 172.16.2.215

traceroute to 172.16.2.215 (172.16.2.215), 30 hops max, 38 byte packets

1 172.16.2.215 (172.16.2.215) 0.069 ms 0.048 ms 0.026 ms

BASIC SHELL COMMANDS

GENERAL PURPOSE COMMANDS:

Command : Date

Command : cal

Command : echo

Command : who am i

Command : who

Command : clear

1. TASK : To display the system date and time.

Page 8

Page 9: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

COMMAND : date.

SYNTAX : date.

EXPLANATION : This command displays the current system

date and time on the screen.

2. TASK : To display the current month.

COMMAND : date.

SYNTAX : date +%m.

EXPLANATION : This command displays the current month on

the screen.

3. TASK : To display the name of the current month.

COMMAND : date.

SYNTAX : date +%h.

EXPLANATION : This command displays the name of the

current month on the screen.

4. TASK : To display the current system date.

COMMAND : date.

SYNTAX : date +%d.

EXPLANATION : This command displays the current system

date on the screen.

5. TASK : To display the current system date (year).

COMMAND : date.

SYNTAX : date +%y.

EXPLANATION : This command displays the current year on

the screen.

6. TASK : To display the current system time.

COMMAND : date.

SYNTAX : date +%H.

Page 9

Page 10: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

EXPLANATION : This command displays the current system

time (in hours) on the screen.

7. TASK : To display the current system time.

COMMAND : date.

SYNTAX : date +%M.

EXPLANATION : This command displays the current system

time (in minutes) on the screen.

8. TASK : To display the current system time.

COMMAND : date.

SYNTAX : date +%S.

EXPLANATION : This command displays the current system

time (in seconds) on the screen.

9. TASK : To display the calendar of the current

month.

COMMAND : calendar.

SYNTAX : cal.

EXPLANATION : This command displays the calendar of the

current month on the screen.

10. TASK : To display user-defined message.

COMMAND : echo.

SYNTAX : echo “message”.

EXPLANATION : This command displays on the screen the

argument of the echo command.

11. TASK : To display the details of all users.

Page 10

Page 11: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

COMMAND : who.

SYNTAX : who.

EXPLANATION : This command lists the information about

all the users who have logged on to that

system.

12. TASK : To display the user detail.

COMMAND : who.

SYNTAX : whoami.

EXPLANATION : This command displays information about

the current user of the system on the

screen.

13. TASK : To clear the working terminal.

COMMAND : clear.

SYNTAX : clear.

EXPLANATION : This command clears the contents in the

working terminal.

FILE AND DIRECTORY COMMANDS

Command : mkdir

Command : rmdir

Command : cat

Command : pwd

Command : cd

Command : cp

Command : mv

Command : rm

Command : ls

Page 11

Page 12: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Command : wc

1. TASK : To create a directory.

COMMAND : make directory.

SYNTAX : mkdir.

EXPLANATION : This command is used to create a new

directory with the specified name.

2. TASK : To change directory.

COMMAND : change directory.

SYNTAX : cd directory name.

EXPLANATION : This command is used to switch from one

directory to another.

3. TASK : To delete a directory.

COMMAND : remove directory.

SYNTAX : rmdir directory name

EXPLANATION : This command is used to delete the

specified directory.

4. TASK : To come out of a sub-directory.

COMMAND : change directory.

SYNTAX : cd ..

EXPLANATION : This command helps in switching to the

main directory.

5. TASK : To list all the files and directories.

COMMAND : list.

SYNTAX : ls.

EXPLANATION : This command displays all the files and

directories of the system.

Page 12

Page 13: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

6. TASK : To create a file.

COMMAND : cat.

SYNTAX : cat> file name.

EXPLANATION : This command leads to the creation of

a new file with the specified file

name and contents.

7. TASK : To view a file.

COMMAND : cat.

SYNTAX : cat file name.

EXPLANATION : This command displays the contents of

the specified file.

8. TASK : To copy a file.

COMMAND : copy.

SYNTAX : cp sourcefile destinationfile.

EXPLANATION : This command produces a copy of the

source file and is stored in the

specified destination file by

overwriting its previous contents.

9. TASK : To move a file.

COMMAND : move.

SYNTAX : mv sourcefile destinationfile.

EXPLANATION : After moving the contents of the

source file into destination file, the

source file is deleted.

10. TASK : To delete a file.

COMMAND : remove.

SYNTAX : rm file name.

EXPLANATION : This command deletes the specified

Page 13

Page 14: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

file from the directory.

11. TASK : To count the file content

COMMAND : word count

SYNTAX : wc [-l –w –c] filename.

EXPLANATION : This command displays the number of

lines,words,characters in a file.

12. TASK : To print the current working directory.

COMMAND : print working directory.

SYNTAX : pwd.

EXPLANATION : This command prints the current

working directory.

FILTERS AND PIPES

Command : head

Command : tail

Command : more

Command : grep

Command : sort

Command : cut

Command : paste

Command : tr

1. TASK : To retrieve a part of a file.

COMMAND : head.

SYNTAX : head -no. of rows file name.

EXPLANATION: This command displays the specified no. of

rows form the top of the specified file.

Page 14

Page 15: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

2. TASK : To retrieve a file.

COMMAND : tail.

SYNTAX : tail -no. of rows file name.

EXPLANATION: This command displays the specified no. of

rows form the bottom of the specified file.

3. TASK : To sort the contents of a file.

COMMAND : sort.

SYNTAX : sort file name.

EXPLANATION : This command helps in sorting the

contents of a file in ascending order.

4. TASK : To display / cut a column from a file.

COMMAND : cut.

SYNTAX : cut –c no. file name.

EXPLANATION : This command displays the characters of a

particular column in the specified file.

5. TASK : To search the particular sting in a file.

COMMAND : grep.

SYNTAX : grep “string to find”. file name.

EXPLANATION : This command finds the particular string

and displays if found in the specified file.

6 . TASK : To read a text file.

COMMAND : more

SYNTAX : more text-filename.

EXPLANATION : More is a command used to read text files.

COMMUNICATION THROUGH UNIX COMMANDS:

Command : mesg

Page 15

Page 16: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Command : write

Command : wall

Command : mail

Command : reply

Page 16

Page 17: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Exercise No : 2Program to implement Message Queues

Date

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.

Message queues are implemented as linked lists of data stored in shared

memory.

The message queue itself contains a series of data structures, one for each

message, each of which identifies the address, type, and size of the message

plus a pointer to the next message in the queue.

To allocate a queue, a program uses the msgget() system call .

Messages are placed in the queue by msgsnd() system calls and retrieved by

msgrcv().

Other operations related to managing a given message queue are performed by

the msgctl() system call

int msgget(key_t key, int msgflg);

Program:

MESSAGE SEND

#include<stdio.h>#include<unistd.h>#include<fcntl.h>#include<sys/types.h>#include<string.h>#include<sys/stat.h>#include<sys/ipc.h>struct msg{

Page 17

Page 18: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

long type; char ms[20];}m;

int main(){ int id,f; id=msgget(201,IPC_CREAT|S_IRWXU); printf("\n%d",id);

m.type=1; strcpy(m.ms,"Welcome");

f=msgsnd(id,m.ms,24,IPC_NOWAIT); if(f==0) printf("\nMessage Send Suffully"); else printf("\nMessage not send"); return 0;}

MESSAGE RECEIVE

#include<stdio.h>#include<unistd.h>#include<string.h>#include<sys/stat.h>#include<fcntl.h>#include<sys/types.h>#include<sys/ipc.h>#include<sys/msg.h>struct msg{ long type; char ms[20];}m;

int main(){

int id,f; id=msgget(202,S_IRWXU); printf("\n%d",id); msgrcv(id,&m,24,1,0); printf("\nThe Msg Received is : %s",m.ms);

return 0;}

OUTPUT

Page 18

Page 19: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

[exam24@proxyserver 2mca]$ cc msgsnd.c[exam24@proxyserver 2mca]$ ./a.outMessage Send Suffully

[exam24@proxyserver 2mca]$ cc msgrcv.c[exam24@proxyserver 2mca]$ ./a.outThe msg received is WELCOME

Page 19

Page 20: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Exercise No :3Program to implement Pipes

Date

Aim :

To write a C Program to implement pipes.

Description

A UNIX pipe provides a one-way flow of data.

A pipe can be explicitly created in Unix using the pipe system call. Two file

descriptors are returned--fildes[0] and fildes[1], and they are both open for reading

and writing. A read from fildes[0] accesses the data written to fildes[1] on a first-in-

first-out (FIFO) basis and a read from fildes[1] accesses the data written to fildes[0]

also on a FIFO basis.

When a pipe is used in a Unix command line, the first process is assumed to be

writing to stdout and the second is assumed to be reading from stdin. So, 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.

This is elaborated below in the discussion of multiple command pipelines.

Program:

#include<stdio.h>#include<fcntl.h>#include<sys/ipc.h>#include<sys/types.h>void main(){ char *msg1="Hello"; char in[20]; int p[2],pid; pipe(p); pid=fork(); if(pid>0) { close(p[0]); write(p[1],msg1,16);

Page 20

Page 21: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

} if(pid==0) { close(p[1]); read(p[0],in,16); printf("%s",in); }}

OUTPUT

[exam24@proxyserver 2mca]$ cc pipe.c[exam24@proxyserver 2mca]$ ./a.out

MESSAGE : Hello

Page 21

Page 22: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Exercise No : 4Program to implement Shared Memory

Date

Aim :

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.

One process creates the memory area, and the other process, which has

appropriate permissions accesses the memory. In System V, the shared

memory allows several processes to attach a segment of physical memory to

their virtual addresses. A shared memory is a portion of the memory that is

shared between processes.

If the child process tries to update any page from the table, this page is

specifically copied and then the copy of the child process modified. This is

useful for a process that calls the fork() and the exec() functions.

A shared memory segment is created by using a shmget() system function.

The ownership permission can be modified using the shmctl() system

function. Other processes, if granted permissions, may perform control

functions on the shared memory by using this system function. After the

shared memory is created, a shared location can be attached to the address

space of a process by using the shmat() system function. After the segment is

attached, it may be granted the read or write permission.

A single process may attach a segment many times. A unique number held in

the system variable, shmid, identifies the segment. The shared segment can be

removed by calling the shmdt() function. The structure definitions for the

shared memory area control structures, and prototypes are specified in the

<sys/shm.h> system header file.

Program:

Page 22

Page 23: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

#include<stdio.h>#include<sys/ipc.h>#include<sys/types.h>#include<sys/shm.h>#include<unistd.h>#include<sys/stat.h>#include<string.h>int main(){ char *c; int shmid; if((shmid=shmget(666,10,IPC_CREAT|S_IRWXU))==-1) printf("\n error in creating shared memory"); else { printf("\n Shared memory is created"); if((c=shmat(shmid,NULL,0))==-1) printf("\n error in attaching address"); else { strcpy(c,"welcome"); printf("\n The value is=%s",c); shmdt(c); } }return 0;}

#include<stdio.h>#include<fcntl.h>#include<sys/types.h>#include<sys/ipc.h>#include<sys/stat.h>#include<sys/shm.h>#include<unistd.h>#include<string.h>int main(){ int shmid; char *c; if((shmid=shmget(666,10,0))==-1) printf("error in operating shared memory"); else

Page 23

Page 24: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

{ c=shmat(shmid,NULL,0); printf("received msg is:%s",c); shmdt(c); if((shmctl(shmid,IPC_RMID,0))==-1) printf("\n memory not removed"); else printf("\n memory is removed"); }return 0;}

OUTPUT

[exam24@proxyserver 2mca]$ cc shsend.c[exam24@proxyserver 2mca]$ ./a.outShared memory is createdThe value is=welcome

[exam24@proxyserver 2mca]$ cc shrec.c[exam24@proxyserver 2mca]$ ./a.outreceived msg is:welcomememory is removed

Page 24

Page 25: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Exercise No : 4Implementing byte ordering functions

Date

Page 25

Page 26: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Exercise No : 5 Implementation of address conversion routinesDate

Page 26

Page 27: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Exercise No : 6 Implementation of address conversion routinesDate

Page 27

Page 28: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Exercise No : 7Program to obtain the local and remote socket address

Date

Page 28

Page 29: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Exercise No : 8 Program to manipulate IP address and test its

characteristicsDate

Page 29

Page 30: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Exercise No : 9 Program to manipulate IP address and test its

characteristicsDate

Page 30

Page 31: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Exercise No : 10 Program to obtain information about

(A) Host (B) Network (C)Protocols (D) Domains Date

Page 31

Page 32: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Exercise No : 11 Program to create TCP sockets for sending and

receiving dataDate

Aim

Write a C program to demonstrate TCP network communication

Description

The stream protocol, also known as TCP, is connection orientated.

This requires a connection to be established between the sender and receiver.

One of the sockets listens for a connection request (the server), the other

socket asks for a connection (the client).

When the server accepts the connection request from the client, data can then

be sent between the server and client.

In TCP there is no limit on the amount of data that can be transmitted. TCP is

also a reliable protocol, in that data is received in the same order in which it

was sent.

Client

Step 1 : include the necessary header files

Step 2: declare the two integer values : numbytes, 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 .

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, 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

Page 32

Page 33: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

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;

Step 3:declare a variable clientaddress of type structure sockaddr_in;

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, 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 . 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

Page 34: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Exercise No : 12 Program to create UDP sockets for sending and

receiving dataDate

Aim

Write a C program to demonstrate UDP network communication

Description

The datagram protocol, also known as UDP, is connectionless.

This means that each time a datagram (a packet of data to a destination) is

sent, the socket and destination computers address must be included.

There is a limit of 64KB for datagrams sent to a specific location.

UDP is also unreliable, as there is no guarantee that the datagrams sent will

arrive in the same order at the destination.

Client does not establish a connection with the server

Instead, the client just sends a datagram to the server using sendto()

Similarly the server does not accept a connection from the client. Instead it

just calls a recvfrom()

Recvfrom(int sockfd, void *buff, size_t nbytes, int flags, struct sockaddr

*from , socklen_t *addrlen)

Sendto(int sockfd, const void *buff, size_t nbytes, int flags, struct sockaddr

*to, socklen_t *addrlen)

Algorithm

Client

Step 1 : include the necessary header files

Step 2: declare the two integer values : numbytes, 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 .

Step 7: call the socket function and check if it is successful or not and store

Page 34

Page 35: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

the return value in sockfd

Step 8:initialize the values of serveraddr to 0, 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;

Step 3:declare a variable clientaddress of type structure sockaddr_in;

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, 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 . 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

Page 36: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Exercise No : 13 Program to implement a chat server using TCP/IP

protocolDate

Aim

To write a program for implementing Client-Server Chat using TCP.

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).

Step4: Create a connection queue and wait for clients using the listen() system call

with number of clients request as parameters.

Step5: Accept the connection using accept() system call when client requests for

connection.

Step6: Get the message which has to be sent to the client and check that it is not equal

to ‘Bye’.

Step7: If the message is not equal to‘Bye’ then write the message to the client and

goto step6.

Step8: If the message is ‘Bye’ then terminate the Process.

Step9: Stop the program execution.

Client

Step1: Start the program.

Step2:Create an unnamed socket for client using socket() system.

Step3: Call with parameters AF_INET as domain and SOCK_STREAM as type.

Step4: Name the socket using bind() system call.

Step5: Now connect the socket to server using connect() system call.

Page 36

Page 37: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Step6: Read the message from the server socket and compare it with ‘Bye’.

Step7: If the message is not equal to ‘Bye’ then print the message to the serverm

output device and repeat the steps 6&7.

Step8: Get the message from the client side.

Step9: Write the message is equal to server sockfd and goto step4.

Step10: If the message is equal to ‘Bye’ then print good bye message and terminate

the process.

Step11: Stop the program.

Program:

Page 37

Page 38: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Exercise No : 14Program to implement FTP

Date

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 3: Initialize the socket and set its attributes.

Step 4: Bind the server to socket using bind function.

Step 5: wait for the client request on request establish a connection using accept()

function.

Step 6: Read the source and destination files names from the client.

Step 7: Open the source and destination files.

Step 8: Read one line of source file and send it to client.

Step 9: Receive the line back from the client.

Step 10: Repeat steps 8&9 until the end of the source file.

Step 11: close the source and destination files.

Step 12: close the connection and goto step5.

Client:

Step 1: start

Step 2: Create a socket with address family AEINET type SOCK_STREAM and

default protocol.

Page 38

Page 39: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Step 3: Initialize the socket and set its attribute set required port no.

Step 4: Connect to server using connect () function to initiate the request.

Step 5: send the source and destination file names to server.

Step 6: Recive the string from the server and print it at the console.

Step 7: send the string to the server.

Step 8: Repeat step6&7 until the server terminates and connection.

Step 9: stop.

Program:

Page 39

Page 40: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Exercise No : 15 Simulation of Sliding Window protocolDate

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. Sliding

window protocols are used where reliable in-order delivery of packets is required, such as in

the Data Link Layer (OSI model) as well as in the Transmission Control Protocol (TCP).

Conceptually, each portion of the transmission (packets in most data link layers, but bytes in

TCP) is assigned a unique consecutive sequence number, and the receiver uses the numbers

to place received packets in the correct order, discarding duplicate packets and identifying

missing ones. The problem with this is that there is no limit of the size of the sequence

numbers that can be required. By placing limits on the number of packets that can be

transmitted or received at any given time, a sliding window protocol allows an unlimited

number of packets to be communicated using fixed-size sequence numbers. For the highest

possible throughput, 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). 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. If it is not, the protocol

will limit the effective bandwidth of the link.

Algorithm :

Step 1: start the program.

Step 2: Open the input file in read mode.

Step 3: Read the size of the window

Step 4: Select randomly the number of packets is to be transferred.

Page 40

Page 41: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Step 5: Read the content of the input file.

Step 6: Transfer the packet until it reaches the maximum defined size.

Step 7: Resume the window size and repeat the above two steps until packets in.

Step 8: Close the file.

Step 9: Stop the program.

Program:

Page 41

Page 42: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Exercise No : 15 Simulation of Routing protocolsDate

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,

disseminating information that enables them to select routes then any two nodes on a

computer network, the choice of the route being done by routing algorithms. Each router has

a priori knowledge only of networks attached to it directly. A routing protocol shares this

information first among immediate neighbors, and then throughout the network. This way,

routers gain knowledge of the topology of the network. The term routing protocol may refer

specifically to one operating at layer three of the OSI model, which similarly disseminates

topology information between routers. Although there are many types of routing protocols,

three major classes are in widespread use on IP networks: Interior gateway routing via link-

state routing protocols, such as OSPF and IS-IS , Interior gateway routing via path vector or

distance vector protocols, such as RIP, IGRP and EIGRP , Exterior gateway routing. BGP v4

is the routing protocol used by the public Internet.

Algorithm

1 .Start.

2. Read the no. of nodes.

3. Read the distance between one node to the other for every node.

4. Assign the distance from a node to itself as zero.

5. Find the shortest path from the given node to other node and assign the same as distance.

6. Print the distance from one node to every other node in form of a matrix.

7. Stop.

Page 42

Page 43: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Program:

Page 43

Page 44: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Exercise No : 16 Implementation of RPCDate

Page 44

Page 45: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Exercise No : 17 Implementation of DNSDate

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 automatically

converts the names we type in our Web browser address bar to the IP addresses of Web

servers hosting those sites. DNS implements a distributed database to store this name and

address information for all public hosts on the Internet. DNS assumes IP addresses do not

change (are statically assigned rather than dynamically assigned). The DNS database resides

on a hierarchy of special database servers. When clients like Web browsers issue requests

involving Internet host names, 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. If the DNS server does not contain the needed mapping, it will in turn forward the

request to a different DNS server at the next higher level in the hierarchy. After potentially

several forwarding and delegation messages are sent within the DNS hierarchy, the IP

address for the given host eventually arrives at the resolver, that in turn completes the request

over Internet Protocol.

Algorithm

Server

Step1: Start the program.

Step2: Create the socket for the server.

Step3: Bind the socket to the port.

Page 45

Page 46: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Step4: Listen for the incoming client connection.

Step5: Receive the IP address from the client to be resolved.

Step6: Get the domain name for the client.

Step7: Check the existence of the domain in the server.

Step8: If domain matches then send the corresponding address to the client.

Step9: Stop the program execution.

Client

Step1: Start the Program.

Step2: Create the socket for the client.

Step3: Connect the socket to the Server.

Step4: Send the host name to the server to be resolved.

Step5: If the server corresponds then print the address and terminate the process.

Program:

Page 46

Page 47: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Exercise No : 18 Implementation of HTTPDate

Page 47

Page 48: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Exercise No : 19 Implementation of E-mailDate

Page 48

Page 49: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Exercise No : 20 Implementation of Multi-user chatDate

Page 49

Page 50: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Exercise No : 21 Program to create sockets for handling multiple

connectionsDate

Page 50

Page 51: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Exercise No : 22 Perform Socket ProgrammingDate

Page 51

Page 52: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Exercise No : 23 Program to trace the port of a particular hostDate

Page 52

Page 53: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Exercise No : 23 Ping ProgramDate

Page 53

Page 54: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Page 54

Page 55: Mc9247 Network Programming Lab Manual(1)

MC9247– NETWORK PROGRAMMING LAB

Page 55