Domain Name System: Ex No:2 Program Using Simple Udp EX NO:2.i

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

Ex No:2

EX NO:2.i

PROGRAM USING SIMPLE UDP

DOMAIN NAME SYSTEM

AIM:
To write a C program to develop a DNS client server to resolve the given
hostname.

ALGORITHM:
1.
2.
3.
4.
5.
6.
7.
8.

Create a new file. Enter the domain name and address in that file.
To establish the connection between client and server.
Compile and execute the program.
Enter the domain name as input.
The IP address corresponding to the domain name is display on the screen
Enter the IP address on the screen.
The domain name corresponding to the IP address is display on the screen.
Stop the program.

Program :
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<netdb.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
int main(int argc,char *argv[1])
{
struct hostent *hen;
if(argc!=2)
{
fprintf(stderr,"Enter the hostname \n");
exit(1);
}
hen=gethostbyname(argv[1]);
if(hen==NULL)
{
fprintf(stderr,"Host not found \n");
}
printf("Hostname is %s \n",hen->h_name);
printf("IP address is %s \n",inet_ntoa(*((struct in_addr *)hen->h_addr)));
}

Output
[cse5062@linuxserver ~]$cc dns.c o c
[cse5062@linuxserver ~]$./c www.yahoo.com
Host name is www.yahoo-ht3.akadns.net
IP address is 87.248.113.14
RESULT:
Thus the above program udp performance using domain name server was
executed and successfully

EX NO:2.ii

PROGRAM USING UDP SOCKET

AIM:
To write a client-server application for chat using UDP
ALGORITHM: CLIENT
1. Include necessary package in java
2. To create a socket in client to server.
3. The client establishes a connection to the server.
4. The client accept the connection and to send the data from client to server and vice
versa
5. The client communicate the server to send the end of the message
6. Stop the program.
ALGORITHM: SERVER
1. Include necessary package in java
2. To create a socket in server to client
3. The server establishes a connection to the client.
4. The server accept the connection and to send the data from server to client and vice
versa
5. The server communicate the client to send the end of the message
6. Stop the program.
Program :

UDPserver.java
import java.io.*;
import java.net.*;
class UDPserver
{
public static DatagramSocket ds;
public static byte buffer[]=new byte[1024];
public static int clientport=789,serverport=790;
public static void main(String args[])throws Exception
{
ds=new DatagramSocket(clientport);
System.out.println("press ctrl+c to quit the program");
BufferedReader dis=new BufferedReader(new InputStreamReader(System.in));

InetAddress ia=InetAddress.getByName("localhost");
while(true)
{
DatagramPacket p=new DatagramPacket(buffer,buffer.length);
ds.receive(p);
String psx=new String(p.getData(),0,p.getLength());
System.out.println("Client:" + psx);
System.out.println("Server:");
String str=dis.readLine();
if(str.equals("end"))
break;
buffer=str.getBytes();
ds.send(new DatagramPacket(buffer,str.length(),ia,serverport));
}
}
}

UDPclient.java
import java .io.*;
import java.net.*;
class UDPclient
{
public static DatagramSocket ds;
public static int clientport=789,serverport=790;
public static void main(String args[])throws Exception
{
byte buffer[]=new byte[1024];
ds=new DatagramSocket(serverport);
BufferedReader dis=new BufferedReader(new InputStreamReader(System.in));
System.out.println("server waiting");
InetAddress ia=InetAddress.getByName("10.0.200.36");
while(true)
{
System.out.println("Client:");
String str=dis.readLine();
if(str.equals("end"))
break;
buffer=str.getBytes();
ds.send(new DatagramPacket(buffer,str.length(),ia,clientport));
DatagramPacket p=new DatagramPacket(buffer,buffer.length);
ds.receive(p);
String psx=new String(p.getData(),0,p.getLength());
System.out.println("Server:" + psx);
}
}
}

Output
Server
C:\Program Files\Java\jdk1.5.0\bin>javac UDPserver.java
C:\Program Files\Java\jdk1.5.0\bin>java UDPserver
press ctrl+c to quit the program
Client:Hai Server
Server:
Hello Client
Client:How are You
Server:
I am Fine what about you

Client
C:\Program Files\Java\jdk1.5.0\bin>javac UDPclient.java
C:\Program Files\Java\jdk1.5.0\bin>java UDPclient
server waiting
Client:
Hai Server
Server:Hello Clie
Client:
How are You
Server:I am Fine w
Client:
end

RESULT:
Thus the above program a client-server application for chat using UDP was
executed and successfully

You might also like