UDP Program (DNS CLIENT-SERVER)
AIM : To create a client server program to Domain Name System using the UDP protocol client server.
ALGORITHM
Server
- Declare the necessary arrays and variables.
- Set server port address using socket().
- Get the current message.
- Connect to the client.
- Stop the process.
Client
- Set the client machine address.
- Connect to the server.
- Read from the server the current message.
- Display the current message.
- Close the connection.
PROGRAM:
UDPclient
import java .io.*;
import java.net.*;
classUDPclient
{
public static DatagramSocket ds;
public static intclientport=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”);
InetAddressia=InetAddress.getLocalHost();
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);
}
}
}
UDP server
import java.io.*;
import java.net.*;
classUDPserver
{
public static DatagramSocket ds;
public static byte buffer[]=new byte[1024];
public static intclientport=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));
InetAddressia=InetAddress.getLocalHost();
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);
InetAddressib=InetAddress.getByName(psx);
System.out.println(“Server output:”+ib);
String str=dis.readLine();
if(str.equals(“end”))
break;
buffer=str.getBytes();
ds.send(new DatagramPacket(buffer,str.length(),ia,serverport));
}
}
}
OUTPUT:
UDPclient
C:\Program Files\Java\jdk1.6.0\bin>javac UDPclient.java
C:\Program Files\Java\jdk1.6.0\bin>java UDPclient
Server waiting
Client:www.yahoo.com
UDPserver
C:\Program Files\Java\jdk1.6.0\bin>javac UDPserver.java
C:\Program Files\Java\jdk1.6.0\bin>java UDPserver
Press ctrl+c to quit the program
Client:www.yahoo.com
Server output:www.yahoo.com/106.10.170.115
RESULT:
Thus client server program to Domain Name System using the UDP protocol client server has been executed and verified successfully.
🔥77 Views