You are on page 1of 45

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

IT 0421 NETWORKING LAB 0032 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

Page | 2

IT0421 NETWORKING LAB Sl.No 1 2 3 4 5 6 7 8 9 10 Table of contents Study of Header Files Study of Basic Functions of Socket Programming Simple Tcp/Ip Client Server Communication UDP Echo Client Server Communication Concurrent TCP/IP Day-Time Server Half Duplex Chat Using TCP/IP Full Duplex Chat Using TCP/IP Implementation Of File Transfer Protocol Remote Command Execution Using UDP ARP Implementation Using UDP Page No. 4 6 12 16 21 25 30 35 39 43

Page | 3

Ex.No:1 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.

Page | 4

h: Has function definitions that are required for packet capturing. Page | 5 .h: Contains the definitions for Address Resolution Protocol. sys/ioctl. We use the function ioctl() that is defined in this header file. Some of the functions are pcap_lookupdev(). net/if_arp. 14. 12. 15.h: Macros and defines used in specifying an ioctl request are located in this header file. inet_ntoa functions. arpa/inet.h: It sets an error number when an error and that error can be displayed using perror function. pcap_lookupdev() is used to initialize the network device. The error number is never set to zero by any library function.arp_dev and arp_ha. ioctl() function is used to perform ARP cache operations. It has symbolic error names.The device to be sniffed is opened using the pcap_open_live().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). inet_addr. We use this to manipulate the ARP request structure and its data members arp_pa. Pcap_loop() determines the number of packets to be sniffed. It is used for inet_aton. errno. 13.11. pcap.pcap_open_live() and pcap_loop(). The arp_ha structure’s data member sa_data[ ] has the hardware address.

The domain parameter specifies a common domain this selects the protocol family which will be used for communication.Ex No: 2 Date: STUDY OF BASIC FUNCTIONS OF SOCKET PROGRAMMING AIM: To discuss some of the basic functions used for socket programming.h>. connection based byte streams. FORMAT: NAME PF_UNIX. SYNOPSIS: #include<sys/types. TYPES: 1.SOCK_STREAM: Provides sequenced . two-way . 1. The socket has the indicated type. Apple Talk. IPV4 Internet Protocols.man socket NAME: Socket – create an endpoint for communication.h> int socket(int domain. DESCRIPTION: Socket creates an endpoint for communication and returns a descriptor.h> #include<sys/socket.PF_LOCAL PF_INET PF_IPX PF_APPLETALK PURPOSE Local Communication. reliable.int protocol). IPX-Novell Protocols. which specifies the communication semantics.int type. Page | 6 . These families are defined in <sys/socket.

The socket structure address is outside the user’s address space.man connect: NAME: connect – initiate a connection on a socket. ERRORS: EBADF EFAULT ENOTSOCK EISCONN ECONNREFUSED 3. unreliable messages of a fixed maximum length).SOCK_PACKET: Obsolete and shouldn’t be used in new programs. zero is returned. If the socket is of type SOCK_STREAM or SOCK_SEQPACKET . may be supported.man accept Not a valid Index. On error . 2.h> int connect(int sockfd.SOCK_DGRAM: Supports datagram (connectionless.h> #include<sys/socket.socklen_t addrlen). No one listening on the remote address. 2. and error number is set appropriately.SOCK_RAW: Provides raw network protocol access. 5. SYNOPSIS: #include<sys/types. two-way connection based data transmission path for datagrams of fixed maximum length. Page | 7 . DESCRIPTION: The file descriptor sockfd must refer to a socket. 6. RETURN VALUE: If the connection or binding succeeds. An out-of-band data transmission mechanism. Not associated with a socket. -1 is returned . reliable. Socket is already connected. 3. 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.const (struct sockaddr*)serv_addr. this call attempts to make a connection to another socket.SOCK_RDM: Provides a reliable datagram layer that doesn’t guarantee ordering.SOCK_SEQPACKET: Provides a sequenced . 4.

sendto. The send call may be used only when the socket is in a connected state. void *buf. ssize_t sendto(int s. size_t len. The –r option sets the reason for rejecting print jobs. recvmsg – receive a message from a socket. 5. int flags).NAME: accept/reject job is sent to a destination. DESCRIPTION: The system calls send. The –e option forces encryption when connecting to the server. Page | 8 . size_t len. sendmsg . size_t len. SYNOPSIS: #include<sys/types. sendto and sendmsg are used to transmit a message to another socket. socklen_t tolen). int flags). int flags). int flags. The parameter is the file descriptor of the sending socket. 4. ssize_t sendmsg(int s.man recv NAME: recv. const void *buf. The only difference between send and write is the presence of flags. SYNOPSIS: #include<sys/types. const struct sock_addr*to.h> ssize_t recv(int s. recvfrom. const void *buf.h> ssize_t send(int s. 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. const struct msghdr *msg.send a message from a socket.man send NAME: send.h> #include<sys/socket.h> #include<sys/socket.

size_t len. and may be used to recv data on a socket whether or not it is connection oriented. 8. struct msghdr *msg. DESCRIPTION: The recvfrom and recvmsg calls are used to receive messages from a socket. ssize_t recvmsg(int s. SYNOPSIS: ifconfig[interface] Page | 9 .configure a network interface.man write NAME: write. this src addr is filled in. If from is not NULL. ifconfig NAME: ifconfig. int flags. 6. int flags).ssize_t recvfrom(int s. return 7. readonly. The recv call is normally used only on a connection socket and is identical to recvfrom with a NULL from parameter. If the other user wants to reply they must run write as well. struct sockaddr *from. void *buf. socklen_t* from len). SYNOPSIS: write user[ttyname] DESCRIPTION: write allows you to communicate with other users.send a message to another user. and the underlying protocol provides the src addr .man read NAME: read. by copying lines from terminal to ……… When you run the write and the user you are writing to get a message of the form: Message from yourname @yourhost on yourtty at hh:mm:… Any further lines you enter will be copied to the specified user’s terminal.

uint16_t htons(uint32_t hostshort). uint16_t ntohs(uint16_t netshort).h> int gethostname(char *name. ifconfig displays the status of the currently active interfaces. The htons() converts the unsigned short integer hostshort from host byte order to network byte order. int sethostname(const char *name. DESCRIPTION: The htonl() function converts the unsigned integer hostlong from host byte order to network byte order. SYNOPSIS: #include<netinet/in. man bind SYNOPSIS: bind[-m keymap] [-lp sv psv] 10. ntohl.convert values between host and network byte order. DESCRIPTION: ifconfig is used to configure the kernel resident network interfaces. sethostname. ntohs .get/set host name. SYNOPSIS: #include<unistd.h> uint32_t htonl(uint32_t hostlong). If no arguments are given. man gethostname NAME: gethostname. man htons/ man htonl NAME: htonl. htons. uint32_t ntohl(uint32_t netlong). After that. Page | 10 .ifconfig interface[aftype] options | address…… DESCRIPTION: 9. It is used at boot time to setup interfaces as necessary.size_t len).size_t len). it is usually only needed when debugging or when system tuning is needed. 11. The ntohl() converts the unsigned integer netlong from network byte order to host byte order.

herror.h> extern int h_errno. AF_INET6. gethostbyaddr. but the hostname is truncated. Server queries for UDP datagrams. struct hostent *gethostbyname(const char *name). SYNOPSIS: #include<netdb.int af).h> struct hostent *gethostbyaddr(const char *addr)int len. DESCRIPTION: The gethostbyname() returns a structure of type hostent for the given hostname. TCP socket connection should be open during queries. In case the NULL terminated then hostname does not fit . It is unspecified whether the truncated hostname will be NULL terminated.struct of type hostent / host address length Address types. man gethostbyname NAME: gethostbyname.ends the use of TCP connection. These functions are used to access or to change the host name of the current processor.AF_INET. endhostent(). hstr – error – get network host entry. #include<sys/socket. int type). gethostbyaddr(). sethostent. Members of hostent structure: a) h_name b) h_aliases c) h_addrtype d) h_length e) h_addr-list f) h_addr. endhostent. 12. struct hostent *gethostbyname2(const char *name.no error is returned. RESULT: Thus the basic functions used for Socket Programming was studied successfully. Page | 11 . Name->hostname or IPV4/IPV6 with dot notation. sethostent() – stay open is true(1). The gethostname() returns a NULL terminated hostname(set earlier by sethostname()) in the array name that has a length of len bytes.

The Server reads the String and prints it. Create a socket using socket function with family AF_INET. Within an infinite loop. Create a socket using socket function with family AF_INET. Assign the sin_family to AF_INET.h> #include<sys/socket. Initialize server address to 0 using the bzero function.h> #include<netinet/in. and assign it to sin_addr of the server address structure. Assign the sin_family to AF_INET. The Server receives the message and prints it. Within an infinite loop.h> #include<netdb. sends a string to the Server.c #include<sys/types. The Client accepts the message from the user and sends it to the Server. type as SOCK_STREAM. Request a connection from the server using the connect function. Listen on the socket for connection request from the client. using the recv function receive message from the client and print it on the console. Get the server IP address and port number from the console. where the Client on establishing a connection with the Server. Client and Server. Bind the local host address to socket using the bind function. sin_addr to INADDR_ANY. sin_port to a dynamically assigned port number.Ex No: 3 Date: SIMPLE TCP/IP CLIENT SERVER COMMUNICATION GIVEN REQUIREMENTS: There are two hosts. type as SOCK_STREAM. Client: Include the necessary header files.h> #include<arpa/inet. Accept connection request from the client using accept function.h> #include<string. Using gethostbyname function assign it to a hostent structure. METHODOLOGY: Server: Include the necessary header files. TECHNICAL OBJECTIVE: To implement a simple TCP Client-Server application .h> Page | 12 . read message from the console and send the message to the server using the send function. CODING: Server: tcpserver. Initialize server address to 0 using the bzero function.

bzero(&servaddr. char buff[1024].buff). servaddr. char buff[1024].h> #include<netdb.sin_addr.&clilen). } } Client: tcpclient.c #include<stdio.char * argv[]) { int cd. /*Listen function specifies the maximum number of connections that kernel should queue for this socket*/ listen(sd.int main(int asrgc.h> int main(int argc.5).(struct sockaddr*)&servaddr. /*Receiving the request message from the client*/ recv(ad.0). while(1) { bzero(&buff. struct sockaddr_in cliaddr. clilen=sizeof(cliaddr).sizeof(buff)).s_addr=htonl(INADDR_ANY). printf("Server is running….0).sd.\n"). /*Socket address structure*/ servaddr.buff.servaddr.char*argv[]) { int bd. printf("Message received is %s\n".h> #include<netinet/in.h> #include<string. /*TCP socket is created.(struct sockaddr*)&cliaddr.sizeof(servaddr)). /*Bind function assigns a local protocol address to the socket*/ bd=bind(sd.sizeof(servaddr)).ad. Page | 13 . struct sockaddr_in cliaddr.h> #include<arpa/inet.sin_port=htons(1999).h> #include<sys/types. servaddr. an Internet socket address structure is filled with wildcard address & server’s well known port*/ sd=socket(AF_INET.sin_family=AF_INET.servaddr.sd.ad.h> #include<unistd.sizeof(buff).h> #include<sys/socket. socklen_t clilen. /*The server to return the next completed connection from the front of the completed connection Queue calls it*/ ad=accept(sd.SOCK_STREAM.

/*Creating a socket.c [root@localhost 4ita33]# .sin_addr. /*Reads the message from standard input*/ fgets(buff. printf("\n Data Sent "). /*Send function is used on client side to send data given by user on client side to the server*/ send(sd.0.0.h->h_addr_list[0]. assigning IP address and port number for that socket*/ sd = socket(AF_INET. } } SAMPLE OUTPUT: Server: (Host Name:Root1) [root@localhost 4ita33]# vi tcpserver. memcpy((char *)&servaddr.h->h_length).c [root@localhost 4ita33]# cc tcpserver.sizeof(servaddr))./a.sin_family=AF_INET. Message received is hi Message received is hi Client: (Host Name:Root2) [root@localhost 4ita33]# vi tcpclient.stdin).0). /*Connect establishes connection with the server using server IP address*/ cd=connect(sd.0).buff.(struct sockaddr*)&servaddr. printf("%s".out Server is running….buff.sizeof(buff)+1. /*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]). /*Socket address structure*/ servaddr./a. while(1) { printf("Enter the message: \n").buff).sizeof(servaddr)). //recv(sd.0).sin_port = htons(1999). servaddr. bzero(&servaddr.out 127.1 Enter the message: Page | 14 .strlen(buff)+1.s_addr.struct hostent *h.100.c [root@localhost 4ita33]# .SOCK_STREAM.c [root@localhost 4ita33]# cc tcpclient.

hi Data Sent hi Enter the message: how r u Data Sent how r u Enter the message: INFERENCE: Thus. Page | 15 . a program to perform simple communication between client and server using TCP/IP was implemented.

Ex No: 4 Date: UDP ECHO CLIENT SERVER COMMUNICATION GIVEN REQUIREMENTS: There are two hosts. Assign the sin_family to AF_INET. Get the server IP address from the console. read message from the console and send the message to the server using the sendto function. type as SOCK_DGRAM.h> #include<netinet/in.h> #include<string. Within an infinite loop. TECHNICAL OBJECTIVE: To implement an UDP Echo Client-Server application . The Client accepts the message from the user and sends it to the Server. CODING: Server: udpserver. Using gethostbyname function assign it to a hostent structure. Client: Include the necessary header files. print it on the console and send (echo) the message back to the client using sendto function. a macro defined port number. sin_port to SERVER_PORT. Client and Server. Initialize server address to 0 using the bzero function. Assign the sin_family to AF_INET. prints it and echoes the message back to the Client. sends a string to the Server. Bind the local host address to socket using the bind function. METHODOLOGY: Server: Include the necessary header files.h> #include<unistd. Create a socket using socket function with family AF_INET.h> Page | 16 . Initialize server address to 0 using the bzero function. The Server reads the String. Create a socket using socket function with family AF_INET.h> #include<stdio. Within an infinite loop. Receive the echo message using the recvfrom function and print it on the console. prints it and echoes it back to the Client. and assign it to sin_addr of the server address structure.h> #include<netdb.c #include<sys/socket. where the Client on establishing a connection with the Server. type as SOCK_DGRAM. receive message from the client using recvfrom function. The Server receives the message. sin_addr to INADDR_ANY.

exit(1). if (sd<0) { perror ("Cannot open Socket").buff).buff. exit(1).h> int main(int argc.char *argv[]) { int sd. } bzero(&servaddr. /*Bind function assigns a local protocol address to the socket*/ if(bind(sd.sin_family=AF_INET.sizeof(servaddr)). wildcard while(1) { bzero(&buff.buff. /*Read the message from the client*/ if(recvfrom(sd. servaddr. clilen=sizeof(cliaddr).clilen)<0) { Page | 17 .SOCK_DGRAM.0.sin_addr.#include<arpa/inet. /*Socket address structure*/ servaddr. struct sockaddr_in cliaddr.0. socklen_t clilen.servaddr. exit(1).sin_port=htons(5669). servaddr.sizeof(buff)."Server is Running…\n"). an Internet socket address structure is filled with address & server’s well known port*/ sd=socket(AF_INET.sizeof(buff)). } printf("Message is received \n".&clilen)<0) { perror("Cannot rec data").(struct sockadddr*)&cliaddr.s_addr=htonl(INADDR_ANY).sizeof(buff). /*Sendto function is used to echo the message from server to client side*/ if(sendto(sd. char buff[1024].h> #include<sys/types.0).(struct sockaddr*)&cliaddr.(struct sockaddr*)&servaddr.sizeof(servaddr))<0) { perror("error in binding the port"). /*UDP socket is created. } printf("%s".

sin_addr.sizeof (buff). exit(1).c #include<sys/types.h> #include<unistd.sin_family=AF_INET. /*UDP socket is created. /*sendto is used to transmit the request message to the server*/ if(sendto (sd. exit(1).s_addr=htonl(INADDR_ANY).h> #include<string.h> #include<sys/socket.buff. bzero(buff.len). return 0. socklen_t len.SOCK_DGRAM. char buff[1024]. } close(sd). } bzero(&servaddr. an Internet socket address structure is filled with wildcard address & server’s well known port*/ sd = socket(AF_INET.0).(struct sockaddr*)&servaddr.stdin).0.sizeof(buff)).h> int main(int argc. /*Socket address structure*/ servaddr. servaddr.perror("Cannot send data to client"). } Client: udpclient.h> #include<stdio.sin_port=htons(5669).sizeof (buff). struct sockaddr_in servaddr.h> #include<netinet/in. } printf("Send data to UDP Client: %s". while(1) { printf("Enter Input data : \n"). servaddr. if(sd<0) { perror("Cannot open socket").char*argv[]) { int sd. /*Reads the message from standard input*/ fgets(buff.len)<0) Page | 18 .buff).h> #include<netdb. len=sizeof(servaddr).

c [root@localhost 4ita33]# . } close(sd).c [root@localhost 4ita33]# cc udpclient. exit(1). } SAMPLE OUTPUT: Server: (Host Name:Root1) [root@localhost 4ita33]# vi udpserver. } printf("Data sent to UDP Server:%s".(struct sockaddr*)&servaddr.buff).sizeof(buff)).{ perror("Cannot send data").0.buff). /*Receiving the echoed message from server*/ if(recvfrom (sd. } printf("Received Data from server: %s".0.sizeof(buff).buff.&len)<0) { perror("Cannot receive data")./a.c [root@localhost 4ita33]# . bzero(buff./a.out 127.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. return 0.1 Enter input data : hi Data sent to UDP Server:hi Received Data from server: hi Page | 19 .0. exit(1).c [root@localhost 4ita33]# cc udpserver.

Page | 20 . 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.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.

Use the fork system call to spawn the processes. Client: Include the necessary header files. The Client requests the concurrent server for the date and time. CODING: Server: dtserver. Get the server IP address from the console. TECHNICAL OBJECTIVE: To implement a TCP/IP day time server (concurrent server) that handles multiple client requests. sin_port to statically assigned port number. Request a connection from the server using the connect function. Using gethostbyname function assign it to a hostent structure. type as SOCK_STREAM. Client and Server.h> #include<sys/types. sin_addr to INADDR_ANY. Assign the sin_family to AF_INET. type as SOCK_STREAM. Initialize server address to 0 using the bzero function.Ex No:5 Date: CONCURRENT TCP/IP DAY-TIME SERVER GIVEN REQUIREMENTS: There are two hosts. accept connection request from the client using accept function. Bind the local host address to socket using the bind function. Create a socket using socket function with family AF_INET. The Server sends the date and time. the server sends its day-time details to the client which the client prints in its console. Once the client establishes connection with the server. receive the date and time from the server using the read function and print the date and time on the console.h> #include<stdio. Create a socket using socket function with family AF_INET.h> #include<sys/socket. and assign it to sin_addr of the server address structure. Within a for loop. Calculate the current date and time using the ctime() function. Initialize server address to 0 using the bzero function. which the Client accepts and prints.c #include<time. METHODOLOGY: Server: Include the necessary header files. Within an infinite loop. Assign the sin_family to AF_INET.h> #include<unistd.h> Page | 21 . Change the format so that it is appropriate for human readable form and send the date and time to the client using the write function.

cliaddr.sizeof(buff)).s_addr=htonl(INADDR_ANY). /*TCP socket is created. } Client: dtclient.h> 0int main(int argc.SOCK_STREAM.sizeof(servaddr)). send(ad. servaddr.sizeof(buff).sizeof(buff).NULL).ctime(&t1)). /*Prints the converted string format*/ snprintf(buff. while(1) { bzero(&buff.(struct sockaddr*)&servaddr.sin_family=AF_INET. printf("Server is running…\n").buff."%24s\r\n". struct sockaddr_in servaddr.h> #include<netdb.ad. bzero(&servaddr. servaddr. /*The server to return the next completed connection from the front of the completed connection Queue calls it*/ ad=accept(sd. /*Bind function assigns a local protocol address to the socket*/ bind(sd.char *argv[]) { int sd.c #include<stdio.h> #include<netinet/in.0). /*Listen function specifies the maximum number of connections that kernel should queue for this socket*/ listen(sd. char buff[1024].sin_port=htons(1507).(struct sockaddr *)NULL. time_t t1.5). /*Library function time returns the Coordinated Universal Time*/ t1=time(NULL).#include<string. //socklen_t clilen=sizeof(cliaddr).h> Page | 22 . /*Socket address structure*/ servaddr.0).sizeof(servaddr)). an Internet socket address structure is filled with wildcard address & server’s well known port*/ sd=socket(AF_INET.sin_addr.

an Internet socket address structure is filled with wildcard address & server’s well known port*/ sd=socket(AF_INET.h> #include<netinet/in. recv(sd.c [root@localhost 4ita33]# cc dtserver.c [root@localhost 4ita33]# .h> #include<time. struct sockaddr_in cliaddr.SOCK_STREAM. bzero(&servaddr. /*Socket address structure*/ servaddr. memcpy((char*)&servaddr.servaddr.(struct sockaddr*)&servaddr.buff. /*Connect establishes connection with the server using server IP address*/ connect(sd.sizeof(buff).out Server is running… Client: (Host Name:Root2) [root@localhost 4ita33]# vi dtclient. struct hostent *h.s_addr. /*TCP socket is created.h> int main(int argc.c Page | 23 .sizeof(servaddr)). } SAMPLE OUTPUT: Server: (Host Name:Root1) [root@localhost 4ita33]# vi dtserver.sin_addr./a.h> #include<unistd.h> #include<sys/socket.0).0).sizeof(servaddr)).h->h_addr_list[0].h->h_length).sin_port=htons(1507).buff).#include<sys/types. char buff[1024].ad. printf("Day time of server is: %s\n". servaddr.h> #include<netdb.sin_family=AF_INET.char *argv[]) { int sd. h=gethostbyname(argv[1]).

c [root@localhost 4ita33]# .server communication is established by sending the request message from the client to the concurrent server and the server sends its time to all the clients and displays it./a.0.out 127.1 Day time of server is: Sat Oct 27 18:02:21 2007 INFERENCE: Thus the concurrent daytime client. Page | 24 .[root@localhost 4ita33]# cc dtclient.0.

Client and Server. Using gethostbyname function assign it to a hostent structure.h> #include<stdio. There is only a single way communication between them. Assign the sin_family to AF_INET. Bind the local host address to socket using the bind function. Get the server IP address and the Port number from the console. type as SOCK_STREAM. Request a connection from the server using the connect function. and assign it to sin_addr of the server address structure. METHODOLOGY: Server: Include the necessary header files. Read message from the console and send it to the server. Client: Include the necessary header files.h> #include<sys/socket. Create a socket using socket function with family AF_INET. Listen on the socket for connection request from the client. The Client can send and the server well receive messages at the same time. Initialize server address to 0 using the bzero function. CODING: Server: hserver. Create a socket using socket function with family AF_INET. sin_addr to INADDR_ANY. sin_port to dynamically assigned port number.h> #include<arpa/inet. they send messages or receive message from the other. type as SOCK_STREAM.h> #include<unistd. Fork the process to receive message from the client and print it on the console.c #include<sys/types. where the Client establishes a connection with the Server. Assign the sin_family to AF_INET.e.h> Page | 25 .h> #include<netdb. TECHNICAL OBJECTIVE: To implement a half duplex application.Ex No:6 Date: HALF DUPLEX CHAT USING TCP/IP GIVEN REQUIREMENTS: There are two hosts. Fork the process to receive message from the server and print it on the console.h> #include<netinet/in. Both the Client and the Server exchange message i. Read message from the console and send it to the client. Initialize server address to 0 using the bzero function. Accept connection request from the Client using accept function.

sin_family=AF_INET.sd."Enter the input data:"). printf("%s\n".&clilen). /*Socket address structure*/ servaddr. while(n==1) { bzero(&buff1.buff1. /*Sends the message to client*/ send(ad. n=1.sin_port=htons(5000).(struct sockaddr*)&cliaddr.sizeof(buff).sin_addr.buff1[10000].sizeof(buff)). while(1) { bzero(&buff.s_addr=htonl(INADDR_ANY).0). socklen_t clilen.cliaddr.sizeof(servaddr)).sizeof(buff1)).sizeof(servaddr)). } } Page | 26 . n=n+1. /*The server to return the next completed connection from the front of the completed connection Queue calls it*/ ad=accept(sd.int main(int argc. servaddr.strlen(buff1)+1.buff). bzero(&servaddr. /*Listen function specifies the maximum number of connections that kernel should queue for this socket*/ listen(sd.0)."Data sent")."server is running…"). printf("%s\n".10000.5).buff. char buff[10000]. printf("Receive from the client:%s\n". struct sockaddr_in servaddr. /*Bind function assigns a local protocol address to the socket*/ bind(sd.char *argv[]) { int n.stdin). /*Receiving the request from client*/ recv(ad.SOCK_STREAM. printf("%s\n".ad. /*Read the message from client*/ fgets(buff1. an Internet socket address structure is filled with wildcard address & server’s well known port*/ sd=socket(AF_INET. /*TCP socket is created.servlen. servaddr.(struct sockaddr*)&servaddr.0).

/*Socket address structure*/ servaddr.cd.(struct sockaddr*)&servaddr.sin_port=htons(5000).cliaddr. /*Send the message to server*/ send(sd.sizeof(buff1))."Data sent"). servaddr. /*This function is used to read from server*/ fgets(buff.0).sin_family=AF_INET. assigning IP address and port number for that socket*/ sd=socket(AF_INET. bzero(&servaddr. while(1) { bzero(&buff.char *argv[]) { int n. char buff[10000].0).stdin).buff1[10000]. printf("%s\n".10000. while(n==1) { bzero(&buff1.sd.h> #include<sys/socket.h> #include<netdb."Enter the input data:"). servaddr. printf("%s\n".c #include<sys/types. /*Creating a socket.sizeof(servaddr)).SOCK_STREAM. } Client: hclient. n=1.sizeof(servaddr)).s_addr=inet_addr(argv[1]). Page | 27 .return 0.h> #include<netinet/in. struct sockaddr_in servaddr.sizeof(buff)). socklen_t servlen.buff.clilen.sin_addr. /*Connect establishes connection with the server using server IP address*/ cd=connect(sd.h> #include<stdio.h> #include<unistd.h> int main(int argc.strlen(buff)+1.h> #include<arpa/inet.

buff1).c [root@localhost 4ita33]# . } SAMPLE OUTPUT: Server: (Host Name:Root1) [root@localhost 4ita33]# vi hserver.sizeof(buff1)./a./*Receive the message from server*/ recv(sd. } } return 0.buff1.0). n=n+1.c [root@localhost 4ita33]# cc hserver. printf("Received from the server:%s\n".out Server is running… Receive from the client:hi Enter the input data: Page | 28 .

Data sent INFERENCE: Thus the chat application full duplex communication is established by sending the request from the client to the server.1 Enter the input data: hi Data sent: Received from the server:how are u da .0. Data sent Receive from the client:me fine da . Enter the input data: me fine da .c [root@localhost 4ita33]# cc hclient.c [root@localhost 4ita33]# ..out 127...how are u da ...0./a. Enter the input data: Client: (Host Name:Root2) [root@localhost 4ita33]# vi hclient. server gets the message and gives response to the client and prints it. Page | 29 ..

Both the Client and the Server exchange message i. Using gethostbyname function assign it to a hostent structure. Both the Client and Server exchange messages. Bind the local host address to socket using the bind function.h> #include<sys/socket.h> #include<arpa/inet. where the Client establishes a connection with the Server. Initialize server address to 0 using the bzero function. CODING: Server: fserver. TECHNICAL OBJECTIVE: To implement a full duplex application. METHODOLOGY: Server: Include the necessary header files. Request a connection from the server using the connect function.c #include<sys/types. Create a socket using socket function with family AF_INET. type as SOCK_STREAM. Initialize server address to 0 using the bzero function. sin_addr to INADDR_ANY. Fork the process to receive message from the client and print it on the console. Read message from the console and send it to the client. type as SOCK_STREAM. Assign the sin_family to AF_INET.Ex No:7 Date: FULL DUPLEX CHAT USING TCP/IP GIVEN REQUIREMENTS: There are two hosts. they send messages to and receive message from the other. The Client and Server can send as well as receive messages at the same time. Client: Include the necessary header files. Fork the process to receive message from the server and print it on the console. Read message from the console and send it to the server.h> #include<unistd. Client and Server. Accept connection request from the Client using accept function. Assign the sin_family to AF_INET. Get the server IP address and the Port number from the console.h> #include<netdb. Create a socket using socket function with family AF_INET. sin_port to dynamically assigned port number. and assign it to sin_addr of the server address structure. There is a two way communication between them. Listen on the socket for connection request from the client.e.h> #include<stdio.h> Page | 30 .

"Server is running.. /*Fork system call is used to create a new process*/ cpid=fork().buff.sin_family=AF_INET. char buff[1000]. bzero(&servaddr.char *argv[]) { int ad.(struct sockaddr*)&servaddr.clilen.cliaddr.0).#include<netinet/in... } } else { while(1) { Page | 31 .&clilen).SOCK_STREAM.sin_port=htons(5500).sizeof(servaddr))..sizeof(servaddr)).sin_addr.5).buff). /*Socket address structure*/ servaddr.(struct sockaddr*)&cliaddr. /*Listen function specifies the maximum number of connections that kernel should queue for this socket*/ listen(sd. printf("Received message from the client:%s\n". if(cpid==0) { while(1) { bzero(&buff. /*TCP socket is created. servaddr.s_addr=htonl(INADDR_ANY).h> int main(int argc. printf("%s\n". pid_t cpid. socklen_t servlen.sd. /*Bind function assigns a local protocol address to the socket*/ bind(sd.sizeof(buff).0). servaddr. /*Receiving the request from client*/ recv(ad..sizeof(buff)).").buff1[1000]. struct sockaddr_in servaddr.. an Internet socket address structure is filled with wildcard address & server’s well known port*/ sd=socket(AF_INET. /*The server to return the next completed connection from the front of the completed connection Queue calls it*/ ad=accept(sd.

/*Connect establishes connection with the server using server IP address*/ cd=connect(sd.0). } } return 0. struct sockaddr_in servaddr. Page | 32 .(struct sockaddr*)&servaddr. printf("%s\n". servaddr.sizeof(buff1)).buff1. servaddr.sizeof(buff)). bzero(&servaddr. /*Read the message from client*/ fgets(buff1.h> #include<sys/types."Enter the input data:"). /*Fork is used to create a new process*/ cpid=fork().SOCK_STREAM.sin_addr. } Client: fclient. /*Sends the message to client*/ send(ad.h> int main(int argc.h> #include<unistd. printf("%s\n".sizeof(servaddr)). socklen_t servlen.cd.h> #include<netinet/in.char *argv[]) { int sd. assigning IP address and port number for that socket*/ sd=socket(AF_INET.h> #include<stdio.h> #include<arpa/inet.stdin). if(cpid==0) { while(1) { bzero(&buff.strlen(buff1)+1.10000.c #include<sys/socket. servaddr.0).sin_port=htons(5500).sin_family=AF_INET.buff1[1000]. /*Creating a socket.cliaddr."Data sent…").h> #include<netdb.sizeof(servaddr)).clilen. char buff[1000]. pid_t cpid.s_addr=inet_addr(argv[1]).bzero(&buff1.

} } else { while(1) { bzero(&buff1.c [root@localhost 4ita33]# cc fserver. /*This function is used to read from server*/ fgets(buff. /*Send the message to server*/ send(sd."Enter the input data:").0)....buff1.buff1).printf("%s\n".buff. /*Receive the message from server*/ recv(sd."Data sent…").out Server is running. Enter the input data: Received message from the client:hi how are u Data sent… Enter the input data: Received message from the client:i am fine Page | 33 . printf("%s\n".strlen(buff)+1. printf("Received message from the server:%s\n". } SAMPLE OUTPUT: Server: (Host Name:Root1) [root@localhost 4ita33]# vi fserver.10000.stdin). } } return 0../a..c [root@localhost 4ita33]# ..sizeof(buff1)).sizeof(buff1).0).

/a.Client: (Host Name:Root2) [root@localhost 4ita33]# vi fclient.c [root@localhost 4ita33]# cc fclient.1 Enter the input data: hi Data sent… Enter the input data: Received message from the server:how are u i am fine Data sent… Enter the input data: INFERENCE: Thus the chat application full duplex communication is established by sending the request from the client to the server.0.c [root@localhost 4ita33]# .0. Page | 34 .out 127. server gets the message and gives response to the client and prints it.

Bind the local host address to socket using the bind function. Assign the sin_family to AF_INET.c #include<sys/types. Receive the file contents.h> #include<arpa/inet. Initialize server address to 0 using the bzero function. sin_addr to INADDR_ANY.h> #include<netdb.h> #include<stdio. where it is stored in a file. type as SOCK_STREAM.Ex No: Date: 8 IMPLEMENTATION OF FILE TRANSFER PROTOCOL GIVEN REQUIREMENTS: There are two hosts. Page | 35 . where the Client on establishing a connection with the Server sends the name of the file it wishes to access remotely. Create a socket using socket function with family AF_INET. Using gethostbyname function assign it to a hostent structure. Get the server IP address and the Port number from the console. read the file contents to a buffer and send the buffer to the Client. store it in a file and print it on the console. The Server then sends the contents of the file to the Client. Accept connection request from the Client using accept function.h> Include the necessary header files. TECHNICAL OBJECTIVE: To implement FTP application. Initialize server address to 0 using the bzero function. where it is stored. receive the file name from the Client.h> #include<netinet/in. sin_port to dynamically assigned port number. Listen on the socket for connection request from the client. type as SOCK_STREAM. Within an infinite loop. Assign the sin_family to AF_INET. CODING: Server: ftps.h> #include<sys/socket.h> #include<unistd. send the name of the file to be viewed to the Server. The Client sends the name of the file it needs from the Server and the Server sends the contents of the file to the Client. METHODOLOGY: Server: Client: Include the necessary header files. Within an infinite loop. Create a socket using socket function with family AF_INET.h> #include<sys/stat. Open the file. Client and Server. and assign it to sin_addr of the server address structure.

h> #include<netdb. Page | 36 . printf("%s\n". stat(buff.buff. bind(sd.&clilen).#include<string. fread(file. struct hostent *h. struct sockaddr_in servaddr.sizeof(servaddr)). socklen_t clilen.file[10000].c #include<sys/types.fp).(struct sockaddr*)&servaddr.char *argv[]) { int sd.s_addr=htonl(INADDR_ANY). struct stat x.sin_port=htons(1500). char buff[100]. FILE *fp.sizeof(buff). ad=accept(sd. send(ad.&x).sizeof(file)..ad.h> #include<arpa/inet.1.0).h> #include<stdio.(struct sockaddr*)&cliaddr.cd.sizeof(file)). } } Client: ftpc."r"). char buff[100]. clilen=sizeof(cliaddr). servaddr.sin_addr.h> int main(int argc.h> #include<sys/socket.cliaddr.sizeof(buff)).size.st_size. struct sockaddr_in servaddr.sizeof(file). servaddr.file.h> #include<unistd..h> #include<netinet/in. sd=socket(AF_INET. while(1) { bzero(buff. recv(ad. servaddr.0). bzero(file.SOCK_STREAM.cliaddr. size=x..").sin_family=AF_INET.char *argv[]) { int sd.5). socklen_t clilen."Server Is Running.sizeof(servaddr)). listen(sd.h> int main(int argc. fp=fopen(buff.file[10000].0). bzero(&servaddr.

file).0). send(sd.c [root@localhost 4ita33]# .h=gethostbyname(argv[1]). cd=connect(sd.sizeof(file).SOCK_STREAM.out Server is Running… FILE REACHED File output : this is my network lab Client: (Host Name:Root2) [root@localhost 4ita33]# vi ftpc.txt Page | 37 .0).buff.h->h_length).strlen(buff)+1.(struct sockaddr*)&servaddr.c [root@localhost 4ita33]# cc ftps.file.sin_addr./a. printf("%s\n"."Enter the File Name :").out Enter the filename: ita.sizeof(servaddr)). scanf("%s". memcpy((char *)&servaddr. } return 0. sd=socket(AF_INET.h->h_addr_list[0].sin_port=htons(1500).c [root@localhost 4ita33]# cc ftpc. servaddr. servaddr. while(1) { printf("%s\n"./a. } SAMPLE OUTPUT: Server: (Host Name:Root1) [root@localhost 4ita33]# vi ftps."File Output :"). printf("%s". bzero(&servaddr.0).sin_family=h->h_addrtype. recv(sd.c [root@localhost 4ita33]# .buff).s_addr.sizeof(servaddr)).

INFERENCE: Thus the FTP client-server communication is established and data is transferred between the client and server machines....Sending the file content Data sent. Page | 38 ..

type as SOCK_DGRAM.c #include<sys/types. Assign the sin_family to AF_INET. receive the command to be executed from the client.h> #include<stdio. and assign it to sin_addr of the server address structure. METHODOLOGY: Server: Client: Include the necessary header files. Within an infinite loop. Execute the command using the “system()” system call. The Server executes the command and the send result of the execution back to the Client. Get the server IP address and the Port number from the console. The Client sends a command to the Server. Create a socket using socket function with family AF_INET. which executes the command and sends the result back to the Client.h> #include<sys/socket. Receive the output from the server and print it on the console.Ex No: 9 Date: REMOTE COMMAND EXECUTION USING UDP GIVEN REQUIREMENTS: There are two hosts. Using gethostbyname() function assign it to a hostent structure.h> #include<netinet/in. sin_port to dynamically assigned port number. Initialize server address to 0 using the bzero function. Here. CODING: Server: udpremoteserver. Bind the local host using the bind() system call.txt” to the command. Append text “> temp. sin_addr to INADDR_ANY.h> #include<netdb. type as SOCK_DGRAM. Obtain the command to be executed in the server from the user. Create a socket using socket function with family AF_INET. Send the result of execution to the Client using a file buffer. Assign the sin_family to AF_INET. Send the command to the server. Initialize server address to 0 using the bzero function.h> Include the necessary header files. Client and Server. Page | 39 . the Client sends the command to the Server for remote execution. TECHNICAL OBJECTIVE: Remote Command execution is implemented through this program using which Client is able to execute commands at the Server.

c #include<sys/types.(struct sockaddr *)&servaddr.h> #include<arpa/inet.sin_family=AF_INET.SOCK_DGRAM. servaddr."r").(struct sockaddr *)&cliaddr.buff).h> #include<sys/stat.h> int main(int argc.#include<string. system(buff).sin_addr.(struct sockaddr *)&cliaddr. stat("file1". return 0.servaddr. servaddr.sin_port=htons(9976).h> int main(int argc.&x). char buff[1024]. sd=socket(AF_INET. socklen_t clilen. fread(file.h> #include<unistd.h> #include<stdio.h> #include<netinet/in.sizeof(buff)).sizeof(servaddr)). strcat(buff. recvfrom(sd.file[10000]. FILE *fp. clilen=sizeof(cliaddr).size.size.sizeof(buff).0.0).sizeof(cliaddr)).">file1").st_size. sendto(sd. } bind(sd. printf("Data Sent to UDPCLIENT %s".buff.h> #include<sys/stat.h> #include<arpa/inet.h> #include<netdb. servaddr.h> #include<unistd.1. struct stat x.h> #include<sys/socket. struct sockaddr_in cliaddr. if(sd<0) { printf("Socket CReation Error").fp). size=x. fp=fopen("file1".char* argv[]) { Page | 40 . } Client: udpremoteclient.file. bzero(&servaddr.s_addr=htonl(INADDR_ANY).sizeof(file).&clilen).0. while(1) { bzero(buff.h> #include<string.char* argv[]) { int sd.sizeof(servaddr)). } close(sd).

VIM(1) VIM(1) NAME vim .. } SAMPLE OUTPUT: Server: (Host Name:Root1) [root@localhost 4ita33]# vi udpremoteserver.stdin). sendto(sd. servlen=sizeof(servaddr)./a...&servlen).sizeof(servaddr)).1024. printf("Recieved From UDPSERVER %s". fgets(buff.(struct sockaddr *)&servaddr.strlen(file)+1. sd=socket(AF_INET. memcpy((char *)&servaddr. socklen_t servlen. char buff[1024]..0.(struct sockaddr *)&servaddr.file[10000].sin_addr.] vim [options] vim [options] -t tag vim [options] -q [errorfile} ex review rview renbjc Page | 41 . while(1) { printf("\nEnter the command to be executed")..c [root@localhost 4ita33]# . bzero(&servaddr.sin_family=h->h_addrtype. printf("\nData Sent").SOCK_DGRAM..buff.c [root@localhost 4ita33]# cc udpremoteserver. if(sd<0) { printf("Socket CReation Error").h->h_addr_list[0].sizeof(servaddr)).servaddr.0).strlen(buff)+1.h->h_length).0.sizeof(servaddr))..out Server is running. a programmers text editor SYNOPSIS vim [options] [file .int sd..(struct sockaddr *)&servaddr.Vi IMproved. } return 0. servaddr.file. struct hostent *h. } bind(sd. recvfrom(sd.file).sin_port=htons(9976). servaddr.. h=gethostbyname(argv[1]).. struct sockaddr_in cliaddr...

out 127. Page | 42 .1 Enter command: man vi Command sent to server Enter command: INFERENCE: Thus the Remote Command Execution between the client and server is implemented.c [root@localhost 4ita33]# ./a.c [root@localhost 4ita33]# cc udpremoteclient.Client: (Host Name:Root2) [root@localhost 4ita33]# vi udpremoteclient.0.0.

METHODOLOGY: Include the necessary header files. type as SOCK_DGRAM.h> Page | 43 . arp_dev=”eth0”.c #include<sys/types. CODING: ARP: arp. Ping the required Client. The IP address of any Client is given as the input. Declare structures arpreq ( as NULL structure. The IP address of any Client in the network is given as input and the corresponding hardware address is got as the output. The ARP cache is looked up for the corresponding hardware address.h> #include<net/if_arp.h> #include<sys/ioctl. This is returned as the output. Before compiling that Client is pinged. Assign the sin_family to AF_INET and sin_addr using inet_aton(). Using the object of arpreq structure assign the name of the Network Device to the data member arp_dev like. TECHNICAL OBJECTIVE: Address Resolution Protocol (ARP) is implemented through this program.h> #include<arpa/inet.h> #include<stdio. Create a socket using socket function with family AF_INET. if required) and sockaddr_in. Print the hardware address of the given IP address on the output console.Ex No: 10 Date: ARP IMPLEMENTATION USING UDP GIVEN REQUIREMENTS: There is a single host. Initialize server address to 0 using the bzero function.h> #include<sys/socket.h> #include<netinet/in. Using the ioctl() we get the ARP cache entry for the given IP address.h> #include<unistd. The output of the ioctl() function is stored in the sa_data[0] datamember of the arp_ha structure which is in turn a data member of structure arpreq.

sd=socket(AF_INET. 64 bytes from172. exit(0). struct arpreq myarp={{0}}. sin.29.*(ptr+2).19 ms 64 bytes from 172.arp_ha.*(ptr+1). } ptr=&myarp.SOCK_DGRAM. } memcpy(&myarp.16.51 (172.*(ptr+4).&myarp)==1) { printf("No Entry in ARP Cache for '%s'". strcpy(myarp.29.char *argv[]) { struct sockaddr_in sin={0}. if(ioctl(sd.16.29.*(ptr+5)).*ptr.sizeof(myarp.0). exit(0). unsigned char *ptr.29.argv[1]).int main(int argc.51 PING 172.arp_pa.SIOCGARP. int sd.sin_family=AF_INET.51: icmp_seq=1 ttl=64 time=1. printf("%X:%X:%X:%X:%X:%X\n".*(ptr+3).16.c [root@localhost 4ita33]# ping 172.29.c (Host Name:Root1) [root@localhost 4ita33]# vi arp.16.51: icmp_seq=2 ttl=64 time=0. if(inet_aton(argv[1]. return 0.sin_addr)==0) { printf("Ip address Entered '%s' is not valid \n".&sin.51) 56(84) bytes of data."eth0").817 ms Page | 44 .&sin. } SAMPLE OUTPUT: Host: arp.*(ptr+5).arp_pa)).argv[1]).16.sa_data[0].argv[1]).arp_dev. printf("MAC Address For '%s' : ".

29. 0% packet loss.005/1.16.51 Hardware Address is: 172.51: The MAC address is:0:8:5C:5D:47:50: INFERENCE: Thus the ARP implementation is developed to gets the MAC address of the remote machine’s IP address from ARP cache and prints it.29.16.16./a.--.29. Page | 45 .out 172.193/0. 2 received.172. time 999ms rtt min/avg/max/mdev = 0.817/1.c [root@localhost 4ita33]# .188 ms [root@localhost 4ita33]# cc arp.51ping statistics --2 packets transmitted.