Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Network Programming Lab, Study notes of Network Programming

manual programming

Typology: Study notes

2014/2015

Uploaded on 12/07/2015

YOGA_PRIYA.M
YOGA_PRIYA.M 🇮🇳

3.5

(4)

1 document

1 / 45

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Department of Information Technology
Faculty of Engineering And Technology
Lab Manual
SUBJECT CODE / TITLE: IT0421/ NETWORKING LAB
Prepared By
Mr. S.Muruganandam & Mr. A.R.Nagoor Meeran
Assistant professor(O.G), Assistant Professor(O.G),
Department of Information Technology
SRM University, SRM Nagar, Kattankulathur-603203
Kancheepuram District, Tamilnadu
Page | 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d

Partial preview of the text

Download Network Programming Lab and more Study notes Network Programming in PDF only on Docsity!

Department of Information Technology

Faculty of Engineering And Technology

Lab Manual

SUBJECT CODE / TITLE: IT0421/ NETWORKING LAB

Prepared By

Mr. S. Muruganandam & Mr. A.R.Nagoor Meeran

Assistant professor(O.G), Assistant Professor(O.G),

Department of Information Technology

SRM University, SRM Nagar, Kattankulathur-

Kancheepuram District, Tamilnadu

IT 0421 NETWORKING LAB 0 0 3 2

Co requisite

IT 0405 TCP/IP Technology

PURPOSE

The purpose of this course is to implement various protocols used in networking and analyze

them.

INSTRUCTIONAL OBJECTIVES

After successful completion of the course, the students should be able to

  • To implement File transfer protocols
  • To learn and implement RPCs
  • To implement DES and RSA encryption/decryption schemes
  • To implement packet capturing and analysis
  • To implement Chatting mechanism
  • To configure and implement firewalls.
  • To design and implant simple IDS

LIST OF EXPERIMENTS

1. Study of Header Files

2. Study of Basic Functions of Socket Programming

3. Simple Tcp/Ip Client Server Communication

4. Udp Echo Client Server Communication

5. Concurrent Tcp/Ip Day-Time Server

6. Half Duplex Chat Using Tcp/Ip

7. Full Duplex Chat Using Tcp/Ip

8. Implementation Of File Transfer Protocol

9. Remote Command Execution Using Udp

10. Arp Implementation Using Udp

TOTAL 60

Ex.No:

Date:

STUDY OF HEADER FILES

1. stdio.h: Has standard input and output library providing simple and efficient buffered stream IO interface. 2. unistd.h: It is a POSIX standard for open system interface. 3. string.h: This header file is used to perform string manipulation operations on NULL terminated strings. 4. stdlib.h: This header file contains the utility functions such as string conversion routines, memory allocation routines, random number generator, etc. 5. sys/types.h: Defines the data type of socket address structure in unsigned long. 6. sys/socket.h: The socket functions can be defined as taking pointers to the generic socket address structure called sockaddr. 7. netinet/in.h: Defines the IPv4 socket address structure commonly called Internet socket address structure called sockaddr_in. 8. netdb.h: Defines the structure hostent for using the system call gethostbyname to get the network host entry. 9. time.h: Has structures and functions to get the system date and time and to perform time manipulation functions. We use the function ctime(), that is defined in this header file , to calculate the current date and time. 10. sys/stat.h: Contains the structure stat to test a descriptor to see if it is of a specified type. Also it is used to display file or file system status.

  1. sys/ioctl.h : Macros and defines used in specifying an ioctl request are located in this header file. We use the function ioctl() that is defined in this header file. ioctl() function is used to perform ARP cache operations. 12. pcap.h: Has function definitions that are required for packet capturing. Some of the functions are pcap_lookupdev(),pcap_open_live() and pcap_loop(). pcap_lookupdev() is used to initialize the network device.The device to be sniffed is opened using the pcap_open_live(). Pcap_loop() determines the number of packets to be sniffed. 13. net/if_arp.h: Contains the definitions for Address Resolution Protocol. We use this to manipulate the ARP request structure and its data members arp_pa,arp_dev and arp_ha. The arp_ha structure’s data member sa_data[ ] has the hardware address. 14. errno.h: It sets an error number when an error and that error can be displayed using perror function. It has symbolic error names. The error number is never set to zero by any library function. 15. arpa/inet.h: This is used to convert internet addresses between ASCII strings and network byte ordered binary values (values that are stored in socket address structures). It is used for inet_aton, inet_addr, inet_ntoa functions.

¾ An out-of-band data transmission mechanism, may be supported. 2.SOCK_DGRAM: ¾ Supports datagram (connectionless, unreliable messages of a fixed maximum length). 3.SOCK_SEQPACKET: ¾ Provides a sequenced , reliable, two-way connection based data transmission path for datagrams of fixed maximum length. 4.SOCK_RAW: ¾ Provides raw network protocol access. 5.SOCK_RDM: ¾ Provides a reliable datagram layer that doesn’t guarantee ordering. 6.SOCK_PACKET: ¾ Obsolete and shouldn’t be used in new programs.

2.man connect:

NAME: connect – initiate a connection on a socket.

SYNOPSIS:

#include<sys/types.h> #include<sys/socket.h> int connect(int sockfd,const (struct sockaddr*)serv_addr,socklen_t addrlen);

DESCRIPTION:

¾ The file descriptor sockfd must refer to a socket. ¾ If the socket is of type SOCK_DGRAM then the serv_addr address is the address to which datagrams are sent by default and the only addr from which datagrams are received. ¾ If the socket is of type SOCK_STREAM or SOCK_SEQPACKET , this call attempts to make a connection to another socket.

RETURN VALUE :

¾ If the connection or binding succeeds, zero is returned. ¾ On error , -1 is returned , and error number is set appropriately.

ERRORS:

EBADF Not a valid Index. EFAULT The socket structure address is outside the user’s address space. ENOTSOCK Not associated with a socket. EISCONN Socket is already connected. ECONNREFUSED No one listening on the remote address.

3.man accept

NAME:

accept/reject job is sent to a destination.

SYNOPSIS:

accept destination(s) reject[-t] [-h server] [-r reason] destination(s)

DESCRIPTION:

¾ accept instructs the printing system to accept print jobs to the specified destination. ¾ The –r option sets the reason for rejecting print jobs. ¾ The –e option forces encryption when connecting to the server.

4.man send

NAME:

send, sendto, sendmsg - send a message from a socket.

SYNOPSIS:

#include<sys/types.h> #include<sys/socket.h>

ssize_t send(int s, const void *buf, size_t len, int flags); ssize_t sendto(int s, const void buf, size_t len, int flags, const struct sock_addrto, socklen_t tolen); ssize_t sendmsg(int s, const struct msghdr *msg, int flags);

DESCRIPTION:

¾ The system calls send, sendto and sendmsg are used to transmit a message to another socket. ¾ The send call may be used only when the socket is in a connected state. ¾ The only difference between send and write is the presence of flags. ¾ The parameter is the file descriptor of the sending socket.

5.man recv

NAME:

recv, recvfrom, recvmsg – receive a message from a socket.

SYNOPSIS:

#include<sys/types.h> #include<sys/socket.h> ssize_t recv(int s, void *buf, size_t len, int flags);

ifconfig interface[aftype] options | address……

DESCRIPTION:

¾ ifconfig is used to configure the kernel resident network interfaces. ¾ It is used at boot time to setup interfaces as necessary. ¾ After that, it is usually only needed when debugging or when system tuning is needed. ¾ If no arguments are given, ifconfig displays the status of the currently active interfaces.

9. man bind

SYNOPSIS:

bind[-m keymap] [-lp sv psv]

10. man htons/ man htonl

NAME:

htonl, htons, ntohl, ntohs - convert values between host and network byte order.

SYNOPSIS:

#include<netinet/in.h> uint32_t htonl(uint32_t hostlong); uint16_t htons(uint32_t hostshort); uint32_t ntohl(uint32_t netlong); uint16_t ntohs(uint16_t netshort);

DESCRIPTION:

¾ The htonl() function converts the unsigned integer hostlong from host byte order to network byte order. ¾ The htons() converts the unsigned short integer hostshort from host byte order to network byte order. ¾ The ntohl() converts the unsigned integer netlong from network byte order to host byte order.

11. man gethostname

NAME:

gethostname, sethostname- get/set host name.

SYNOPSIS:

#include<unistd.h> int gethostname(char *name,size_t len); int sethostname(const char *name,size_t len);

DESCRIPTION:

¾ These functions are used to access or to change the host name of the current processor. ¾ The gethostname() returns a NULL terminated hostname(set earlier by sethostname()) in the array name that has a length of len bytes. ¾ In case the NULL terminated then hostname does not fit ,no error is returned, but the hostname is truncated. ¾ It is unspecified whether the truncated hostname will be NULL terminated.

12. man gethostbyname

NAME:

gethostbyname, gethostbyaddr, sethostent, endhostent, herror, hstr – error – get network host entry.

SYNOPSIS:

#include<netdb.h> extern int h_errno; struct hostent *gethostbyname(const char *name); #include<sys/socket.h> struct hostent *gethostbyaddr(const char *addr)int len, int type); struct hostent *gethostbyname2(const char *name,int af);

DESCRIPTION:

¾ The gethostbyname() returns a structure of type hostent for the given hostname. ¾ Name->hostname or IPV4/IPV6 with dot notation. ¾ gethostbyaddr()- struct of type hostent / host address length ¾ Address types- AF_INET, AF_INET6. ¾ sethostent() – stay open is true(1). ¾ TCP socket connection should be open during queries. ¾ Server queries for UDP datagrams. ¾ endhostent()- ends the use of TCP connection. ¾ Members of hostent structure: a) h_name b) h_aliases c) h_addrtype d) h_length e) h_addr-list f) h_addr.

RESULT :

Thus the basic functions used for Socket Programming was studied successfully.

int main(int asrgc,char*argv[]) { int bd,sd,ad; char buff[1024]; struct sockaddr_in cliaddr,servaddr; socklen_t clilen; clilen=sizeof(cliaddr); bzero(&servaddr,sizeof(servaddr));

/Socket address structure/** servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(1999);

/TCP socket is created, an Internet socket address structure is filled with wildcard address & server’s well known port/** sd=socket(AF_INET,SOCK_STREAM,0);

/Bind function assigns a local protocol address to the socket/** bd=bind(sd,(struct sockaddr*)&servaddr,sizeof(servaddr));

/Listen function specifies the maximum number of connections that kernel should queue for this socket/** listen(sd,5); printf("Server is running….\n");

/The server to return the next completed connection from the front of the completed connection Queue calls it/** ad=accept(sd,(struct sockaddr)&cliaddr,&clilen); while(1) { bzero(&buff,sizeof(buff)); /Receiving the request message from the client/* recv(ad,buff,sizeof(buff),0); printf("Message received is %s\n",buff); } } Client: tcpclient.c #include<stdio.h> #include<string.h> #include<sys/socket.h> #include<sys/types.h> #include<unistd.h> #include<netinet/in.h> #include<netdb.h> #include<arpa/inet.h> int main(int argc,char * argv[]) { int cd,sd,ad; char buff[1024]; struct sockaddr_in cliaddr,servaddr;

struct hostent h; /This function looks up a hostname and it returns a pointer to a hostent structure that contains all the IPV4 address/* h=gethostbyname(argv[1]); bzero(&servaddr,sizeof(servaddr));

/Socket address structure/** servaddr.sin_family=AF_INET; memcpy((char *)&servaddr.sin_addr.s_addr,h->h_addr_list[0],h->h_length); servaddr.sin_port = htons(1999);

/Creating a socket, assigning IP address and port number for that socket/** sd = socket(AF_INET,SOCK_STREAM,0);

/Connect establishes connection with the server using server IP address/** cd=connect(sd,(struct sockaddr*)&servaddr,sizeof(servaddr)); while(1) { printf("Enter the message: \n");

/Reads the message from standard input/** fgets(buff,100,stdin);

/Send function is used on client side to send data given by user on client side to the server/** send(sd,buff,sizeof(buff)+1,0); printf("\n Data Sent "); //recv(sd,buff,strlen(buff)+1,0); printf("%s",buff); } } SAMPLE OUTPUT:

Server : (Host Name:Root1) [root@localhost 4ita33]# vi tcpserver.c [root@localhost 4ita33]# cc tcpserver.c [root@localhost 4ita33]# ./a.out Server is running…. Message received is hi

Message received is hi

Client: (Host Name:Root2)

[root@localhost 4ita33]# vi tcpclient.c [root@localhost 4ita33]# cc tcpclient.c [root@localhost 4ita33]# ./a.out 127.0.0. Enter the message:

Ex No: 4

Date:

UDP ECHO CLIENT SERVER COMMUNICATION

GIVEN REQUIREMENTS:

There are two hosts, Client and Server. The Client accepts the message from the user and sends it to the Server. The Server receives the message, prints it and echoes the message back to the Client.

TECHNICAL OBJECTIVE:

To implement an UDP Echo Client-Server application , where the Client on establishing a connection with the Server, sends a string to the Server. The Server reads the String, prints it and echoes it back to the Client.

METHODOLOGY:

Server: ¾ Include the necessary header files. ¾ Create a socket using socket function with family AF_INET, type as SOCK_DGRAM. ¾ Initialize server address to 0 using the bzero function. ¾ Assign the sin_family to AF_INET, sin_addr to INADDR_ANY, sin_port to SERVER_PORT, a macro defined port number. ¾ Bind the local host address to socket using the bind function. ¾ Within an infinite loop, receive message from the client using recvfrom function, print it on the console and send (echo) the message back to the client using sendto function. Client: ¾ Include the necessary header files. ¾ Create a socket using socket function with family AF_INET, type as SOCK_DGRAM. ¾ Initialize server address to 0 using the bzero function. ¾ Assign the sin_family to AF_INET. ¾ Get the server IP address from the console. ¾ Using gethostbyname function assign it to a hostent structure, and assign it to sin_addr of the server address structure. ¾ Within an infinite loop, read message from the console and send the message to the server using the sendto function. ¾ Receive the echo message using the recvfrom function and print it on the console.

CODING: Server: udpserver.c

#include<sys/socket.h> #include<stdio.h> #include<unistd.h> #include<string.h> #include<netinet/in.h> #include<netdb.h>

#include<arpa/inet.h> #include<sys/types.h> int main(int argc,char *argv[]) { int sd; char buff[1024]; struct sockaddr_in cliaddr,servaddr; socklen_t clilen; clilen=sizeof(cliaddr);

/UDP socket is created, an Internet socket address structure is filled with wildcard address & server’s well known port/** sd=socket(AF_INET,SOCK_DGRAM,0); if (sd<0) { perror ("Cannot open Socket"); exit(1); } bzero(&servaddr,sizeof(servaddr));

/Socket address structure/** servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(5669);

/Bind function assigns a local protocol address to the socket/** if(bind(sd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0) { perror("error in binding the port"); exit(1); } printf("%s","Server is Running…\n");

while(1) { bzero(&buff,sizeof(buff));

/Read the message from the client/** if(recvfrom(sd,buff,sizeof(buff),0,(struct sockaddr*)&cliaddr,&clilen)<0) { perror("Cannot rec data"); exit(1); } printf("Message is received \n",buff);

/Sendto function is used to echo the message from server to client side/** if(sendto(sd,buff,sizeof(buff),0,(struct sockadddr*)&cliaddr,clilen)<0) {

perror("Cannot send data"); exit(1);

} printf("Data sent to UDP Server:%s",buff); bzero(buff,sizeof(buff));

/Receiving the echoed message from server/** if(recvfrom (sd,buff,sizeof(buff),0,(struct sockaddr*)&servaddr,&len)<0) { perror("Cannot receive data"); exit(1); } printf("Received Data from server: %s",buff); } close(sd); return 0; }

SAMPLE OUTPUT:

Server : (Host Name:Root1) [root@localhost 4ita33]# vi udpserver.c [root@localhost 4ita33]# cc udpserver.c [root@localhost 4ita33]# ./a.out Server is Running…

Message is received Send data to UDP Client: hi

Message is received Send data to UDP Client: how are u

Client: (Host Name:Root2)

[root@localhost 4ita33]# vi udpclient.c [root@localhost 4ita33]# cc udpclient.c [root@localhost 4ita33]# ./a.out 127.0.0. Enter input data : hi Data sent to UDP Server:hi Received Data from server: hi

Enter input data : how are u Data sent to UDP Server:how are u Received Data from server: how are u

Enter input data :

INFERENCE:

Thus, the UDP ECHO client server communication is established by sending the message from the client to the server and server prints it and echoes the message back to the client.