CN Lab
CN Lab
Capture ping and traceroute PDUs using a network protocol analyzer and examine.CLIENT:
Ex.No: 2 Write a HTTP web client program to download a web page using TCP sockets
PROGRAM
Client
import javax.swing.*;
import java.net.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
java.io.ByteArrayOutputStream; import
java.io.File;
javax.imageio.ImageIO;
Socket soc;
soc=new
Socket("localhost",4000);
System.out.println("Client is running.
");
try {
baos.flush();
dos.writeInt(bytes.length);
dos.write(bytes, 0, bytes.length);
dos.close();
out.close();
catch (Exception e)
soc.close();}
soc.close();
}}
Server
import java.net.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
class Server
{
public static void main(String args[]) throws Exception
ServerSocket server=null;
Socket socket;
server=new ServerSocket(4000);
InputStream in = socket.getInputStream();
dis.readFully(data);
dis.close();
in.close();
l.setIcon(icon);
f.add(l);
f.pack();
f.setVisible(true);
}}
Ex.No: 3 Applications using TCP sockets like: Echo client and echo server,
PROGRAM
Client:
import java.net.*;
import java.io.*;
Socket c=null;
String line;
DataInputStream is,is1;
PrintStream os;
try{
c=new Socket("172.27.101.243",9000);
catch(Exception e){
System.out.println(e);
try{
System.out.println("input:");
is=new DataInputStream(System.in);
os=new PrintStream(c.getOutputStream());
is1=new DataInputStream(c.getInputStream());
while(true)
{
System.out.println("client:");
line=is.readLine();
os.println(line);
System.out.println("server:"+is1.readLine());
}}
catch(Exception e){
SERVER:
import java.net.*;
import java.io.*;
ServerSocket s=null;
String line;
DataInputStream is;
PrintStream ps;
Socket c=null;
try
s=new ServerSocket(9000);
catch(Exception e)
System.out.println(e);
}
try
c=s.accept();
is=new DataInputStream(c.getInputStream());
ps=new PrintStream(c.getOutputStream());
while(true)
line=is.readLine();
ps.println(line);
System.out.println(line);
catch(Exception e)
System.out.println(e);
}
OUTPUT:
CLIENT:
SERVER:
Chat
CLIENT:
import java.net.*;
import java.io.*;
class client
String str="",str2="";
while(!str.equals("stop"))
str=br.readLine();
dout.writeUTF(str);
dout.flush();
str2=din.readUTF();
System.out.println("server says:"+str2);
dout.close();
s.close();
}}
SERVER:
import java.net.*;
import java.io.*;
class server
Socket s=ss.accept();
String str="",str2="";
while(!str.equals("stop"))
str=din.readUTF();
System.out.println("client says:"+str);
str2=br.readLine();
dout.writeUTF(str2);
dout.flush();
din.close();
s.close();
ss.close();
}
OUTPUT:
CLIENT:
SERVER:
File Transfer
CLIENT:
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
InputStream is = socket.getInputStream();
int bytesRead = 0;
while((bytesRead=is.read(contents))!=-1)
bos.write(contents, 0, bytesRead);
bos.flush();
socket.close();
SERVER:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
InetAddress IA = InetAddress.getByName("localhost");
OutputStream os = socket.getOutputStream();
byte[] contents;
long current = 0;
while(current!=fileLength){
current += size;
else{
current = fileLength;
}
bis.read(contents, 0, size);
os.write(contents);
os.flush();
socket.close();
ssock.close();
}}
OUTPUT:
CLIENT:
SERVER:
Ex.No: 4 Simulation of DNS using UDP Sockets
PROGRAM
import java.net.*;
import java.io.*;
import java.util.*;
int n;
do
n=Integer.parseInt(System.console().readLine());
if(n==1)
try
String hname=in.readLine();
InetAddress address;
address=InetAddress.getByName(hname);
System.out.println("host name:"+address.getHostName());
System.out.println("IP:"+address.getHostAddress());
}
catch(IOException ioe)
ioe.printStackTrace();
}}
if(n==2)
try
System.out.println("\nenter IP address");
String ipstr=in.readLine();
InetAddress ia=InetAddress.getByName(ipstr);
System.out.println("Hostname:"+ia.getHostName());
catch(IOException ioe)
ioe.printStackTrace();
}}
}while(!(n==3));}}
OUTPUT:
Ex.No:6 study of network simular(ns) and simulation of congestion control algorithm NS
PROGRAM:
CLIENT:
import java.io.*;
import java.util.*;
import java.net.*;
class clientar
try
String str1=in.readLine()
dout.writeBytes(str1+'\n');
String str=din.readLine();
cl.close();}
catch(Exception e){
System.out.println(e);}
}}
SERVER:
import java.io.*;
import java.util.*;
import java.net.*;
class serverar
Socket obj1=obj.accept();
while(true){
String ip[]={"165.168.80.80","165.165.79.1"};
String mac[]={"6A:08:AA:C2","8A:BC:E3:FA"};
for(int i=0;i<ip.length;i++)
if(str.equals(ip[i]))
dout.writeBytes(mac[i]+'\n');
System.out.println("mac"+mac[i]);
break;
}}
obj.close();
}}
catch(Exception e)
System.out.println(e);
}}}
OUTPUT:
CLIENT:
SERVER: