0% found this document useful (0 votes)
18 views

CN Lab

Lab manual

Uploaded by

THALA DHONI
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)
18 views

CN Lab

Lab manual

Uploaded by

THALA DHONI
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/ 21

EX.NO:1 Learn to use commands like tcpdump, netstat, ifconfig, nslookup and traceroute.

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.*;

import java.awt.image.BufferedImage; import

java.io.ByteArrayOutputStream; import

java.io.File;

import java.io.IOException; import

javax.imageio.ImageIO;

public class Client

public static void main(String args[]) throws Exception

Socket soc;

BufferedImage img = null;

soc=new

Socket("localhost",4000);

System.out.println("Client is running.

");

try {

System.out.println("Reading image from disk. ");


img = ImageIO.read(new File("digital_image_processing.jpg"));

ByteArrayOutputStream baos = new ByteArrayOutputStream();

ImageIO.write(img, "jpg", baos);

baos.flush();

byte[] bytes = baos.toByteArray(); baos.close();

System.out.println("Sending image to server.");

OutputStream out = soc.getOutputStream();

DataOutputStream dos = new DataOutputStream(out);

dos.writeInt(bytes.length);

dos.write(bytes, 0, bytes.length);

System.out.println("Image sent to server. ");

dos.close();

out.close();

catch (Exception e)

System.out.println("Exception: " + e.getMessage());

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);

System.out.println("Server Waiting for image");

socket=server.accept(); System.out.println("Client connected.");

InputStream in = socket.getInputStream();

DataInputStream dis = new DataInputStream(in);

int len = dis.readInt();

System.out.println("Image Size: " + len/1024 + "KB"); byte[] data = new byte[len];

dis.readFully(data);

dis.close();

in.close();

InputStream ian = new ByteArrayInputStream(data);

BufferedImage bImage = ImageIO.read(ian);

JFrame f = new JFrame("Server");

ImageIcon icon = new ImageIcon(bImage);

JLabel l = new JLabel();

l.setIcon(icon);

f.add(l);

f.pack();

f.setVisible(true);

}}
Ex.No: 3 Applications using TCP sockets like: Echo client and echo server,

Chat and File Transfer

PROGRAM

Echo client and echo server

Client:

import java.net.*;

import java.io.*;

public class eclient

public static void main (String args[])

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){

System.out.println("Socket Closed p");}}}

SERVER:

import java.net.*;

import java.io.*;

public class eserver

public static void main (String args[])

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

public static void main(String args[]) throws Exception

Socket s=new Socket("localhost",3333);

DataInputStream din=new DataInputStream(s.getInputStream());

DataOutputStream dout=new DataOutputStream(s.getOutputStream());

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

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

public static void main(String args[]) throws Exception

ServerSocket ss=new ServerSocket(3333);

Socket s=ss.accept();

DataInputStream din=new DataInputStream(s.getInputStream());

DataOutputStream dout=new DataOutputStream(s.getOutputStream());

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

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;

public class FileClient

public static void main(String[] args) throws Exception

Socket socket = new Socket(InetAddress.getByName("localhost"), 5000);

byte[] contents = new byte[10000];

FileOutputStream fos = new FileOutputStream("e:\\Bookmarks1.html");

BufferedOutputStream bos = new BufferedOutputStream(fos);

InputStream is = socket.getInputStream();

int bytesRead = 0;

while((bytesRead=is.read(contents))!=-1)

bos.write(contents, 0, bytesRead);

bos.flush();

socket.close();

System.out.println("File saved successfully!");

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;

public class FileServer

public static void main(String[] args) throws Exception

ServerSocket ssock = new ServerSocket(5000);

Socket socket = ssock.accept();

InetAddress IA = InetAddress.getByName("localhost");

File file = new File("e:\\ragu.txt");

FileInputStream fis = new FileInputStream(file);

BufferedInputStream bis = new BufferedInputStream(fis);

OutputStream os = socket.getOutputStream();

byte[] contents;

long fileLength = file.length();

long current = 0;

long start = System.nanoTime();

while(current!=fileLength){

int size = 10000;

if(fileLength - current >= size)

current += size;

else{

size = (int)(fileLength - current);

current = fileLength;
}

contents = new byte[size];

bis.read(contents, 0, size);

os.write(contents);

System.out.print("Sending file ... "+(current*100)/fileLength+"% complete!");

os.flush();

socket.close();

ssock.close();

System.out.println("File sent succesfully!");

}}
OUTPUT:

CLIENT:

SERVER:
Ex.No: 4 Simulation of DNS using UDP Sockets

PROGRAM

import java.net.*;

import java.io.*;

import java.util.*;

public class dns

public static void main (String args[])

int n;

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

do

System.out.println("\n menu:\n1.DNS 2.ReverseDns 3.EXIT\n");

System.out.println("\n enter your choice");

n=Integer.parseInt(System.console().readLine());

if(n==1)

try

System.out.println("\n enter the host name");

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

public static void main (String args[])

try

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

Socket cl=new Socket("127.0.0.1",139);

DataInputStream din=new DataInputStream(cl.getInputStream());

DataOutputStream dout= new DataOutputStream(cl.getOutputStream());

System.out.println("enter the logical address(ip):");

String str1=in.readLine()

dout.writeBytes(str1+'\n');

String str=din.readLine();

System.out.println("the physical address is "+str);

cl.close();}

catch(Exception e){

System.out.println(e);}

}}

SERVER:

import java.io.*;

import java.util.*;
import java.net.*;

class serverar

public static void main (String args[])

ServerSocket obj = new ServerSocket(139);

Socket obj1=obj.accept();

while(true){

DataInputStream din=new DataInputStream(obj1.getInputStream());

DataOutputStream dout=new DataOutputStream (obj1.getOutputStream());

String str= din.readLine();

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:

You might also like