0% found this document useful (0 votes)
65 views2 pages

Ajp G Exp10

1. The document demonstrates using the InetAddress class in Java to get the local host address, lookup a website address, and get all host addresses for a domain name. 2. It also provides code for a UDP chat client and server program that allows two applications on the same machine to exchange messages over the network using datagram packets. The client and server create DatagramSocket objects to send and receive byte buffers containing chat messages and determine when to end the session. 3. Running the programs produces output showing the client and server exchanging greetings and goodbye messages before terminating when the client sends the "END" message.

Uploaded by

Nashrah Ansari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views2 pages

Ajp G Exp10

1. The document demonstrates using the InetAddress class in Java to get the local host address, lookup a website address, and get all host addresses for a domain name. 2. It also provides code for a UDP chat client and server program that allows two applications on the same machine to exchange messages over the network using datagram packets. The client and server create DatagramSocket objects to send and receive byte buffers containing chat messages and determine when to end the session. 3. Running the programs produces output showing the client and server exchanging greetings and goodbye messages before terminating when the client sends the "END" message.

Uploaded by

Nashrah Ansari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Expermient no 10

1)Demonstrate InetAddress.
import java.net.*;
class InetAddressTest
{
public static void main(String args[]) throws UnknownHostException
{
InetAddress Address = InetAddress.getLocalHost();
System.out.println(Address);
Address = InetAddress.getByName("google.com");
System.out.println(Address);
InetAddress SW[] = InetAddress.getAllByName("www.nba.com");
for (int i=0; i<SW.length; i++)
System.out.println(SW[i]);
}
}

Output:
F:\Java\jdk1.6.0_24\bin>java InetAddressTest
YOGITA/172.16.93.185
google.com/173.194.72.113
www.nba.com/128.241.216.216
www.nba.com/128.241.216.208

2)//UdpChatserver.java
Code:
import java.io.*;
import java.net.*;
public class UdpChatServer
{

public static DatagramSocket ds;


public static int clientport=789,serverport=790;
public static void main(String args[]) throws Exception
{
byte buffer[] = new byte[10000];
byte bufferS[] = new byte[10000];
int ret=0;
String temp="";
ds=new DatagramSocket(serverport);
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());
if (psx.equalsIgnoreCase("END"))
{
buffer ="BYE".getBytes();
int len="BYE".length();
ds.send(new DatagramPacket(buffer,len,ia,clientport));
break;
}
System. out.print("Client : "+psx+"\n");
System.out.print("Server : ");
String s=dis.readLine();
bufferS =s.getBytes();
ds.send(new DatagramPacket(bufferS,s.length(),ia,clientport));
}}}
First Execute Udpcharserver.java
F:\Java\jdk1.6.0_24\bin>javac UdpChatServer.java
F:\Java\jdk1.6.0_24\bin>java UdpChatServer

//UDPchatclient.java
Code:
import java.io.*;
import java.net.*;
public class UdpChatClient
{
public static DatagramSocket ds;
public static byte buffer[]=new byte[10000];
public static byte bufferR[]=new byte[5000];
public static int clientport=789,serverport=790;
public static int a=0;
public static void main(String args[]) throws Exception
{
ds=new DatagramSocket(clientport);
BufferedReader dis = new BufferedReader(new InputStreamReader(System.in));
InetAddress ia = InetAddress.getByName("localhost");
while(true)
{
System.out.print("Client : ");
String str =dis.readLine();
buffer =str.getBytes();
ds.send(new DatagramPacket(buffer,str.length(),ia,serverport));

DatagramPacket p =new DatagramPacket(bufferR,bufferR.length);


ds.receive(p);
String psx = new String(p.getData(),0,p.getLength());
System.out.print("Server : "+psx+"\n");

if (psx.equalsIgnoreCase("BYE"))
{
break;
}}}}

Then, Executeb UdpChatCleint.java


OutPut:
F:\Java\jdk1.6.0_24\bin>javac UdpChatClient.java
F:\Java\jdk1.6.0_24\bin>java UdpChatClient
Client : Vidyalankar
Server : hello students
Client : BYE
Server : BYE
F:\Java\jdk1.6.0_24\bin>javac UdpChatServer.java
F:\Java\jdk1.6.0_24\bin>java UdpChatServer
Client : Vidyalankar
Server : hello students
Client : BYE
Server : BYE

You might also like