Networking Lab Programs (2009-10)
Networking Lab Programs (2009-10)
import java.io.*;
import java.net.*;
class hello
{
public static void main(String args[])
{
if(args.length>0)
{
try
{
URL u=new URL(args[0]);
URLConnection uc=u.openConnection();
InputStream raw=uc.getInputStream();
InputStream buffer=new BufferedInputStream(raw);
Reader r=new InputStreamReader(buffer);
int c;
while((c=r.read())!=-1) System.out.print((char)c);
}
catch(MalformedURLException e)
{
System.err.println(args[0]+ "is not a parseable URL");
}
catch(IOException e)
{ System.err.println(e);}
}
}
}
OUTPUT:
<h3>
<font face="Times New Roman,Times"><font
size=+4>Components</font></font></h3>
<blockquote>
<li>
<font face="Times New Roman,Times"><font size=+2><a
href="manual/index.html">Apa
che</a></font></font></li>
<li>
<font face="Times New Roman,Times"><font size=+2><a href="/jservdocs/">JServ</a>
</font></font></li>
*/ Ex.No:2 a Implementation of Socket Programming using TCP/IP */
Server:
import java.io.*;
import java.net.*;
class Server
{
public static void main(String []args)throws IOException
{
try
{
ServerSocket s=new ServerSocket(3128);
System.out.println("\n Waiting for clients...");
Socket c=s.accept();
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
BufferedReader br=new BufferedReader(new InputStreamReader(c.getInputStream()));
PrintWriter p=new PrintWriter(c.getOutputStream(),true);
p.println("Hello");
System.out.println("Connection Established");
while(true)
{
String msg=br.readLine();
System.out.println("\n"+msg);
System.out.println("\n Enter message:");
String inf=in.readLine();
if(inf.equals("exit")) break;
p.flush();
p.println(inf);
}
c.close();
}
catch(Exception e)
{
System.out.println("Error: "+e);
}
}
}
Client:
import java.io.*;
import java.net.*;
class Client
{
public static void main(String [] args) throws IOException
{
try
{
Socket c=new Socket("itflab105",3128);
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
BufferedReader br=new BufferedReader(new InputStreamReader(c.getInputStream()));
PrintWriter p=new PrintWriter(c.getOutputStream(),true);
String inf,msg;
System.out.println("Connection request.....");
System.out.println("Connected");
while(true)
{
msg=br.readLine();
System.out.println("\n"+msg);
System.out.println("\n Enter message:");
inf=in.readLine();
if(inf.equals("exit")) break;
p.println(inf);
}
c.close();
}
catch(Exception e)
{
System.out.println("Error: "+e);
}
}
}
/* Server Output */
hi how are u
Enter Message:
i am fine
where are u
Enter Message:
i am in Trichy
null
/* Client Output */
Hello
Enter message:
hi how are u
i am fine
Enter message:
Where are u
I am in trichy
Enter message:
Exit
/* Ex.No:2b Implementation of Socket Programming – by UDP */
SERVER:
import java.io.*;
import java.net.*;
class UServer
{
public static int serport=3128;
public static int cliport=3127;
public static int bs=1024;
public static DatagramSocket ds;
public static byte buf[]=new byte[bs];
public static byte buffer[]=new byte[bs];
public static void main(String args[]) throws IOException
{
ds=new DatagramSocket(cliport);
while(true)
{
DatagramPacket p=new DatagramPacket(buffer,buffer.length);
ds.receive(p);
System.out.println("Message for server");
System.out.println(new String(p.getData(),0,p.getLength()));
System.out.println("Enter message for client:");
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String inf=in.readLine();
if(inf.equals("exit")) break;
buf=inf.getBytes();
ds.send(new DatagramPacket(buf,inf.length(),InetAddress.getLocalHost(),serport));
}
}
}
CLIENT:
import java.io.*;
import java.net.*;
class UClient
{
public static int serport=3128;
public static int cliport=3127;
public static int bs=1024;
public static DatagramSocket ds;
public static byte buf[]=new byte[bs];
public static byte buffer[]=new byte[bs];
public static void main(String args[]) throws IOException
{
ds=new DatagramSocket(serport);
while(true)
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the message for server");
String inf=in.readLine();
if(inf.equals("exit")) break;
buffer=inf.getBytes();
ds.send(new DatagramPacket(buffer,inf.length(),InetAddress.getLocalHost(),cliport));
DatagramPacket p=new DatagramPacket(buf,buf.length);
ds.receive(p);
System.out.println("Message from server");
System.out.println(new String(p.getData(),0,p.getLength()));
}
}
}
SERVER OUTPUT:
CLIENT OUTPUT:
C:\Program Files\Java\jdk1.6.0\bin>javac UClient.java
C:\Program Files\Java\jdk1.6.0\bin>java UClient
enter the message for server
hai
Message from server
hai
enter the message for server
how are you
Message from server
I am fine
enter the message for server
exit
/* Ex.No: 03 Implementation of FTP */
Server:
import java.io.*;
import java.net.*;
class FServer
{
public static void main(String args[])throws IOException
{
ServerSocket s=new ServerSocket(3128);
Socket c;
System.out.println("\n Waiting for clients...");
c=s.accept();
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
BufferedReader br=new BufferedReader(new InputStreamReader(c.getInputStream()));
PrintWriter p=new PrintWriter(c.getOutputStream(),true);
String inf,msg;
System.out.println("Connection Established");
inf=br.readLine();
System.out.println("Client wants file:"+inf);
File ff=new File(inf);
if(ff.exists())
{
BufferedReader b=new BufferedReader(new FileReader(ff));
String u;
while((u=b.readLine())!=null) p.write(u);
}
p.close();
s.close();
}
}
Client:
import java.io.*;
import java.net.*;
class FClient
{
public static void main(String args[])throws IOException
{
Socket c=new Socket(InetAddress.getLocalHost(),3128);
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
BufferedReader br=new BufferedReader(new InputStreamReader(c.getInputStream()));
PrintWriter p=new PrintWriter(c.getOutputStream(),true);
System.out.println("Connection request.....");
System.out.println("Connected");
System.out.println("Enter the filename:");
String f=in.readLine();
p.println(f);
p.flush();
String inp;
while((inp=br.readLine())!=null)
{
System.out.println(inp);
}
c.close();
}
}
Server Output:
Client Output:
Connected
import java.net.*;
import java.io.*;
import java.util.*;
public class pingTest
{
public static void main(String[] args)
{
String pingResult = "";
try
{
Runtime r = Runtime.getRuntime();
Process p = r.exec("ping 10.1.1.3");
BufferedReader in = new BufferedReader(new
InputStreamReader(p.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
{
System.out.println(inputLine);
pingResult += inputLine;
}
in.close();
}
catch (IOException e)
{
System.out.println(e);
}
}
}
Output:
SERVER:
import java.io.*;
import java.net.*;
class echoser
{
public static void main(String args[])throws IOException
{
try
{
ServerSocket s=new ServerSocket(3128);
while(true)
{
Socket c=s.accept();
System.out.println("Connected to "+c);
BufferedReader br=new BufferedReader(new InputStreamReader(c.getInputStream()));
PrintWriter p=new PrintWriter(c.getOutputStream(),true);
System.out.println("connection established...");
String inf,msg;
do
{
inf=br.readLine();
if(inf!=null) p.println("SERVER: "+inf);
System.out.print(" Message from client :”+inf);
}
while(!inf.trim().equals("exit"));
{
if(inf.equals("exit"))
System.exit(0);
c.close( );
}}}
catch(Exception e)
{
System.out.println("Error :"+e+"\n");
}}}
Client:
import java.io.*;
import java.net.*;
class echocli
{
public static void main(String args[])throws IOException
{
try
{
Socket c=new Socket(InetAddress.getLocalHost(),3128);
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
BufferedReader br=new BufferedReader(new InputStreamReader(c.getInputStream()));
PrintWriter p=new PrintWriter(c.getOutputStream(),true);
String inf,msg;
do
{
System.out.println("Enter the message");
msg=in.readLine();
if(msg!=null)
{
p.println(msg);
}
inf=br.readLine();
System.out.println("Echo from "+inf);
}
while(!msg.trim().equals("exit"));
}
catch(Exception e)
{
System.out.println("Error :"+e+"\n");
}
}
}
OUTPUT:
Server:
Connected to Socket[addr=/127.0.0.1,port=1042,localport=3128]
connection established...
Client:
C:\Program Files\Java\jdk1.6.0\bin>javac echocli.java
import java.io.*;
import java.net.*;
class rceser
{
public static void main(String args[])throws IOException
{
ServerSocket s=new ServerSocket(3128);
System.out.println("Started "+s);
try
{
Socket c=s.accept();
try
{
System.out.println("Connection started "+c);
BufferedReader br=new BufferedReader(new InputStreamReader(c.getInputStream()));
PrintWriter p=new PrintWriter(new BufferedWriter(new
OutputStreamWriter(c.getOutputStream())),true);
String inf=br.readLine();
p.println(inf);
Process pr=Runtime.getRuntime().exec(inf);
}
finally
{
c.close();
}
}
finally
{
s.close();
}
}
}
CLIENT:
import java.io.*;
import java.net.*;
class rcecli
{
public static void main(String args[])throws IOException
{
Socket s=new Socket(InetAddress.getLocalHost(),3128);
try
{
System.out.println("Server:"+s);
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
PrintWriter p=new PrintWriter(new BufferedWriter(new
OutputStreamWriter(s.getOutputStream())),true);
System.out.println("Enter the command to be executed remotely");
String msg=in.readLine();
p.println(msg);
}
finally
{
s.close();
}
}
}
SERVER OUTPUT:
C:\Program Files\Java\jdk1.6.0\bin>javac rceser.java
C:\Program Files\Java\jdk1.6.0\bin>java rceser
Started ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=3128]
Connection started Socket[addr=/10.1.3.145,port=2996,localport=3128]
CLIENT OUTPUT:
C:\Program Files\Java\jdk1.6.0\bin>javac rcecli.java
C:\Program Files\Java\jdk1.6.0\bin>java rcecli
Server:Socket[addr=itflab105/10.1.3.145,port=3128,localport=2996]
Enter the command to be executed remotely
Mspaint
CLIENT:
import java.io.*;
import java.net.*;
public class arpclient
{
public static void main(String args [])throws IOException
{
Socket s=new Socket(InetAddress.getLocalHost(),2000);
String in;
System.out.println("Client connected to server");
BufferedReader dis=new BufferedReader(new BufferedReader(new
InputStreamReader(System.in)));
BufferedReader sin=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintStream dos=new PrintStream(s.getOutputStream());
System.out.println("Enter the IP address:");
in=dis.readLine();
dos.println(in);
System.out.println("MAC address:"+sin.readLine());
}
}
SERVER:
import java.io.*;
import java.net.*;
public class arpser
{
public static void main(String args[])throws IOException
{
ServerSocket ss=new ServerSocket(2000);
Socket s;
ARP arp=new ARP( );
String in;
int i;
System.out.println("Server started");
s=ss.accept();
BufferedReader dis=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintStream dos=new PrintStream(s.getOutputStream());
System.out.println("Client Accepted:");
in=dis.readLine();
for(i=0;i<3;i++)
{
if(in.equals(arp.phy[i]))
{
dos.println(arp.ip[i]);
break;
}
}
if(i==3)
dos.println("IP not found");
}
}
SERVER OUTPUT:
Server started
Client Accepted
CLIENT OUTPUT:
CLIENT:
import java.io.*;
import java.net.*;
public class arpclient
{
public static void main(String args[])throws IOException
{
Socket s=new Socket(InetAddress.getLocalHost(),2000);
String in;
System.out.println("Client connected to server");
BufferedReader dis=new BufferedReader(new BufferedReader(new
InputStreamReader(System.in)));
BufferedReader sin=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintStream dos=new PrintStream(s.getOutputStream());
System.out.println("Enter the MAC address:");
in=dis.readLine();
dos.println(in);
System.out.println("IP address:"+sin.readLine());
}
}
SERVER:
import java.io.*;
import java.net.*;
public class arpser
{
public static void main(String args[])throws IOException
{
ServerSocket ss=new ServerSocket(2000);
Socket s;
ARP arp=new ARP();
String in;
int i;
System.out.println("Server started");
s=ss.accept();
BufferedReader dis=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintStream dos=new PrintStream(s.getOutputStream());
System.out.println("Client Accepted:");
in=dis.readLine();
for(i=0;i<3;i++)
{
if(in.equals(arp.phy[i]))
{
dos.println(arp.ip[i]);
break;
}
}
if(i==3)
dos.println("MAC not found");
}
}
SERVER OUTPUT:
Server started
Client Accepted
CLIENT OUTPUT:
IP address: 127.0.0.1
SERVER:
import java.rmi.*;
import java.net.*;
public class server
{
public static void main(String args[])
{
try
{
serverimpl ad=new serverimpl();
Naming.rebind("server",ad);
}
catch(Exception e)
{
System.out.println("Exception "+e);
}
}
}
SERVER IMPLEMENTATION:
import java.rmi.*;
import java.rmi.server.*;
public class serverimpl extends UnicastRemoteObject implements serverintf
{
public serverimpl()throws RemoteException
{
}
public double add(double d1,double d2)throws RemoteException
{
return d1+d2;
}
}
CLIENT:
import java.rmi.*;
public class client
{
public static void main(String args[])
{
try
{
String a="rmi://"+args[0]+"/server";
serverintf addserver=(serverintf)Naming.lookup(a);
System.out.println("the first number is "+args[1]);
double d1=Double.valueOf(args[1]).doubleValue();
System.out.println("the second number is "+args[2]);
double d2=Double.valueOf(args[2]).doubleValue();
System.out.println("The sum is "+addserver.add(d1,d2));
}
catch(Exception e)
{
System.out.println("Exception "+e);
}
}
}
SERVER OUTPUT:
C:\Program Files\Java\jdk1.6.0\bin>start rmiregistry
CLIENT OUTPUT:
First number is 40
Second number is 20
Sum is 60.0
class shortpath
{
public static void main(String args[])
{
try
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
int ports[]={100,101,102,103,104,105};
DatagramSocket ds=new DatagramSocket(100);
InetAddress inet=InetAddress.getLocalHost();
String s1=null;
int count=0,z,k,I,m,p=0;
int weight[][]=new int[10][10],n,s=0,t,path[]=new int[10],label[]=new int[10];
boolean vect[]=new boolean[10];
System.out.println("Enter no.of nodes");
n=Integer.parseInt(br.readLine());
System.out.println("dest");
t=Integer.parseInt(br.readLine());
for(int i=0;i<n;i++)
for(int j=i;j<n;j++)
if(i==j)
{weight[i][j]= weight[j][i]=99;}
else
{
System.out.println("Node"+i+" to node"+j);
weight[i][j]=Integer.parseInt(br.readLine());
weight[j][i]=weight[i][j];
}}}
for(int l=0;l<n;l++)
{
label[l]=9999;vect[l]=false;
}
label[s]=0;vect[s]=true;
I=s;k=0;
for(;;)
{
m=9999;
path[k]=I;
k++;
for(int j=0;j<n;j++)
{
if(vect[j]==true)
continue;
z=weight[I][j]+label[I];
if(z<label[j])
label[j]=z;
if(label[j]>m)
continue;
m=label[j];
p=j;
}
vect[p]=true;
if(p==t) break;
I=p;
}
count=k+1;
System.out.println("The distance matrix is:");
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
System.out.print(weight[i][j]+"\t");
}
System.out.println();
}
System.out.println("The path is:");
s1=" ";
for(int x=0;x<k;x++)
{
s1=s1+path[x]+" " ;
System.out.print(path[x]+" --> ");
if(x<k-1)
s+=weight[path[x]][path[x+1]];
}
path[k]=t;
System.out.println(t);
s+=weight[path[k-1]][path[k]];
System.out.print("Shortest distance "+s+"\t\t");
s1+=t+" ";
isr.close();
s1+="1";
s1=count+" "+s1;
byte b[]=s1.getBytes();
DatagramPacket dp=new DatagramPacket(b,b.length,inet,ports[path[1]]);
System.out.println("\n\n"+s1);
ds.send(dp);
}
catch(Exception e)
{
System.out.println("Exception"+ e);
}
}
}
OUTPUT:
C:\Program Files\Java\jdk1.6.0\bin>javac shortpath.java
C:\Program Files\Java\jdk1.6.0\bin>java shortpath
Enter no.of nodes 4
Dest 1
Node0 to node1
10
Node0 to node2
2
Node0 to node3
99
Node1 to node2
99
Node1 to node3
1
Node2 to node3
1
Shortest distance :4
0231 1
while(true)
{
for(i=0,j=1;j>0;j=j-5,i++)
{
str1[i]=str.substring(start,end);
System.out.println("The sub string is "+str1[i]);
start=end;
end=end+5;
if(end>l)
end=(start-5)+j;
n=n+1;
Client:
C:\Program Files\Java\jdk1.6.0\bin>javac swindowcli.java
C:\Program Files\Java\jdk1.6.0\bin>java swindowcli
Waiting...... connected...
Enter the message welcome to networking lab.
The length is 26
The sub string is welco
Packet number= 1 ACK2
The sub string is me to
Packet number= 2
The sub string is netw
Packet number= 3 ACK4
The sub string is orkin
Packet number= 4
The sub string is g lab
Packet number= 5 ACK6