Anda di halaman 1dari 25

CS 1305 – NETWORK LAB Department of Information Technology

Ex.No: 1 STUDY OF NETWORK PROGRAMMING & SOCKET


SYSTEM CALLS

Aim:
To study about various Network Programming and socket system calls.

Preparing an Internet address

The first thing you need to do when writing network applications, is obtain the addresses of
the two involved machines: your address, and the remote host's address.

This process is made up of several stages:

1. Address Resolution - Translating the host name into an IP address.


2. Byte Ordering - Translating host byte order into network byte order.
3. Address Formation - Forming up the remote address in the right structure.

Address Resolution

Given a host name, we want to find it's IP address. We do that using the function
gethostbyname(). This function is defined in the file /usr/include/netdb.h (or the equivalent for
your system) as follows:

struct hostent *gethostbyname(char *hostname);

The input to the function will be the name of the host whose address we want to resolve. The
function returns a pointer to a structure hostent, whose definition is as follows:

struct hostent {
char* h_name; /* official name of host */
char** h_aliases; /* alias list */
int h_addrtype; /* host address type */
int h_length; /* length of address */
char** h_addr_list; /* list of addresses from name server */
#define h_addr h_addr_list[0] /* address, for backward compatibility */
};

Lets see what each field in the hostent structure means:

• h_name: This is the official name of the host, i.e. the full address.
• h_aliases: a pointer to the list of aliases (other names) the host might have.
• h_addrtype: The type of address this host uses.
• h_length: The length of the address. Different address types might have different
lengths.

Prepared by R.MuthuKumar
CS 1305 – NETWORK LAB Department of Information Technology

• h_addr_list: A pointer to the list of addresses of the host. Note that a host might have
more than one address, as explained earlier.
• h_addr: In older systems, there was only the h_addr field, so it is defined here so old
programs could compile without change on newer systems.

Byte Ordering

We normally have several functions (or macros) to form 4 types of translations:

• htons() - short integer from host byte order to network byte order.
• ntohs() - short integer from network byte order to host byte order.
• htonl() - long integer from host byte order to network byte order.
• ntohl() - long integer from network byte order to host byte order.

Address Formation

Forming addresses for Internet protocols is done using a structure named


sockaddr_in, whose definition, as given in the file /usr/include/netinet/in.h, is as follows:

struct sockaddr_in {
short int sin_family; /* Address family */
unsigned short sin_port; /* Port number */
struct in_addr sin_addr; /* Internet address */

/* Pad to size of `struct sockaddr'. */


/* Pad definition deleted */
};
The fields have the following meanings:

• sin_family: Family of protocols for this address. We will want the Internet family.
• sin_port: The port part of the address.
• sin_addr: The IP number part of the address.
• Pad: This will be explained in the next section.

The socket interface

A socket is formally defined as an endpoint for communication between an


application program, and the underlying network protocols. This odd collection of words
simply means that the program reads information from a socket in order to read from the
network, writes information to it in order to write to the network, and sets sockets options in
order to control protocol options. From the programmer's point of view, the socket is identical
to the network. Just like a file descriptor is the endpoint of disk operations.

Types of sockets

In general, 3 types of sockets exist on most Unix systems: Stream sockets, Datagram
sockets and Raw sockets.

Prepared by R.MuthuKumar 2
CS 1305 – NETWORK LAB Department of Information Technology

Stream sockets are used for stream connections, i.e. connections that exist for a long duration.
TCP connections use stream sockets.

Datagram sockets are used for short-term connections, that transfer a single packet across the
network before terminating. the UDP protocol uses such sockets, due to its connection-less
nature.

Raw sockets are used to access low-level protocols directly, bypassing the higher protocols.
They are the means for a programmer to use the IP protocol, or the physical layer of the
network, directly. Raw sockets can there for be used to implement new protocols on top of the
low-level protocols. Naturally, they are out of our scope.

Creating sockets

Creation of sockets is done using the socket() system call. This system call is
defined as follows:

int socket(int address_family, int socket_type, int proto_family);

address_family defines the type of addresses we want this socket to use, and therefor defines
what kind of network protocol the socket will use. We will concentrate on the Internet address
family, cause we want to write Internet applications.

socket_type could be one of the socket types we mentioned earlier, or any other socket type
that exists on your system. We choose the socket type according to the kind of interaction (and
type or protocol) we want to use.

proto_family selects which protocol we want to socket to use. We will usually leave this value
as 0 (or the constant PF_UNSPEC on some systems), and let the system choose the most suitable
protocol for us. As for the protocol itself, In the Internet address family, a socket type of
SOCK_STREAM will cause the protocol type to be set to TCP. A socket type of SOCK_DGRAM
(Datagram socket) will cause the protocol type to be set to UDP.

The socket system call returns a file descriptor which will be used to reference the socket in
later requests by the application program. If the call fails, however (due to lack of resources)
the value returned will be negative (note that file descriptors have to be non-negative integers).
As an example, suppose that we want to write a TCP application. This application needs at
least one socket in order to communicate across the Internet, so it will contain a call such as
this:

int s; /* descriptor of socket */

/* Internet address family, Stream socket */


s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0) {
perror("socket: allocation failed");

Prepared by R.MuthuKumar 3
CS 1305 – NETWORK LAB Department of Information Technology

Associating a socket with a connection

After a socket is created, it still needs to be told between which two end points it will
communicate. It needs to be bound to a connection. There are two steps to this binding. The
first is binding the socket to a local address. The second is binding it to a remote (foreign)
address. Binding to a local address could be done either explicitly, using the bind() system
call, or implicitly, when a connecting is established. Binding to the remote address is done only
when a connection is established. To bind a socket to a local address, we use the bind()
system call, which is defined as follows:

int bind(int socket, struct sockaddr *address, int addrlen);

There are 4 possible variations of address binding that might be used when binding a
socket in the Internet address family. The first is binding the socket to a specific address, i.e. a
specific IP number and a specific port. This is done when we know exactly where we want to
receive messages. Actually this form is not used in simple servers, since usually these servers
wish to accept connections to the machine, no matter which IP interface it came from. The
second form is binding the socket to a specific IP number, but letting the system choose an
unused port number. This could be done when we don't need to use a well-known port. The
third form is binding the socket to a wild-card address called INADDR_ANY (by assigning it
to the sockaddr_in variable), and to a specific port number. This is used in servers that are
supposed to accept packets sent to this port on the local host, regardless of through which
physical network interface the packet has arrived (remember that a host might have more than
one IP address).The last form is letting the system bind the socket to any local IP address and
to pick a port number by itself. This is done by not using the bind() system call on the socket.
The system will make the local bind when a connection through the socket is established, i.e.
along with the remote address binding. This form of binding is usually used by clients.

Connection establishment

The connect() system call is responsible to making the connection to the specified
address of the remote machine, using the specified socket. Note that the address is being type-
cast into the general address type, struct sockaddr, because this same system call is used to
establish connections in various address families, not just the Internet address family.

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

int connect(int s, struct sockaddr *name, int namelen);

The bind() call only allows specification of a local address. To specify the remote side of an
address connection the connect() call is used. In the call to connect, s is the file descriptor for
the socket. name is a pointer to a structure of type sockaddr:

struct sockaddr {

Prepared by R.MuthuKumar 4
CS 1305 – NETWORK LAB Department of Information Technology

u_short sa_family;
char sa_data[14];
};

As with the bind() system call, name.sa_family should be AF_UNIX. name.sa_data should
contain up to 14 bytes of a file name which will be assigned to the socket. namelen gives the
actual length of name. A return value of 0 indicates success, while a value of -1 indicates
failure with errno describing the error.

Sending and receiving data over a socket

After a connection is established (We will explain that when talking about Client and
Server writing), There are several ways to send information over the socket. We will only
describe one method for reading and one for writing.
The read() system call

The most common way of reading data from a socket is using the read() system call,
which is defined like this:

int read(int socket, char *buffer, int buflen);

• socket - The socket from which we want to read.


• buffer - The buffer into which the system will write the data bytes.
• buflen - Size of the buffer, in bytes (actually, how much data we want to read).
The read system call returns one of the following values:
• 0 - The connection was closed by the remote host.
• -1 - The read system call was interrupted, or failed for some reason.
• n - The read system call put 'n' bytes into the buffer we supplied it with.
Note that read() might read less than the number of bytes we requested, due to unavailability
of buffer space in the system.

The write() system call

The most common way of writing data to a socket is using the write() system call,
which is defined like this:

int write(int socket, char *buffer, int buflen);

• socket - The socket into which we want to write.


• buffer - The buffer from which the system will read the data bytes.
• buflen - Size of the buffer, in bytes (actually, how much data we want to write).
The write system call returns one of the following values:

• 0 - The connection was closed by the remote host.


• -1 - The write system call was interrupted, or failed for some reason.
• n - The write system call wrote 'n' bytes into the socket.

Prepared by R.MuthuKumar 5
CS 1305 – NETWORK LAB Department of Information Technology

Note that the system keeps internal buffers, and the write system call write data to those
buffers, not necessarily directly to the network. thus, a successful write() doesn't mean the
data arrived at the other end, or was even sent onto the network. Also, it could be that only
some of the bytes were written, and not the actual number we requested. It is up to us to try to
send the data again later on, when it's possible, and we'll show several methods for doing just
that.

Closing a socket

When we want to abort a connection, or to close a socket that is no longer needed, we


can use the close() system call. it is defined simply as:

int close(int socket);

• socket - The socket that we wish to close. If it is associated with an open connection,
the connection will be closed.

Ex.No: 2 IMPLEMENTATION OF TCP ECHO CLIENT & SERVER

Aim:
Prepared by R.MuthuKumar 6
CS 1305 – NETWORK LAB Department of Information Technology

To implement a TCP Echo client which establishes a TCP connection with the server &
to print what the line echoed from the server to the client.

Problem Description:

1. The client reads a line of text from its standard input and writes the line to the server.
2. The server reads the line from its network input and echoes the line back to the client.
3. The client reads the echoed line and prints it on its standard output.

fgets writen readline


stdin
TCP TCP
Client Server
stdout
fputs readline writen

4. Two arrows between the client and server but this is one full duplex TCP connection.
The fgets and fputs functions are from the standard I/O library and the written and
readline functions

Algorithm:
TCP Echo Client:
1. A TCP Socket is created using Socket system calls and an Internet socket address
structure is filled in with the server’s IP address and port number.
2. Take the server’s IP address from the command line argument and the server’s well
known port.
3. Connect system call to establish the connection with the server.
4. fgets reads a line of text and writen sends the line to the server.
5. readline reads the line echoed back from the server and fputs writes it to the standard
output.
6. The loop terminates when fgets returns a null pointer, which occurs when it encounters
either an end -of –file or an error.

Prepared by R.MuthuKumar 7
CS 1305 – NETWORK LAB Department of Information Technology

TCP Echo Server

1. A TCP Socket is created. An Internet socket address structure is filled in with the
wildcard address(INADDR_ANY) and the server’s well known port (SERV_PORT is
defined as 9877)
2. The server blocks in the call to accept, waiting for a client connection to complete.
3. For each client, fork spawns a child, and the child handles the new client. The child
closes the listening socket and the parent closes the connected socket.
4. The server processing for each client: reading the lines from the client and echoing
them back to the client.
5. readline reads the next line from the socket and the line is echoed back to the client by
written. If the client closes the connection, the receipt of the client’s FIN causes the
child’s readline to return 0.

TCP ECHO SERVER PROGRAM

#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
main()
Prepared by R.MuthuKumar 8
CS 1305 – NETWORK LAB Department of Information Technology

{
socklen_t clilen;
int s1,s2;
char line[8]="";
pid_t chipid;
struct sockaddr_in cliaddr,servaddr;
s1=socket(AF_INET,SOCK_STREAM,0); // create socket
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=inet_addr("180.100.103.3");
servaddr.sin_port=htons(9877);
bind(s1,(struct sockaddr *)&servaddr,sizeof(servaddr)); // bind the socket with server IP
Address
listen(s1,8); // listen the client connection
for( ; ; )
{
clilen=sizeof(cliaddr);
if(chipid==0)
{
clilen=sizeof(cliaddr);
s2=accept(8,(struct sockaddr *)&cliaddr,&clilen); // accept the connection from client
close(s1);
for(; ;)
{
read(s2,line,8); // read the line of text from the networks input
write(s2,line,8); // write the echoes line back to the client
}
exit(0);
}
}
}

TCP ECHO CLIENT PROGRAM

# include<sys/socket.h>
# include<sys/types.h>
# include<netinet/in.h>
# include<stdio.h>
# include<string.h>
main(int argc,char * *argv)
{
int sockfd;
char *sline,*cline,str[8],str1[8];
struct sockaddr_in seraddr;
if(argc!=2)
{
printf("error");

Prepared by R.MuthuKumar 9
CS 1305 – NETWORK LAB Department of Information Technology

}
sockfd=socket(AF_INET,SOCK_STREAM,0);// create socket
seraddr.sin_family=AF_INET; // socket family - Address family of Internet
seraddr.sin_port=htons(9877);
inet_pton(AF_INET,argv[1],&seraddr.sin_addr);
connect(sockfd,(struct sockaddr*)&seraddr,sizeof(seraddr)); // connect to server( with server
address)
for(;;)
{
scanf("%s",str);
sline=str;
cline=str1;
write(sockfd,sline,strlen(sline)); // write the line of text to server
printf("%s",sline);
read(sockfd,cline,0); // read the echoed line from server and print it on to stdout
printf("%s",cline);
exit(0);
}

Output:

Ex.No: 3 IMPLEMENTATION OF SIMPLE TCP CLIENT & SERVER

Aim:

To implement A TCP time of the day Client which establishes a TCP Connection
with a server and the server sends back to the current time and date in human-readable format.

Prepared by R.MuthuKumar 10
CS 1305 – NETWORK LAB Department of Information Technology

Introduction:

When two applications want to communicate to each other reliably, they establish a
connection and send data back and forth over that connection. This is analogous to making a
telephone call. If you want to speak to Aunt Beatrice in Kentucky, a connection is established
when you dial her phone number and she answers. You send data back and forth over the
connection by speaking to one another over the phone lines. Like the phone company, TCP
guarantees that data sent from one end of the connection actually gets to the other end and in
the same order it was sent. Otherwise, an error is reported.

TCP provides a point-to-point channel for applications that require reliable


communications. The Hypertext Transfer Protocol (HTTP), File Transfer Protocol (FTP), and
Telnet are all examples of applications that require a reliable communication channel. The
order in which the data is sent and received over the network is critical to the success of these
applications. When HTTP is used to read from a URL, the data must be received in the order in
which it was sent. Otherwise, you end up with a jumbled HTML file, a corrupt zip file, or some
other invalid information.

Definition: TCP (Transmission Control Protocol) is a connection-based protocol that provides


a reliable flow of data between two computers.

Prepared by R.MuthuKumar 11
CS 1305 – NETWORK LAB Department of Information Technology

Problem Description:

Date time client server is done under three steps.

1. Client establishes a TCP connection to server.


2. Server sends back the current time and date in a human readable format.
3. Client displays the server’s response.

Algorithm:

CLIENT

Step 1: Create a TCP socket using socket function (Internet (AF_INET) stream
(SOCK_STREAM) socket).
Step 2: If the call to socket fails, print socket creation error message.
Step 3: Set the entire structure to 0 using bzero, set the address family to AF_INET, set
the port number to 13.

Prepared by R.MuthuKumar 12
CS 1305 – NETWORK LAB Department of Information Technology

Step 4: Using connect function, establish connection with server.


Step 5: Read the server’s reply and display the result using the standard I/O fputs()
function.
Step 6: Terminate the program using exit function.

SERVER

Step 1: Create a TCP socket using socket function (Internet (AF_INET) stream
(SOCK_STREAM) socket).
Step 2: If the call to socket fails, print socket creation error message.
Step 3: Set the entire structure to 0 using bzero, set the address family to AF_INET, set
the port number to 13.
Step 4: Bind server’s well known port to socket by filling in a internet Socket address
structure and calling bind.
Step 5: By calling listen, convert the socket to listening socket.
Step 6: Accept the client connection and send the current time and date.
Step 7: Terminate the program using close function.
.
CLIENT PROGRAM

#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<unistd.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>

#define MAXLINE 4096

main(int argc,char **argv)


{
int sockfd,connfd,n;
char cline[MAXLINE];
struct sockaddr_in seraddr;
if(argc!=2)
{
printf("Error");
exit(0);
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("Socket Error");
exit(0);
}
bzero(&seraddr,sizeof(seraddr));

Prepared by R.MuthuKumar 13
CS 1305 – NETWORK LAB Department of Information Technology

seraddr.sin_family=AF_INET;
seraddr.sin_port=htons(8068);
if(inet_pton(AF_INET,argv[1],&seraddr.sin_addr)<=0)
{
printf("inet_pton Error for %s",argv[1]);
exit(0);
}
if(connect(sockfd,(struct sockaddr *)&seraddr,sizeof(seraddr))>0)
{
printf("Connect Error");
exit(0);
}
while((n=read(sockfd,cline,MAXLINE))>0)
{
cline[n]=0;
if(fputs(cline,stdout)==EOF)
{
printf("Error");
exit(0);
}
if(n<0)
{
printf("Read Error");
exit(0);
}
}
}

SERVER PROGRAM
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define MAXLINE 4096
main(int argc,char **argv)
{
int listenfd,connfd;
socklen_t len;
struct sockaddr_in seraddr,cliaddr;
time_t ticks;
char buff[MAXLINE];
listenfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&seraddr,sizeof(seraddr));
seraddr.sin_family=AF_INET;
seraddr.sin_addr.s_addr=inet_addr("180.100.100.4");
Prepared by R.MuthuKumar 14
CS 1305 – NETWORK LAB Department of Information Technology

seraddr.sin_port=htons(8068);
bind(listenfd,(struct sockaddr *) &seraddr,sizeof(seraddr));
listen(listenfd,1024);
for(;;)
{
len=sizeof(cliaddr);
connfd=accept(listenfd,(struct sockaddr *) &cliaddr,&len);
ticks=time(NULL);
snprintf(buff,sizeof(buff),"% 24s\r\n",ctime(&ticks));
write(connfd,buff,strlen(buff));
close(connfd);
}
}

OUTPUT

[rmk@INSAT3EL rmk]$ cc -o dc DayClient.c


[rmk@INSAT3EL rmk]$ ./dc 180.100.100.4
Thu Jan 8 12:12:54 2009
[rmk@INSAT3EL rmk]$

Prepared by R.MuthuKumar 15
CS 1305 – NETWORK LAB Department of Information Technology

Ex.No:4 SIMULATION OF SLIDING WINDOW PROTOCOL

Aim:

To implement the concept of Sliding window Protocol using C.

Problem Description:

SWP (Sliding Window Protocol) a connection-less protocol. It allows data to be sent in


one direction between a pair of protocol entities, subject to a maximum number of
unacknowledged messages. If SWP is operated with a window size of 1, it is equivalent to the
Alternating Bit Protocol.

The protocol simulation shows a time-sequence diagram with transmitting and


receiving protocol entities, and a communications medium that carries messages. The
transmitter simply sends messages numbered DT(0), DT(1), etc. Once sequence numbers reach
a maximum number (like 7), they wrap back round to 0. The content of messages is not
explicitly identified. An acknowledgement AK(n) means that the DT message numbered n is
the next one expected (i.e. all messages up to but not including this number have been
received). Since sequence numbers wrap round, an acknowledgement with sequence number 1
refers to messages 0, 1, 7, 6, etc. Note that if a DT message is received again due to re-
transmission, it is acknowledged but discarded.

The protocol has a maximum number of messages that can be sent without
acknowledgement. If this window becomes full, the protocol is blocked until an
acknowledgement is received for the earliest outstanding message. At this point the transmitter
is clear to send more messages.

Algorithm:

Step 1: Get the size of slider window.


Step 2: Print the frames by showing the frames to be passed.
Step 3: Get the acknowledgement number.
Step 4: Mark the sliding window accordingly.
Step 5: If yes said to continue ask number and do the same.
Step 6: Else stop the program.

PROGRAM

Sliding window server


#include<stdio.h>
#include<string.h>
#include<netinet/in.h>

Prepared by R.MuthuKumar 16
CS 1305 – NETWORK LAB Department of Information Technology

#include<sys/socket.h>
#include<sys/types.h>
#include<unistd.h>
#define SA struct sockaddr
#define MAXLINE 4096
int main(int argc,char **argv)
{
int port,listenfd,connfd,i,j,len,ack,n;
char a[MAXLINE],b[MAXLINE];
socklen_t clilen;
struct sockaddr_in servaddr,cliaddr;
printf("enter the port number\n");
scanf("%d",&port);
listenfd=socket(AF_INET,SOCK_STREAM,0);
if(listenfd<0)
{
printf("socket error\n");
exit(0);
}
printf("socket created\n");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=inet_addr("180.100.100.4");
servaddr.sin_port=htons(port);
bind(listenfd,(SA*)&servaddr,sizeof(servaddr));
listen(listenfd,1024);
printf("socket listened\n");
for(;;)
{
clilen=sizeof(cliaddr);
connfd=accept(listenfd,(SA*)&cliaddr,&clilen);
if(connfd<0)
{
printf("accept error\n");
exit(0);
}
printf("socket connection accepted\n");
j=read(connfd,a,MAXLINE);
i=0;
printf("%s",a);
n=strlen(a);
printf("socket connection accepted\n");
printf("enter acknowledgement number");
scanf("%d",&ack);
len=0;
for(i=0;i<n;i++)
{

Prepared by R.MuthuKumar 17
CS 1305 – NETWORK LAB Department of Information Technology

b[len]=a[i-1];
len++;
if(i%ack==0)
{
b[len]='*';
len++;
}
}
b[len]='\0';
printf("%s\n",b);
write(connfd,b,strlen(b));
close(connfd);

}
}

OUTPUT:

[rmk@INSAT3EL rmk]$ cc -o ser swserver.c


[rmk@INSAT3EL rmk]$ ./ser
enter the port number
6578
socket created
socket listened
socket connection accepted
12345678909876554socket connection accepted
enter acknowledgement number3
123*456*789*098*765*54

Sliding window client

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#define MAXLINE 4096
#define SA struct sockaddr
int main(int argc,char **argv)
{
struct sockaddr_in servaddr;
int sockfd;
int port,n,i,j;
char a[MAXLINE],b[MAXLINE];
if(argc!=2)
{
Prepared by R.MuthuKumar 18
CS 1305 – NETWORK LAB Department of Information Technology

printf("usage error\n");
exit(0);
}
printf("enter the port number\n");
scanf("%d",&port);
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
{
printf("socket creation error\n");
exit(0);
}
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(port);
if(inet_pton(AF_INET,argv[1],&servaddr.sin_addr)<0)
{
printf("inet_pton error for %s\n",argv[1]);
exit(0);
}
if(connect(sockfd,(SA*)&servaddr,sizeof(servaddr))<0)
{
printf("connect error\n");
exit(0);
}
printf("enter the data\n");
scanf("%s",a);
write(sockfd,a,MAXLINE);
n=read(sockfd,b,MAXLINE);
if(n>0)
printf("%s",b);
close(sockfd);
}

OUTPUT:
[rmk@INSAT3EL rmk]$ cc -o cli swclient.c
[rmk@INSAT3EL rmk]$ ./cli 180.100.100.4
enter the port number
6578
enter the data
12345678909876554
123*456*789*098*765*54[rmk@INSAT3EL rmk]$

Prepared by R.MuthuKumar 19
CS 1305 – NETWORK LAB Department of Information Technology

Ex.No: 5 SIMULATION OF DOMAIN NAME SYSTEM ( DNS )

Aim:

To write a C program to implement of Domain Name System (DNS).

Introduction:

The Domain Name System (DNS) is the method by which Internet addresses in
mnemonic form such as sunc.scit.wlv.ac.uk are converted into the equivalent numeric IP
address such as 134.220.4.1. To the user and application process this translation is a service
provided either by the local host or from a remote host via the Internet. The DNS server (or
resolver) may communicate with other Internet DNS servers if it cannot translate the address
itself.

Problem Description:

The Domain Name System (DNS) is basically a large database which resides on
various computers and it contains the names and IP addresses of various hosts on the internet
and various domains. The Domain Name System is used to provide information to the Domain
Name Service to use when queries are made. The service is the act of querying the database,
and the system is the data structure and data itself. The Domain Name System is similar to a
file system in UNIX or DOS starting with a root. Branches attach to the root to create a huge
set of paths. Each branch in the DNS is called a label. Each label can be 63 characters long, but
most are less. Each text word between the dots can be 63 characters in length, with the total
domain name (all the labels) limited to 255 bytes in overall length. The domain name system
database is divided into sections called zones. The name servers in their respective zones are
responsible for answering queries for their zones. A zone is a sub tree of DNS and is
administered separately. There are multiple name servers for a zone. There is usually one
primary name server and one or more secondary name servers. A name server may be
authoritative for more than one zone.

DNS names are assigned through the Internet Registries by the Internet Assigned
Number Authority (IANA). The domain name is a name assigned to an internet domain. For
example, mycollege.edu represents the domain name of an educational institution. The names
microsoft.com and 3Com.com represent the domain names at those commercial companies.
Naming hosts within the domain is up to individuals administer their domain.

Access to the Domain name database is through a resolver which may be a program or
part of an operating system that resides on users workstations. In UNIX the resolver is accessed
by using the library functions "gethostbyname" and "gethostbyaddr". The resolver will send
requests to the name servers to return information requested by the user. The requesting
computer tries to connect to the name server using its IP address rather than the name.

Prepared by R.MuthuKumar 20
CS 1305 – NETWORK LAB Department of Information Technology

Algorithm:

Step 1: The Address Resolution Protocol (ARP) module maps address in the network
layer to the corresponding address in the Link layer.
Step 2: The link layer address is network technology specific. For Example, Ethernet
address is six byte wide.
Step 3: Network Configuration can change as host computers join or leave the
network. Luckily, ARP mapping is dynamic. ARP uses the link layers
broadcasting capability to query the network. The ARP module then caches
replies for later use.
Step 4: The Reverse Address Resolution Protocol (RARP) maps a link layer Address
Such as a name of website into an IP Address.
Step 5: As before conversion process depends on the link layer technology.

PROGRAM

#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<fcntl.h>
#include<netinet/in.h>
#include<sys/socket.h>
main()
{
int sd,len=0,sz,i,k=0,port1,port2,port3;
struct sockaddr_in s1,s2,s3;
char str[30]=" ",string[30]=" ",addr[30]=" ",status[10]=" ";
sd=socket(AF_INET,SOCK_DGRAM,0);
s1.sin_port=htons(8500);
s2.sin_addr.s_addr=s3.sin_addr.s_addr=htonl(INADDR_ANY);
s2.sin_port=htons(8600);
s3.sin_port=htons(8700);
if(bind(sd,(struct sockaddr*)&s1,sizeof(s1))<0)
{
printf("Bind error\n");
return 0;
}
sz=sizeof(s2);
if(fork()==0)
while(1)
{
fcntl(sd,F_SETFL,O_NONBLOCK);
recvfrom(sd,&str,30,0,(struct sockaddr*)&s2,&sz);
if(strcmp(str,"\0"))
{

Prepared by R.MuthuKumar 21
CS 1305 – NETWORK LAB Department of Information Technology

k=2;
goto s;
}
sz=sizeof(s3);
recvfrom(sd,&str,30,0,(struct sockaddr*)&s3,&sz);
k=3;
s:if(strcmp(str,"\0"))
{
printf("%s\n",str);
strcpy(addr,str);
strcpy(str,"0");
str[str,addr,strlen(addr)-8];
for(i=0;i<9;i++)
status[i]=addr[strlen(str)+i];
for(i=0;i<10;i++)
{
if(!strcmp(str,ip[i]))
{
strcpy(string,eth[i]);
strcat(string,"to reply");
if(!strcmp(status,"request"))
{
if(k==2)
sendto(sd,&string,30,0,(struct sockaddr*)&s2,sizeof(s2));
else
sendto(sd,&string,30,0,(struct sockaddr*)&s3,sizeof(s3));
break;
}
}
if(!strcmp(str,eth[i]))
{
strcpy(string,ip[i]);
strcat(string,"to reply");
if(!strcmp(status,"request"))
{
if(k==2)
sendto(sd,&string,30,0,(struct sockaddr*)&s2,sizeof(s2));
else
sendto(sd,&string,30,0,(struct sockaddr*)&s2,sizeof(s3));
break;
}
}
}
}
strcpy(str,"\0");
k=0;
}
Prepared by R.MuthuKumar 22
CS 1305 – NETWORK LAB Department of Information Technology

else
while(1)
{
int flg=0;
scanf("%s",str);
for(i=0;i<10;i++)
{
if(!strcmp(str,ip[i]))
{
printf("%s to reply\n",eth[i]);
flg=1;
}
if(!strcmp(str,eth[i]))
{
printf("%s to reply\n",ip[i]);
flg=1;
}
}
if(flg)
continue;
strcat(str,"request");
sendto(sd,&str,30,0,(struct sockaddr*)&s2,sizeof(s2));
sendto(sd,&str,30,0,(struct sockaddr*)&s3,sizeof(s3));
}
close(sd);
}

OUTPUT:

[rmk@INSAT3EL rmk]$ cc dns.c


[rmk@INSAT3EL rmk]$ ./a.out

180.100.100.4
google to reply
google
180.100.100.4 to reply

Prepared by R.MuthuKumar 23
CS 1305 – NETWORK LAB Department of Information Technology

Ex.No: 13 STUDY OF NETWORK SIMULATOR (NS2)

Aim:

Use Network Simulator to create two nodes with duplex –link with CBR traffic.

Problem Description:

ns is an object oriented simulator, written in C++, with an OTcl interpreter as a


frontend. The simulator supports a class hierarchy in C++ (also called the compiled hierarchy
in this document), and a similar class hierarchy within the OTcl interpreter (also called the
interpreted hierarchy in this document). The two hierarchies are closely related to each other;
from the user’s perspective, there is a one-to-one correspondence between a class in the
interpreted hierarchy and one in the compiled hierarchy. The root of this hierarchy is the class
TclObject. Users create new simulator objects through the interpreter; these objects are
instantiated within the interpreter, and are closely mirrored by a corresponding object in the
compiled hierarchy.

The interpreted class hierarchy is automatically established through methods defined in the
class TclClass. user instantiated objects are mirrored through methods defined in the class
TclObject. There are other hierarchies in the C++ code and OTcl scripts; these other
hierarchies are not mirrored in the manner of TclObject.

Why two languages? ns uses two languages because simulator has two different kinds of things
it needs to do. On one hand, detailed simulations of protocols require a systems programming
language which can efficiently manipulate bytes, packet headers, and implement algorithms
that run over large data sets. For these tasks run-time speed is important and turn-around time
(run simulation, find bug, fix bug, recompile, re-run) is less important.

On the other hand, a large part of network research involves slightly varying parameters or
configurations, or quickly exploring a number of scenarios. In these cases, iteration time
(change the model and re-run) is more important. Since configuration runs once (at the
beginning of the simulation), run-time of this part of the task is less important.

ns meets both of these needs with two languages, C++ and OTcl. C++ is fast to run but slower
to change, making it suitable for detailed protocol implementation. OTcl runs much slower but

Prepared by R.MuthuKumar 24
CS 1305 – NETWORK LAB Department of Information Technology

can be changed very quickly (and interactively), making it ideal for simulation configuration.
ns (via tclcl) provides glue to make objects and variables appear on both langauges.

Algorithm:

Step 1: Create a simulator object.


Step 2: Open the trace and name trace file & set up tracing of namtracing in network
simulator.
Step 3: Create 2 nodes as node 0 and node 1.
Step 4: Create a duplex link between these 2 nodes.
Step 5: Create a UDP agent & attach it to the node 0.
Step 6: Create a NULL agent (a traffic sink) *attach, it to node 1.
Step 7: Create a CBR traffic source sink with packet size, interval and attach it to UDP.
Step 8: Schedule the CPR start & CBR stop events for the CBR event.
Step 9: Creates the finish procedure.

 Flushes all trace object write.


 Close trace and namtrace files.
 Execute nam.
Step 10: Run the simulator.

Prepared by R.MuthuKumar 25

Anda mungkin juga menyukai