Faculty of Engineering and Technology Department of Computer Science and Engineering
Faculty of Engineering and Technology Department of Computer Science and Engineering
Faculty of Engineering and Technology Department of Computer Science and Engineering
Lab Manual
SUBJECT CODE / TITLE:18CSC302J/ Computer Networks
Page | 1
18CSC302J/ Computer Networks
LIST OF EXPERIMENTS
Page | 2
18CSC302J/ Computer Networks
Page | 3
Ex.No:1
Date:
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. [Portable Operating System Interface]
3. string.h:
This header file is used to perform string manipulation operations on NULL terminated
strings.(Bzero -0 the m/y)
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.stat() updates any time related fields.when copying from 1 file to
another.
11. 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.
Page | 4
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.
Page | 5
Ex No: 2
Date:
AIM:
To discuss some of the basic functions used for socket programming.
1.man socket
NAME:
SYNOPSIS:
#include<sys/types.h>
#include<sys/socket.h>
int socket(int domain,int type,int protocol);
DESCRIPTION:
⮚ The domain parameter specifies a common domain this selects the protocol family which
will be used for communication.
⮚ These families are defined in <sys/socket.h>.
FORMAT:
NAME PURPOSE
PF_UNIX,PF_LOCAL Local Communication.
PF_INET IPV4 Internet Protocols.
PF_IPX IPX-Novell Protocols.
PF_APPLETALK Apple Talk.
⮚ The socket has the indicated type, which specifies the communication semantics.
TYPES:
1.SOCK_STREAM:
Page | 6
⮚ Provides sequenced , reliable, two-way , connection based byte streams.
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:
⮚ 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:
ERRORS:
Page | 7
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 destination(s)
reject[-t] [-h server] [-r reason] destination(s)
DESCRIPTION:
⮚ accept instructs the printing system to accept print jobs to the specified destination.
4.man send
NAME:
SYNOPSIS:
#include<sys/types.h>
#include<sys/socket.h>
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.
Page | 8
⮚ The only difference between send and write is the presence of flags.
5.man recv
NAME:
SYNOPSIS:
#include<sys/types.h>
#include<sys/socket.h>
ssize_t recv(int s, void *buf, size_t len, int flags);
ssize_t recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t* from len);
ssize_t recvmsg(int s, struct msghdr *msg, int flags);
DESCRIPTION:
⮚ The recvfrom and recvmsg calls are used to receive messages from a socket, and may be
used to recv data on a socket whether or not it is connection oriented.
⮚ If from is not NULL, and the underlying protocol provides the src addr , this src addr is
filled in.
⮚ The recv call is normally used only on a connection socket and is identical to recvfrom
with a NULL from parameter.
6.man read
NAME:
7.man write
NAME:
SYNOPSIS:
write user[ttyname]
DESCRIPTION:
⮚ write allows you to communicate with other users, by copying lines from terminal to
………
Page | 9
⮚ 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.
⮚ If the other user wants to reply they must run write as well.
8. ifconfig
NAME:
SYNOPSIS:
ifconfig[interface]
ifconfig interface[aftype] options | address……
DESCRIPTION:
⮚ 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:
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:
Page | 10
⮚ 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.
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.
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:
Page | 11
⮚ The gethostbyname() returns a structure of type hostent for the given hostname.
RESULT:
Thus the basic functions used for Socket Programming was studied successfully.
Page | 12
Ex No: 3
Date:
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 and prints it.
TECHNICAL OBJECTIVE:
METHODOLOGY:
Server:
⮚ Include the necessary header files.
⮚ Create a socket using socket function with family AF_INET, type as SOCK_STREAM.
⮚ Within an infinite loop, using the recv function receive message from the client and print it
on the console.
Client:
⮚ Include the necessary header files.
⮚ Create a socket using socket function with family AF_INET, type as SOCK_STREAM.
⮚ Get the server IP address and port number from the console.
Page | 13
⮚ Request a connection from the server using the connect function.
⮚ Within an infinite loop, read message from the console and send the message to the server
using the send function.
CODING:
Server: tcpserver.c
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<string.h>
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));
/*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);
/*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
Page | 14
#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));
/*Creating a socket, assigning IP address and port number for that socket*/
sd = socket(AF_INET,SOCK_STREAM,0);
/*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
Page | 15
Server is running….
Message received is hi
Message received is hi
Client:
(Host Name:Root2)
Data Sent hi
Enter the message:
how r u
INFERENCE:
Thus, a program to perform simple communication between client and server using TCP/IP was
implemented.
Page | 16
Page | 17
Ex No: 4
Date:
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:
METHODOLOGY:
Server:
⮚ Include the necessary header files.
⮚ Create a socket using socket function with family AF_INET, type as SOCK_DGRAM.
⮚ 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.
Page | 18
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);
Page | 19
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 to client");
exit(1);
}
printf("Send data to UDP Client: %s",buff);
}
cloSe(sd);
return 0;
}
Client: udpclient.c
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<netinet/in.h>
#include<netdb.h>
int main(int argc,char*argv[])
{
int sd;
char buff[1024];
struct sockaddr_in servaddr;
socklen_t len;
len=sizeof(servaddr);
Page | 20
/*Reads the message from standard input*/
fgets(buff,sizeof (buff),stdin);
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.1
Enter input data :
hi
Data sent to UDP Server:hi
Received Data from server: hi
Page | 21
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.
Ex No:5
Date:
CONCURRENT TCP/IP DAY-TIME SERVER
GIVEN REQUIREMENTS:
There are two hosts, Client and Server. The Client requests the concurrent server for the date and time.
The Server sends the date and time, which the Client accepts and prints.
TECHNICAL OBJECTIVE:
To implement a TCP/IP day time server (concurrent server) that handles multiple client
requests. Once the client establishes connection with the server, the server sends its day-time details to the
client which the client prints in its console.
METHODOLOGY:
Server:
⮚ Include the necessary header files.
⮚ Create a socket using socket function with family AF_INET, type as SOCK_STREAM.
⮚ Within a for loop, accept connection request from the client using accept function.
⮚ Calculate the current date and time using the ctime() function. 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.
Client:
⮚ Include the necessary header files.
⮚ Create a socket using socket function with family AF_INET, type as SOCK_STREAM.
Page | 22
⮚ Initialize server address to 0 using the bzero function.
⮚ Within an infinite loop, receive the date and time from the server using the read function
and print the date and time on the console.
CODING:
Server: dtserver.c
#include<time.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<stdio.h>
#include<string.h>
#include<netinet/in.h>
#include<netdb.h>
0int main(int argc,char *argv[])
{
int sd,ad;
char buff[1024];
struct sockaddr_in servaddr,cliaddr;
//socklen_t clilen=sizeof(cliaddr);
time_t t1;
bzero(&servaddr,sizeof(servaddr));
/*Socket address structure*/
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(1507);
/*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);
/*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 *)NULL,NULL);
while(1)
Page | 23
{
bzero(&buff,sizeof(buff));
Client: dtclient.c
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<netinet/in.h>
#include<unistd.h>
#include<time.h>
int main(int argc,char *argv[])
{
int sd,ad;
char buff[1024];
struct sockaddr_in cliaddr,servaddr;
struct hostent *h;
h=gethostbyname(argv[1]);
bzero(&servaddr,sizeof(servaddr));
SAMPLE OUTPUT:
Server:
(Host Name:Root1)
[root@localhost 4ita33]# vi dtserver.c
[root@localhost 4ita33]# cc dtserver.c
[root@localhost 4ita33]# ./a.out
Page | 24
Server is running…
Client:
(Host Name:Root2)
INFERENCE:
Thus the concurrent daytime client- 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.
Ex No:6
Date:
HALF DUPLEX CHAT USING TCP/IP
GIVEN REQUIREMENTS:
There are two hosts, Client and Server. Both the Client and the Server exchange message i.e. they send
messages or receive message from the other. There is only a single way communication between them.
TECHNICAL OBJECTIVE:
To implement a half duplex application, where the Client establishes a connection with the
Server. The Client can send and the server well receive messages at the same time.
METHODOLOGY:
Server:
⮚ Include the necessary header files.
⮚ Create a socket using socket function with family AF_INET, type as SOCK_STREAM.
Page | 25
⮚ Fork the process to receive message from the client and print it on the console.
⮚ Create a socket using socket function with family AF_INET, type as SOCK_STREAM.
⮚ Get the server IP address and the Port number from the console.
⮚ Fork the process to receive message from the server and print it on the console.
CODING:
Server: hserver.c
#include<sys/types.h>
#include<stdio.h>
#include<netdb.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<netinet/in.h>
int main(int argc,char *argv[])
{
int n,sd,ad;
struct sockaddr_in servaddr,cliaddr;
socklen_t clilen,servlen;
char buff[10000],buff1[10000];
bzero(&servaddr,sizeof(servaddr));
Page | 26
/*Bind function assigns a local protocol address to the socket*/
bind(sd,(struct sockaddr*)&servaddr,sizeof(servaddr));
/*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));
Client: hclient.c
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdio.h>
#include<netdb.h>
int main(int argc,char *argv[])
{
int n,sd,cd;
struct sockaddr_in servaddr,cliaddr;
socklen_t servlen,clilen;
char buff[10000],buff1[10000];
Page | 27
bzero(&servaddr,sizeof(servaddr));
/*Creating a socket, assigning IP address and port number for that socket*/
sd=socket(AF_INET,SOCK_STREAM,0);
SAMPLE OUTPUT:
Server:
(Host Name:Root1)
Server is running…
Receive from the client:hi
Page | 28
Enter the input data:
how are u da ..
Data sent
Client:
(Host Name:Root2)
INFERENCE:
Thus the chat application full duplex communication is established by sending the request from
the client to the server, server gets the message and gives response to the client and prints it.
Ex No:7
Date:
FULL DUPLEX CHAT USING TCP/IP
GIVEN REQUIREMENTS:
There are two hosts, Client and Server. Both the Client and the Server exchange message i.e. they send
messages to and receive message from the other. There is a two way communication between them.
TECHNICAL OBJECTIVE:
To implement a full duplex application, where the Client establishes a connection with the
Server. The Client and Server can send as well as receive messages at the same time. Both the Client and
Server exchange messages.
METHODOLOGY:
Server:
Page | 29
⮚ Include the necessary header files.
⮚ Create a socket using socket function with family AF_INET, type as SOCK_STREAM.
⮚ Fork the process to receive message from the client and print it on the console.
Client:
⮚ Include the necessary header files.
⮚ Create a socket using socket function with family AF_INET, type as SOCK_STREAM.
⮚ Get the server IP address and the Port number from the console.
⮚ Fork the process to receive message from the server and print it on the console.
CODING:
Server: fserver.c
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<unistd.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<netinet/in.h>
int main(int argc,char *argv[])
{
int ad,sd;
Page | 30
struct sockaddr_in servaddr,cliaddr;
socklen_t servlen,clilen;
char buff[1000],buff1[1000];
pid_t cpid;
bzero(&servaddr,sizeof(servaddr));
/*Listen function specifies the maximum number of connections that kernel should queue
for this socket*/
listen(sd,5);
printf("%s\n","Server is running.......");
/*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);
if(cpid==0)
{
while(1)
{
bzero(&buff,sizeof(buff));
Page | 31
fgets(buff1,10000,stdin);
Client: fclient.c
#include<sys/socket.h>
#include<sys/types.h>
#include<stdio.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<netdb.h>
#include<netinet/in.h>
int main(int argc,char *argv[])
{
int sd,cd;
struct sockaddr_in servaddr,cliaddr;
socklen_t servlen,clilen;
char buff[1000],buff1[1000];
pid_t cpid;
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=inet_addr(argv[1]);
servaddr.sin_port=htons(5500);
/*Creating a socket, assigning IP address and port number for that socket*/
sd=socket(AF_INET,SOCK_STREAM,0);
Page | 32
/*Send the message to server*/
send(sd,buff,strlen(buff)+1,0);
printf("%s\n","Data sent…");
}
}
else
{
while(1)
{
bzero(&buff1,sizeof(buff1));
SAMPLE OUTPUT:
Server:
(Host Name:Root1)
Server is running.......
Enter the input data:
Received message from the client:hi
how are u
Data sent…
Client:
(Host Name:Root2)
[root@localhost 4ita33]# vi fclient.c
[root@localhost 4ita33]# cc fclient.c
[root@localhost 4ita33]# ./a.out 127.0.0.1
Enter the input data:
hi
Data sent…
Page | 33
Enter the input data:
Received message from the server:how are u
i am fine
Data sent…
INFERENCE:
Thus the chat application full duplex communication is established by sending the request from
the client to the server, server gets the message and gives response to the client and prints it.
Page | 34
Ex No: 8
Date:
IMPLEMENTATION OF FILE TRANSFER PROTOCOL
GIVEN REQUIREMENTS:
There are two hosts, Client and 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, where it is stored in a file.
TECHNICAL OBJECTIVE:
To implement FTP application, where the Client on establishing a connection with the
Server sends the name of the file it wishes to access remotely. The Server then sends the contents of the
file to the Client, where it is stored.
METHODOLOGY:
Server:
⮚ Include the necessary header files.
⮚ Create a socket using socket function with family AF_INET, type as SOCK_STREAM.
⮚ Within an infinite loop, receive the file name from the Client.
⮚ Open the file, read the file contents to a buffer and send the buffer to the Client.
Client:
⮚ Include the necessary header files.
⮚ Create a socket using socket function with family AF_INET, type as SOCK_STREAM.
⮚ Get the server IP address and the Port number from the console.
Page | 35
⮚ Receive the file contents, store it in a file and print it on the console.
CODING:
Server: ftps.c
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/stat.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<netdb.h>
#include<unistd.h>
#include<stdio.h>
#include<string.h>
int main(int argc,char *argv[])
{
int sd,ad,size;
struct sockaddr_in servaddr,cliaddr;
socklen_t clilen;
clilen=sizeof(cliaddr);
struct stat x;
char buff[100],file[10000];
FILE *fp;
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(1500);
sd=socket(AF_INET,SOCK_STREAM,0);
bind(sd,(struct sockaddr*)&servaddr,sizeof(servaddr));
listen(sd,5);
printf("%s\n","Server Is Running....");
ad=accept(sd,(struct sockaddr*)&cliaddr,&clilen);
while(1)
{
bzero(buff,sizeof(buff));
bzero(file,sizeof(file));
recv(ad,buff,sizeof(buff),0);
fp=fopen(buff,"r");
stat(buff,&x);
size=x.st_size;
fread(file,sizeof(file),1,fp);
send(ad,file,sizeof(file),0);
}
}
Client: ftpc.c
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
Page | 36
#include<arpa/inet.h>
#include<netdb.h>
#include<stdio.h>
#include<unistd.h>
int main(int argc,char *argv[])
{
int sd,cd;
struct sockaddr_in servaddr,cliaddr;
socklen_t clilen;
char buff[100],file[10000];
struct hostent *h;
h=gethostbyname(argv[1]);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=h->h_addrtype;
memcpy((char *)&servaddr.sin_addr.s_addr,h->h_addr_list[0],h->h_length);
servaddr.sin_port=htons(1500);
sd=socket(AF_INET,SOCK_STREAM,0);
cd=connect(sd,(struct sockaddr*)&servaddr,sizeof(servaddr));
while(1)
{
printf("%s\n","Enter the File Name :");
scanf("%s",buff);
send(sd,buff,strlen(buff)+1,0);
printf("%s\n","File Output :");
recv(sd,file,sizeof(file),0);
printf("%s",file);
}
return 0;
}
SAMPLE OUTPUT:
Server:
(Host Name:Root1)
Server is Running…
FILE REACHED
File output : this is my network lab
Client:
(Host Name:Root2)
Page | 37
[root@localhost 4ita33]# vi ftpc.c
[root@localhost 4ita33]# cc ftpc.c
[root@localhost 4ita33]# ./a.out
Enter the filename:
ita.txt
Sending the file content
Data sent.....
INFERENCE:
Thus the FTP client-server communication is established and data is transferred between the
client and server machines.
Ex No: 9
Date:
REMOTE COMMAND EXECUTION USING UDP
GIVEN REQUIREMENTS:
There are two hosts, Client and Server. The Client sends a command to the Server, which executes the
command and sends the result back to the Client.
TECHNICAL OBJECTIVE:
Remote Command execution is implemented through this program using which Client is
able to execute commands at the Server. Here, the Client sends the command to the Server for remote
execution. The Server executes the command and the send result of the execution 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.
⮚ Within an infinite loop, receive the command to be executed from the client.
Page | 38
Client:
⮚ Include the necessary header files.
⮚ Create a socket using socket function with family AF_INET, type as SOCK_DGRAM.
⮚ Get the server IP address and the Port number from the console.
⮚ Receive the output from the server and print it on the console.
CODING:
Server: udpremoteserver.c
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<netdb.h>
#include<netinet/in.h>
#include<string.h>
#include<sys/stat.h>
#include<arpa/inet.h>
#include<unistd.h>
int main(int argc,char* argv[])
{
int sd,size;
char buff[1024],file[10000];
struct sockaddr_in cliaddr,servaddr;
FILE *fp;
struct stat x;
socklen_t clilen;
clilen=sizeof(cliaddr);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(9976);
sd=socket(AF_INET,SOCK_DGRAM,0);
if(sd<0)
{
printf("Socket CReation Error");
}
bind(sd,(struct sockaddr *)&servaddr,sizeof(servaddr));
while(1)
Page | 39
{
bzero(buff,sizeof(buff));
recvfrom(sd,buff,sizeof(buff),0,(struct sockaddr *)&cliaddr,&clilen);
strcat(buff,">file1");
system(buff);
fp=fopen("file1","r");
stat("file1",&x);
size=x.st_size;
fread(file,size,1,fp);
sendto(sd,file,sizeof(file),0,(struct sockaddr *)&cliaddr,sizeof(cliaddr));
printf("Data Sent to UDPCLIENT %s",buff);
}
close(sd);
return 0; }
Client: udpremoteclient.c
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<unistd.h>
#include<netdb.h>
#include<netinet/in.h>
#include<string.h>
#include<arpa/inet.h>
#include<sys/stat.h>
int main(int argc,char* argv[])
{
int sd;
char buff[1024],file[10000];
struct sockaddr_in cliaddr,servaddr;
struct hostent *h;
socklen_t servlen;
servlen=sizeof(servaddr);
h=gethostbyname(argv[1]);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=h->h_addrtype;
memcpy((char *)&servaddr.sin_addr,h->h_addr_list[0],h->h_length);
servaddr.sin_port=htons(9976);
sd=socket(AF_INET,SOCK_DGRAM,0);
if(sd<0)
{
printf("Socket CReation Error");
}
bind(sd,(struct sockaddr *)&servaddr,sizeof(servaddr));
while(1)
{
printf("\nEnter the command to be executed");
fgets(buff,1024,stdin);
sendto(sd,buff,strlen(buff)+1,0,(struct sockaddr *)&servaddr,sizeof(servaddr));
printf("\nData Sent");
recvfrom(sd,file,strlen(file)+1,0,(struct sockaddr *)&servaddr,&servlen);
Page | 40
printf("Recieved From UDPSERVER %s",file);
}
return 0;
}
SAMPLE OUTPUT:
Server:
(Host Name:Root1)
Client:
(Host Name:Root2)
Enter command:
INFERENCE:
Thus the Remote Command Execution between the client and server is implemented.
Page | 41
Ex No: 10
Date:
ARP IMPLEMENTATION USING UDP
GIVEN REQUIREMENTS:
There is a single host. The IP address of any Client in the network is given as input and the corresponding
hardware address is got as the output.
TECHNICAL OBJECTIVE:
Address Resolution Protocol (ARP) is implemented through this program. The IP address
of any Client is given as the input. The ARP cache is looked up for the corresponding hardware address.
This is returned as the output. Before compiling that Client is pinged.
METHODOLOGY:
⮚ Create a socket using socket function with family AF_INET, type as SOCK_DGRAM.
Page | 42
⮚ Initialize server address to 0 using the bzero function.
⮚ Using the object of arpreq structure assign the name of the Network Device to the data
member arp_dev like, arp_dev=”eth0”.
⮚ Ping the required Client.
⮚ Using the ioctl() we get the ARP cache entry for the given IP address.
⮚ 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.
⮚ Print the hardware address of the given IP address on the output console.
CODING:
ARP: arp.c
#include<sys/types.h>
#include<sys/socket.h>
#include<net/if_arp.h>
#include<sys/ioctl.h>
#include<stdio.h>
#include<unistd.h>
#include<netinet/in.h>
#include<arpa/inet.h>
int main(int argc,char *argv[])
{
struct sockaddr_in sin={0};
struct arpreq myarp={{0}};
unsigned char *ptr;
int sd;
sin.sin_family=AF_INET;
if(inet_aton(argv[1],&sin.sin_addr)==0)
{
printf("Ip address Entered '%s' is not valid \n",argv[1]);
exit(0);
}
memcpy(&myarp.arp_pa,&sin,sizeof(myarp.arp_pa));
strcpy(myarp.arp_dev,"eth0");
sd=socket(AF_INET,SOCK_DGRAM,0);
if(ioctl(sd,SIOCGARP,&myarp)==1)
{
printf("No Entry in ARP Cache for '%s'",argv[1]);
exit(0);
}
ptr=&myarp.arp_ha.sa_data[0];
printf("MAC Address For '%s' : ",argv[1]);
printf("%X:%X:%X:%X:%X:%X\n",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5),*(ptr+5));
Page | 43
return 0;
}
SAMPLE OUTPUT:
Host: arp.c
(Host Name:Root1)
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.
Page | 44