0% found this document useful (0 votes)
33 views5 pages

SYSTEMSLA

This document contains code snippets for several Java programs that use the InetAddress class to work with IP addresses and hostnames. The programs include: 1. Printing the IP address of a given hostname. 2. Looking up all IP addresses for a given hostname. 3. Finding the local machine's IP address. 4. Finding all IP addresses mapped to a given domain name. 5. Finding the hostname for a given IP address.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views5 pages

SYSTEMSLA

This document contains code snippets for several Java programs that use the InetAddress class to work with IP addresses and hostnames. The programs include: 1. Printing the IP address of a given hostname. 2. Looking up all IP addresses for a given hostname. 3. Finding the local machine's IP address. 4. Finding all IP addresses mapped to a given domain name. 5. Finding the hostname for a given IP address.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Q-Write a program that prints the IP address of www.dei.ac.

in using the

methods available in InetAddress class.

import java.net.InetAddress;

import java.net.UnknownHostException;

public class IPAddressPrinter { public static void main(String[] args) {

// Specify the host name

String hostName = "www.dei.ac.in";

try { // Get the InetAddress object by providing the host name

InetAddress address = InetAddress.getByName(hostName);

// Get and print the IP address

String ipAddress = address.getHostAddress();

System.out.println("IP Address of " + hostName + ": " + ipAddress); }

catch (UnknownHostException e) {

// Handle the case where the host name is not recognized

System.out.println("Unable to find IP address for " + hostName);

e.printStackTrace();

} }}

Q-Looking up Internet Addresses.

import java.net.InetAddress;

import java.net.UnknownHostException;

import java.util.Scanner;

public class InternetAddressLookup {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in); System.out.print("Enter the host name: ");

String hostName = scanner.nextLine();

try { // Get the InetAddress object by providing the host name

InetAddress[] addresses = InetAddress.getAllByName(hostName);

System.out.println("IP Addresses for " + hostName + ":");

for (InetAddress address : addresses) {

System.out.println(address.getHostAddress());

} } catch (UnknownHostException e) {

// Handle the case where the host name is not recognized

System.out.println("Unable to find IP addresses for " + hostName);

e.printStackTrace(); } finally { scanner.close(); } }}


Q-Write a program to find the address of the local machine.

import java.net.InetAddress;

import java.net.UnknownHostException;

public class LocalMachineAddress {

public static void main(String[] args) {

try {

// Get the local host address

InetAddress localHost = InetAddress.getLocalHost();

// Print the local host address

System.out.println("Local Machine Address: " + localHost.getHostAddress());

} catch (UnknownHostException e) {

// Handle the case where the local host address cannot be determined

System.out.println("Unable to determine the local machine address");

e.printStackTrace(); } }}

Q-Write a program to find all the IP addresses mapped to a domain name.

import java.net.InetAddress;

import java.net.UnknownHostException;

import java.util.Scanner;

public class DomainToIPAddresses {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the domain name: ");

String domainName = scanner.nextLine();

try { // Get all IP addresses associated with the specified domain name

InetAddress[] addresses = InetAddress.getAllByName(domainName);

System.out.println("IP Addresses for " + domainName + ":");

for (InetAddress address : addresses) {

System.out.println(address.getHostAddress()); }

} catch (UnknownHostException e) {

// Handle the case where the domain name is not recognized

System.out.println("Unable to find IP addresses for " + domainName);

e.printStackTrace(); } finally { scanner.close();

} }}
Q-Write program to find the hostname given the IP address.

import java.net.InetAddress;

import java.net.UnknownHostException;

import java.util.Scanner;

public class IPAddressToHostname {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the IP address: ");

String ipAddress = scanner.nextLine();

try { // Get the InetAddress object by providing the IP address

InetAddress address = InetAddress.getByName(ipAddress);

// Get and print the hostname

String hostname = address.getHostName();

System.out.println("Hostname for " + ipAddress + ": " + hostname);

} catch (UnknownHostException e) {

// Handle the case where the IP address is not recognized

System.out.println("Unable to find hostname for " + ipAddress);

e.printStackTrace(); } finally {

scanner.close(); } }}

X. Write a program to implement a TCP echo client. XI. Write a program to implement the TCP echo Server.
import java.net.*; import java.net.*;
import java.io.*; import java.io.*;
public class TCPEchoServer { public class TCPEchoClient {
/*** @param args the command line /**
arguments*/ * @param args the command line arguments
public static void main(String[] */
args) throws Exception{ public static void main(String[] args)
// TODO code application logic here throws Exception{
String clientSentence; // TODO code application logic here
String capSentence; String Sentence;
ServerSocket WelcomeSoc= new String modifiedSentence;
ServerSocket(60000); BufferedReader inFromUser= new
Socket ConSoc= WelcomeSoc.accept(); BufferedReader(new
System.out.println("Server Started"); InputStreamReader(System.in));
BufferedReader inFromClient= new Socket clientSocket= new
BufferedReader(new Socket("localhost",60000);
InputStreamReader(ConSoc.getInputStre DataOutputStream outToServer = new
am())); DataOutputStream(clientSocket.getOutputStr
DataOutputStream outToClient= new eam());
DataOutputStream(ConSoc.getOutputStre BufferedReader inFromServer= new
am()); BufferedReader(new
System.out.println("Connected"); InputStreamReader(clientSocket.getInputStr
outToClient.writeBytes("You are eam()));
Connected to Echo Server" + System.out.println("From Server:" +
"\n");while (true){ inFromServer.readLine()+ "\n");
clientSentence= try {
inFromClient.readLine(); while (true){
capSentence= Sentence= inFromUser.readLine();
clientSentence.toUpperCase(); outToServer.writeBytes(Sentence + "\n");
outToClient.writeBytes(capSentence + modifiedSentence= inFromServer.readLine();
"\n");}}} System.out.println("From Server:" +
modifiedSentence);
}}finally{
clientSocket.close();}}}

Q-VI. Write a program to output the characteristics of a given IP address. VII. Write a program to implement a TCP
daytime protocol server. VIII. Write a program to implement a TCP daytime protocol client.
import java.net.*; import java.io.*;
import java.io.*; import java.net.*;
import java.util.*; public class DateClient {
public class DateServer { /**
public static void main(String args[]) * @param args the command line arguments
throws Exception */
{ServerSocket s=new ServerSocket(5217); public static void main(String args[])
while(true) throws Exception
{System.out.println("Waiting For {Socket soc=new
Socket(InetAddress.getLocalHost(),5217);
Connection ...");
BufferedReader in=new BufferedReader(
Socket soc=s.accept(); new InputStreamReader(
DataOutputStream out=new soc.getInputStream()
DataOutputStream(soc.getOutputStream()); )
out.writeBytes("Server Date " + (new );
Date()).toString() + "\n"); System.out.println(in.readLine());
out.close(); }
soc.close(); }
}}}

Q-Write a program to implement UDP Echo Server. XIV. Write a program to implement UDP Echo Client.
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
/**
public class UDPServer { *
/** * @author vasu
* @param args the command line arguments */
*/ public class UDPClient {
public static void main(String[] args) /**
throws Exception{ * @param args the command line
DatagramSocket serverSocket = new arguments
DatagramSocket(6000); */
byte[] receiveData= new byte[1024]; public static void main(String[] args)
byte [] sendData= new byte[1024]; throws Exception {
while (true) BufferedReader inFromUser= new
{DatagramPacket receivePacket= new BufferedReader(new
DatagramPacket(receiveData, InputStreamReader(System.in));
receiveData.length); DatagramSocket clientSocket= new
serverSocket.receive(receivePacket); DatagramSocket();
String Sentence= new InetAddress IPAddress =
String(receivePacket.getData()); InetAddress.getByName("localhost");
InetAddress IPAddress= byte[] sendData = new byte[1024];
receivePacket.getAddress(); byte[] receiveData= new byte[1024];
int Port = receivePacket.getPort(); String Sentence =
String CapitalSentence inFromUser.readLine();
=Sentence.toUpperCase(); sendData = Sentence.getBytes();
sendData=CapitalSentence.getBytes();
DatagramPacket sendPacket= new DatagramPacket sendPacket= new
DatagramPacket(sendData,sendData.length,I DatagramPacket(sendData,sendData.lengt
PAddress, Port); h, IPAddress,
serverSocket.send(sendPacket); 6000);
} 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(); }
}

You might also like