Experiment no 1.
Objective: Program to manipulate the IP address of a system.
import java.net.*;
class InetDemo
{ public static void main(String args[])
{try
{InetAddress ia = InetAddress.getLocalHost();
System.out.println("The IP address of local host is "+ia);
ia=InetAddress.getByName(args[0]);
System.out.println("the IP address of "+args[0]+"is"+ia);
}
catch(UnknownHostException ue)
{
System.out.println("There is an error "+ue);
}
}
}
Experiment no. 2
Objective:Program to obtain the information about the (a)Host (b)Port(c)
import java.lang.* ;
import java.io.*;
import java.net.*;
class ud1
{
public static void main(String args []) throws
MalformedURLException
{ URL url = new URL("http://www.yahoo.com");
try
{
System.out.println("host name is " + url.getHost());
System.out.println("port no. is " + url.getPort());
System.out.println("protocol used is " + url.getProtocol());
}
catch (Exception e)
{ System.out.println("error"+e);
}
}
}
Experiment no 3.
Objective: Program to access daytime service from server using socket
import java.net.*;
import java.io.*;
class daytime
{
public static void main(String args[])
{
try
{
Socket daytime=new Socket("192.168.1.7",13);
System.out.println("Connection established");
daytime.setSoTimeout(2000);
BufferedReader reader=new BufferedReader(new
InputStreamReader(daytime.getInputStream()));
System.out.println("result:"+reader.readLine());
daytime.close();
}
catch(Exception ioe)
{
System.err.println("Error" +ioe);
}
}
}
Experiment no 4.
p
Objective: Program to get remote and local socket address.
import java.net.*;
import java.io.*;
class daytime
{
public static void main(String args[])
{
try
{
Socket daytime=new Socket("192.168.1.7",13);
System.out.println("Connection established");
daytime.setSoTimeout(2000);
BufferedReader reader=new BufferedReader(new
InputStreamReader(daytime.getInputStream()));
System.out.println("result:"+reader.readLine());
System.out.println("local socket address"+daytime.getLocalPort());
System.out.println("remote socket address"+daytime.getRemoteSocketAddress());
daytime.close();
}
catch(Exception ioe)
{
System.err.println("Error" +ioe);
}
}
}
Experiment no. 5
Objective: Program to find port no running on server.
import java.net.*;
import java.io.*;
public class LocalPortScanner
{
public static void main(String args[])
{
for(int port=1024;port<=65535;port++)
{
try
{
ServerSocket server=new ServerSocket(port);
}
catch(IOException e)
{
System.out.println("There is a server on port"+port);
}
}
}
}
Experiment no 4
Objective: Program to read the source code of the web page
import java.lang.*;
import java.io.*;
import java.net.*;
class urld
{
public static void main(String args[]) throws MalformedURLException
{
try
{URL url=new URL("http://www.google.com");
URLConnection urlcon=url.openConnection();
InputStream ip=urlcon.getInputStream();
boolean flag=true;
while(flag)
{int a=ip.read();
if(a==-1)
{flag=false;
}
else
{
char c=(char)a;
System.out.print(c);
}
}
ip.close();
}
catch(Exception e)
{
System.out.println("error"+e);
}
}
}
Experiment no 7.
Objective: Program to create socket for sending and receiving data
Server
import java.net.*;
import java.io.*;
public class server {
public static void main(String args[]) {
int port = 4917; // just a random port. make sure you enter something between 1025 and
65535.
try {
ServerSocket ss = new ServerSocket(port); /* create a server socket and bind it to the
above port number.*/
System.out.println("Waiting for a client...");
Socket socket = ss.accept(); // make the server listen for a connection, and let you know
when it gets one.
System.out.println("Got a client :) ... ");
System.out.println();
// Get the input and output streams of the socket, so that you can receive and send data to
the client.
InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
// Just converting them to different streams, so that string handling becomes easier.
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);
String line = null;
while(true) {
line = in.readUTF(); // wait for the client to send a line of text.
System.out.println(" client just sending the line : " + line);
line=keyboard.readLine();
System.out.println("I'm sending it ..."+line);
out.writeUTF(line); // send the same line back to the client.
System.out.println("Waiting for the next line...");
System.out.println();
ss.close();
}
} catch(Exception x) {
System.out.println("Exception caught"+x);
}
}
}
Client
import java.net.*;
import java.io.*;
public class Client {
public static void main(String[] ar) {
try {
Socket socket = new Socket("192.168.1.7",4917); // create a socket with the server's IP
address and server's port.
System.out.println("Yes! I just got hold of the program.");
// Get the input and output streams of the socket, so that you can receive and send data to
the client.
InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();
// Just converting them to different streams, so that string handling becomes easier.
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);
// Create a stream to read from the keyboard.
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
String line = null;
System.out.println("Type in something and press enter. Will send it to the server and tell
ya what it thinks.");
System.out.println();
while(true) {
line = keyboard.readLine(); // wait for the user to type in something and press enter.
System.out.println("Sending this line to the server...");
out.writeUTF(line); // send the above line to the server.
out.flush(); // flush the stream to ensure that the data reaches the other end.
line = in.readUTF(); // wait for the server to send a line of text.
System.out.println("The server was very polite. It sent me this : " + line);
System.out.println("Looks like the server is pleased with us. Go ahead and enter more
lines.");
System.out.println();
}
} catch(Exception x) {
System.out.println("exception caught"+x);;
}
}
}