UDP Applications
a. Domain name System
// Datagram Sockets - DNS Server
// Server Program
import java.io.*;
import java.net.*;
public class DNS_Server
{
public
static void main(String as[]) throws IOException
{
DatagramSocket
ds;
DatagramPacket
dp_send;
DatagramPacket
dp_rec;
final
int buf_size = 512;
final
int port = 1500;
byte
msg_rec[] = new byte[buf_size];
byte
msg_send[] = new byte[buf_size];
try
{
ds
= new DatagramSocket(port);
dp_rec
= new DatagramPacket(msg_rec, buf_size);
ds.receive(dp_rec);
String
dname = new String(dp_rec.getData()).trim();
InetAddress
ipaddr = InetAddress.getByName(dname);
InetAddress
DA = dp_rec.getAddress();
int
destport = dp_rec.getPort();
System.out.println(ipaddr);
msg_send
= ipaddr.toString().getBytes();
dp_send = new DatagramPacket(msg_send, msg_send.length, DA, destport);
ds.send(dp_send);
}
catch(Exception
e)
{
System.out.println("Exception
" + e);
}
}
}
// Datagram Sockets - DNS Server
// Client Program
import java.io.*;
import java.net.*;
import java.util.*;
public class DNS_Client
{
public
static void main(String as[]) throws IOException
{
DatagramSocket
ds;
DatagramPacket
dp_send;
DatagramPacket
dp_rec;
final
int port = 1500;
final
int buf_size = 512;
Scanner
s = new Scanner(System.in);
byte[]
msg_send = new byte[buf_size];
byte[]
msg_rec = new byte[buf_size];
try
{
ds
= new DatagramSocket();
System.out.print("Enter
domain name : ");
String
data = s.nextLine();
msg_send
= data.getBytes();
InetAddress
DA = InetAddress.getByName("localhost");
dp_send
= new DatagramPacket(msg_send, msg_send.length, DA, port);
ds.send(dp_send);
dp_rec
= new DatagramPacket(msg_rec, buf_size);
ds.receive(dp_rec);
String
ipaddr = new String(dp_rec.getData()).trim();
System.out.println(ipaddr);
}
catch(Exception
e)
{
System.out.println("Exception
" + e);
}
}
}
/*
Output 1
server window
>javac
DNS_Server.java
>java
DNS_Server
www.drranurekha.com/160.153.137.167
client window
>javac
DNS_Client.java
>java
DNS_Client
Enter
domain name : www.drranurekha.com
www.drranurekha.com/160.153.137.167
Output 2
server window
>javac
DNS_Server.java
>java
DNS_Server
www.google.co.in/172.217.163.163
client window
>javac
DNS_Client.java
>java
DNS_Client
Enter
domain name : www.google.co.in
www.google.co.in/172.217.163.163
*/
UDP Applications
b. File Transfer
// Socket Programing – Datagram Sockets
// Client Server Communication
// File Transfer
// Server Program
import java.io.*;
import java.net.*;
import java.util.*;
public class UDPFileServer
{
public
static void main(String as[]) throws IOException
{
DatagramSocket
ds;
DatagramPacket
dp_send;
DatagramPacket
dp_rec;
final
int buf_size = 512;
final
int port = 1500;
byte
msg_rec[] = new byte[buf_size];
byte
msg_send[] = new byte[buf_size];
try
{
ds
= new DatagramSocket(port);
dp_rec
= new DatagramPacket(msg_rec, buf_size);
ds.receive(dp_rec);
String
filename = new String(dp_rec.getData()).trim();
InetAddress
DA = dp_rec.getAddress();
int
destport = dp_rec.getPort();
System.out.println("Requested
file : "+filename);
FileInputStream
fin = new FileInputStream(filename);
int
i = 0;
while(true)
{
int
ch = fin.read();
if(ch==-1)
break;
msg_send[i]=(byte)ch;
i++;
}
fin.close();
dp_send = new DatagramPacket(msg_send, msg_send.length, DA, destport);
ds.send(dp_send);
}
catch(Exception
e)
{
System.out.println("Exception
" + e);
}
}
}
// Socket Programing – Datagram Sockets
// File Transfer
// Client Program
import java.io.*;
import java.net.*;
import java.util.*;
public class UDPFileClient
{
public
static void main(String as[]) throws IOException
{
DatagramSocket
ds;
DatagramPacket
dp_send;
DatagramPacket
dp_rec;
final
int port = 1500;
final
int buf_size = 512;
Scanner
s = new Scanner(System.in);
byte[]
msg_send = new byte[buf_size];
byte[]
msg_rec = new byte[buf_size];
try
{
ds
= new DatagramSocket();
System.out.print("Enter
file name : ");
String
data = s.nextLine();
msg_send
= data.getBytes();
InetAddress
DA = InetAddress.getByName("localhost");
dp_send
= new DatagramPacket(msg_send, msg_send.length, DA, port);
ds.send(dp_send);
dp_rec
= new DatagramPacket(msg_rec, buf_size);
ds.receive(dp_rec);
String
filedata = new String(dp_rec.getData()).trim();
System.out.println(filedata);
}
catch(Exception
e)
{
System.out.println("Exception
" + e);
}
}
}
No comments:
Post a Comment
Don't be a silent reader...
Leave your comments...
Anu