5. Practical No:2(a)
Aim: Study of InetTest.
#Source Code:
import java.net.*;
class InetTest
{
public static void main(String[] args)
{
try
{
InetAddress i = InetAddress.getByName(args[0]);
System.out.println(i.getHostAddress());
}
catch(Exception e)
{
e.printStackTrace();
}
InetTest i = new InetTest();
}
}
****************************************************************
Output:
6. Practical No:2(b)
Aim: Study of InetTest1
#Source Code:
import java.net.*;
class InetTest1
{
public static void main(String [] args)
{
try
{
InetAddress i=InetAddress.getLocalHost();
System.out.println(i.getHostAddress());
}
catch(Exception e)
{
e.printStackTrace();
}
InetTest1 j1= new InetTest1();
}
}
****************************************************************
Output:
7. Practical No:2(c)
Aim: Study of InetAddressTest.
#Source Code:
import java.net.*;
class InetAddressTest
{
public static void main(String args[]) throws UnknownHostException {
InetAddress Address = InetAddress.getLocalHost();
System.out.println(Address);
Address = InetAddress.getByName("youtube.com");
System.out.println(Address);
InetAddress SW[] = InetAddress.getAllByName("www.gmail.com");
for (int i=0; i<SW.length; i++)
System.out.println(SW[i]);
}
}
****************************************************************
Output:
8. Practical No:3
Aim: Program to demonstrate various part of URL.
#Source Code:
import java.net.*;
import java.io.*;
public class URLDemo1
{
public static void main(String [] args)
{
try
{
URL url = new URL(args[0]);
System.out.println("URL is " + url.toString());
System.out.println("protocol is " + url.getProtocol());
System.out.println("authority is " + url.getAuthority());
System.out.println("file name is " + url.getFile());
System.out.println("host is " + url.getHost());
System.out.println("path is " + url.getPath());
System.out.println("port is " + url.getPort());
System.out.println("default port is "+ url.getDefaultPort());
System.out.println("query is " + url.getQuery());
System.out.println("ref is " + url.getRef());
}catch(IOException e)
{
e.printStackTrace();
}
}
}
10. Practical No:4
Aim: Connectionless Communication between server and client using
datagram.
#Source Code:
Server:
import java.io.*;
import java.net.*;
class UDPServer
{
public static void main(String args[]) throws Exception
{
DatagramSocket serverSocket = new DatagramSocket(9876);
//Server Socekt Created
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while(true)
{
DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length);
serverSocket.receive(receivePacket);
String sentence = new String(receivePacket.getData());
System.out.println("RECEIVED: " + sentence);
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
String capitalizedSentence = sentence.toUpperCase();
//Change sentence to Capital letter
sendData = capitalizedSentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,
IPAddress, port);
serverSocket.send(sendPacket);
//Send Capitalized data back to client
}
}
}
11. Client:
import java.io.*;
import java.net.*;
class UDPClient
{
public static void main(String args[]) throws Exception
{
BufferedReader inFromUser =new BufferedReader(new
InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
//Client Socket is created
InetAddress IPAddress = InetAddress.getByName("localhost");
//Gets the IP Address
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
String sentence = inFromUser.readLine();
sendData = sentence.getBytes();
//sends data
DatagramPacket sendPacket = new DatagramPacket(sendData,
sendData.length, IPAddress,9876);
clientSocket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket
(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence = new String(receivePacket.getData());
System.out.println("FROM SERVER:" + modifiedSentence);
clientSocket.close();
}