Networking Lab
Networking Lab
INDEX
CONTENTS PAGE NO
1
[Link] IDENTIFY WELL KNOWN PORTS
Problem Description:
Registered Ports:
The ports ranging from 1-24 to 49151 are
not assigned or controlled by IANA. They can only be
registered with IANA to prevent duplication.
Dynamic Ports:
The ports ranging from 49152 to 65535 are
neither controlled nor registered. They can be used by
any process. These are ethereal ports.
Remote system:
Any other computer in the network with
which the local computer can communicate.
2
Program:
/* This program will identify the ports which are active also
shows host name and address of active ports
Filename : [Link]
variables :i
Methods :
getLocalHost(),getHostName(),getHostAddress()
Classes used : InetAddress ,port1
Objects used : se,e,host,haddr,s,h
Ports : 1 to 1024
Loops used : for
Exception handlers : throws,try..catch */
3
[Link]("\n Host Address :: "+haddr);
Output Screen:
4
2. PROGRAMS FOR CHAT APPLICATION
Problem Description:
Client:
A program that initiates communication with
another program called a server.
Server:
5
A program that can provide services to other
programs called clients.
Socket:
A socket is a software endpoint that establishes
bi-directional communication between a server
program and one or more client programs. The socket
associates the server program with a specific hardware
port on the machine where it runs so that any program
anywhere in the network with a socket associated with
that same port can communicate with the server
program.
In one-one chat one party can communicate with
another party and exchange information.
In many- many chat each client opens a socket
connection to the chat server and writes to the socket.
This is seen by all other parties connected to the
server.
6
Filename : [Link]
variables : --
Methods : readLine() , accept() ,getInputStream(),
getOutputStream, print(), println() ,outputStreamWriter(),
flush()
Socket classes used : BufferedReader , InputStreamReader
PrintWriter(), Socket() ,ServerSocket () , chats
Objects used : s,ss,in ,br,e,msg,pw
Ports : clientport , serverport
Loops used : while
Exception handlers :try..catch */
Socket s=[Link]();
7
/* [Link] is an object which is wrapped up in to
inputStreamReader , this converts bytes to
characters. To obtain a character based stream it is
attached to BufferedReader in : reads the data from
keyboard and br reads from socket object */
8
} // end main
} // end class
Filename : [Link]
variables : --
Methods : readLine() , getInputStream(),
getOutputStream ,print(), println() ,outputStreamWriter(),
flush()
Socket classes used : BufferedReader , InputStreamReader
PrintWriter(), Socket() , chats
Objects used : in,pw,msg,from,br,e
Ports : clientport , serverport
Loops used : while
Exception handlers : try..catch */
9
BufferedReader br=new BufferedReader(new
InputStreamReader([Link]()));
// pw : to write the client data into output stream
PrintWriter pw=new
PrintWriter([Link]());
while(true) // infinite loop
{
[Link]("client");
String msg=[Link](); // reads client data from
key boards
[Link](msg); // sends msg through output
stream
[Link](); // flushes print writer
String from = [Link]();// reads server data
from stream
[Link]("from server="+from);
if([Link]("quit")) // if quit
{
[Link]("Disconnect");
[Link](0);
} // end if
} // end while
} // end try
10
Output screen:
11
2b. MANY TO MANY CHATTING
12
BufferedReader br=new BufferedReader(new
InputStreamReader(s[i].getInputStream()));
String msg=[Link](); // reads client data
user[i]=msg;
[Link](msg+" connected");
try
{
reqhandler req=new reqhandler(s[i],i);
total=i;
i++;
Thread t=new Thread(req); // thread object
[Link]();
}// end inner try
catch(Except ion e)
{
[Link](e);
} // end catch
}// end while
} // end outer try
catch(Exception e)
{
[Link](e);
} // end outer catch
} // end main
} // end catch
13
String msg=" ";
BufferedReader br;
PrintWriter pw;
try
{
while(true)
{
br=new BufferedReader(new
InputStreamReader([Link]()));
msg=[Link]();
if([Link]("quit"))
[Link]--;
else
[Link]([Link][n]+"->"+msg);
if([Link]==-1)
{ // if -1 server gets disconnected
[Link]("server Disconnected..");
[Link](0);
} // end if
for(int k=0;k<=[Link];k++)
{
if(![Link][k].equals([Link][n])&&(!
[Link]("quit")))
{
pw=new PrintWriter(new
OutputStreamWriter(mserver.s[k].getOutputStr
eam()));
[Link]([Link][n]+":"+msg+"\n);
[Link]();
} // end if
} // end for
} // end while
} //end try
catch(Exception e) // holding exception object
{ } // end catch
} // end method run
} // end class req handler
14
/* CLIENT SIDE PROGRAM */
15
[Link]("Enter name:");
in =new BufferedReader(new
InputStreamReader([Link]));
String msg=[Link](); // reading string
pw=new PrintWriter(new
OutputStreamWriter([Link]()));
[Link](msg+"\n");
[Link](); // flush the stream
while(true)
{
readdata rd=new readdata(s); // invoking readdata()
Thread t=new Thread(rd); // creating thread
[Link]();
msg=[Link]();
if([Link]("quit"))
{
[Link](0);
} // end if
[Link](msg); // sending message to the stream
[Link]();
} // end while
} // end try
catch(Exception e)
{
[Link](e);
} // end catch
} // end main
} // end class
16
try
{
while(true)
{
br= new BufferedReader(new
InputStreamReader([Link]()));
String msg=[Link]();
[Link](msg);
} // end while
} // end try
catch(Exception e)
{
[Link](e);
} // end catch
} // end run method
} // end class
17
Output Screen:
18
[Link] RETRIEVAL FROM REMOTE
DATABASE
19
Problem Description:
Remote database:
A database to which a connection is made using a
database link which is connected to a local database.
Remote database access is a protocol
standard for database access. It describes the
connection of a database client to a database server. It
includes features for:
o communicating database operations and
parameters from the client to the server.
o in return transporting result data from the server
to the client.
o Database transaction management.
20
/* SERVER SIDE PROGRAM */
21
[Link]("[Link]");
// Obtain database connection , remotedb is data source
name registered at 32 // bit odbc , scott and tiger are
username ,password to log in to sql
con =
[Link]("jdbc:odbc:remotedb","sco
tt","tiger");
// used to create statement to run sql query
s = [Link]();
ss = new ServerSocket(8504);
while(true)
{
try
{
soc = [Link]();
[Link]("Connected........");
pw = new PrintWriter([Link]());
br = new BufferedReader(new
InputStreamReader([Link]()));
String qry = [Link]();
rs = [Link](qry); // executes the query and
stores the records in rs
if(rs!=null)
rm = [Link]();
int cnt = [Link](); // returns [Link]
columns in the database
for(int i=1;i<=cnt;i++)
[Link]([Link](i)+" | "); // sending
columns names in output stream
[Link]("\n");
[Link](); // flushes stream
int ct=0;
while([Link]()) // repeats till end of the record
{
for(int i=1;i<=cnt;i++)
[Link]([Link](i)+" | ");
[Link]("\n"); //Printing Line By Line
[Link]();// flushes stream
++ct;
22
} //while rs()
[Link]("\n\n" +ct+" Row(s) Selected");
[Link]();
[Link]("end#");
[Link]();
[Link](); //Closing the PrintWrite
[Link](); //Closing the br
[Link](); //Closing the socket
[Link]("Service Complete...");
} // end try
catch(Exception e)
{ } // end catch
}//while
}//main
}//class
23
/* CLIENT SIDE PROGRAM */
import [Link].*;
import [Link].*;
import [Link].*;
class dbclient
{
public static void main(String args[]) throws Exception
{
Socket soc;
BufferedReader br,keyin;
PrintWriter pw;
String name,data;
keyin = new BufferedReader(new
InputStreamReader([Link]));
// host id reading
[Link]("Enter Remote Server ID: ");
name = [Link]();
[Link]("Enter U R Query");
while(true)
24
{
[Link]("Sql>");
String sql=[Link](); // input query like select
* from emp
if([Link]("quit"))
break;
soc = new
Socket([Link](name),8504);
pw = new PrintWriter([Link]());
br = new BufferedReader(new
InputStreamReader([Link]()));
[Link](sql);
[Link]();
while(true)
{
data=[Link]();
if ([Link]("end#"))
break;
[Link](data);
}
}//outer while
}//main
}//class
25
Output Screen:
26
27
4. FTP CLIENT SERVER PROGRAM
Problem Description:
28
/* SERVER SIDE PROGRAM */
29
PrintStream p=new
PrintStream([Link]());
String st=[Link]();
String temp=st;
temp= "[Link]
// url object creation
URL u=new URL(temp);
// opening connection to download the file
URLConnection uc = [Link]();
// creates file input stream to copy source to target
FileInputStream dil = new FileInputStream(st);
int ch;
while((ch=[Link]())!=-1) // repeat till not equal to -
1
{
[Link]((char)ch); // print on the screen
[Link](ch);
} // end inner while
[Link]();
} // end outer while
} // end try
catch(Exception e)
{
} // end catch
} // end main
} // end class
30
/* CLIENT SIDE PROGRAM */
31
[Link](ch);
} // end while
}// end main
}// end class
Output Screen:
32
5. TRIVIAL FILE TRANSFER PROTOCOL
Problem Description:
33
block of data less than 512 bytes terminates connection.
Each DATA message, except the last, carries 512 bytes
of data from the file. FTP uses stop-and-wait protocol for
flow control.
34
Loops used : while
Exceptions used : try catch , throws */
35
{
[Link](new
DatagramPacket(buff,pos,[Link]
(),50));
pos=0;
} // end if
} // end while
[Link](new
DatagramPacket(buff,pos,[Link](),5
0));
} // end try
catch(Exception e)
{
} // end catch
} // end run
} // end class
36
/* CLIENT SIDE PROGRAM */
class tftpc
{
DatagramSocket ds;
int buff_size=2000;
byte buff[]=new byte[buff_size];
tftpc() // constructor
{
try
{
ds=new DatagramSocket(50);
} // end try
catch (Exception e)
{
37
[Link](0);
} // end catch
DataInputStream in=new DataInputStream([Link]);
try
{
// reading absolute file path
[Link]("Enter absolute path of file:");
String str=[Link]();
int pos=[Link]();
// creates byte array
byte buf[]=new byte[pos];
for(int i=0;i<pos;i++)
buf[i]=(byte)[Link](i);
[Link](new
DatagramPacket(buf,pos,[Link](),69
));
[Link]("enter the name to be saved");
String stt=[Link]();
// creates file output stream
FileOutputStream fos=new FileOutputStream(stt);
while(true)
{
DatagramPacket dp=new
DatagramPacket(buff,[Link]);
[Link](dp);
String file=new
String([Link](),0,[Link]());
int g=[Link]();
for(int j=0;j<g;j++)
[Link]((char)[Link](j));
} // while
}// end try
catch(Exception e)
{
} // end catch
}// end method
} // end class
38
Output Screen:
39
6. SIMULATION OF TELNET(Remote Login)
Problem Description:
40
Program :
41
import [Link].*; // importing io package
import [Link].*; // importing net package
import [Link].*; // swing package
import [Link].*; // awt packages
import [Link].*;
42
exit=new JMenuItem("Exit");
[Link](this);
[Link]('C');
[Link](conhost);
[Link](discon);
[Link](exit);
setVisible(true); // displays frame
new condialog();
}
public void keyPressed(KeyEvent ke)
{ // no code required
}
public void keyReleased(KeyEvent ke)
{// no code required
}
public void keyTyped(KeyEvent ke)
{
// [Link]([Link]());
if([Link]()==KeyEvent.VK_ENTER)
{
[Link](command);
[Link](command);
command="";
}
else
if([Link]()!=KeyEvent.VK_SHIFT)
command=command+[Link]();
}
43
if(ob==discon)
if(!(soc==null))
{
[Link]("Connection closed");
try
{
[Link]();
soc=null;
}catch(Exception e)
{
[Link](e);
}// end catch
} // end if
} // end method
void makeconnection()
{
try
{
soc=new
Socket([Link]().trim(),[Link](con
[Link]().trim()));
[Link](null,"Connection
established");
[Link]("Connection established ");
br=new BufferedReader(new
InputStreamReader([Link]()));
pw=new PrintWriter([Link](),true);
/* String reply=[Link]();
[Link](reply+"\n");
[Link]([Link]()); */
new readthread().start();
}
catch(Exception e)
{
[Link](e);
[Link](null,"Connection
to host lost");
}
44
}
// class that reads responses from server
int lc=[Link]();
off=[Link](lc);
[Link](off); // places cursor at the
end
} // end while
} // end try
catch(Exception e)
{
[Link](e);
[Link](null,"Connection
To Host Lost");
} // end catch
} // end run
} // end class
public static void main(String a[])
{
t=new telnet();
// [Link]();
}
}
// class that creates connection dialog
class condialog implements ActionListener
45
{
JDialog jd;
public static JTextField host,port;
JButton connect,cancel;
public condialog()
{
jd=new JDialog();
host=new JTextField(40);
port=new JTextField(40);
connect=new JButton("CONNECT");
cancel=new JButton("CANCEL");
[Link](this);
[Link](this);
[Link](200,300,300,150);
[Link]().setLayout(new GridLayout(3,2));
// adding components to content pane
[Link]().add(new JLabel("Remote
Host :"));
[Link]().add(host);
[Link]().add(new JLabel("Port
Number :"));
[Link]().add(port);
[Link]().add(connect);
[Link]().add(cancel);
[Link](true);
}// end method condialog constructor
public void actionPerformed(ActionEvent ae)
{
Object ob=[Link]();
if(ob==cancel)
[Link]();
else
if(ob==connect)
{
[Link]();
[Link]();
} // end if
} // end action performed
46
}// end class condialog
Output Screen :
47
48
7: HYPER TEXT TRANSFER PROTOCOL
Problem Description:
49
The client makes a HTTP request using a web-browser
is referred as user agent. The responding server which
stores or creates resources such as HTML files and
images is called the origin server.
HTTP methods:
GET:
Requests a representation of the specified resource.
POST:
Submits data to be processed to the identified
resource. The data is included in the body of the
request. This may result in the creation of a new
resource or the updates of existing resources or both.
HEAD:
Asks for the response identical to the one that would
correspond to a GET request, but without the response
body. This is useful for retrieving meta info. Written in
response header, without having to transport entire
content.
DELETE:
Deletes the specified resource.
50
Methods : ServerSocket(), accept(), BufferedReader(),
InutStreamReader(), getInputStream(), getOutputStream(),
PrintWriter(), lastindexof(), subString(), readLine(),
FileInputStream(), available(), String(), Close(), delete(),
exit()
Classes used : htps
Loops used : do..while
Condition :if,switch
Exceptions used : try catch , throws */
choice=[Link]([Link]());
String file;
byte line[]=null;
File f;
51
switch(choice)
{
//head method to get the header details of a
resource
case 1:[Link]("[Link]");
file=[Link]();
f=new File(file);
int index=[Link](".");
String type=[Link](index+1);
[Link](type);
long length=[Link]();
[Link](length);
break;
//receiving the message that has been
posted
case 2:[Link]("[Link]");
file=[Link]();
[Link]("MESSAGE POSTED FROM
CLIENT:");
[Link](file);
break;
//reading the file contents and
//printing the contents of the resource
case 3:[Link]("[Link]");
file=[Link]();
FileInputStream fis=new
FileInputStream(file);
while([Link]()!=0)
{
if([Link]()<1024)
line=new byte[[Link]()];
else
line=new byte[1024];
[Link](line);
file=new String(line);
[Link](file);
} // end while
[Link]("***");
[Link]();
52
break;
//deleting the resource from the
server
case 4:[Link]("[Link]");
file=[Link]();
f=new File(file);
[Link]();
break;
default:[Link]("[Link]");
[Link](0);
} // end switch
} // end do while
while(choice<=4);
[Link]();
[Link]();
} // end method
} // end class
53
/* CLIENT SIDE PROGRAM */
// A client program for implementing http
Program name : [Link]
Packages used : [Link].*,[Link].*,[Link].*,
Objects used :
s1,con1,commandstoserver,input,f,file,line[],fos
Variables used :length,index
Methods : Socket(),accept(), BufferedReader(),
InutStreamReader(), getInputStream(), getOutputStream(),
PrintWriter(), readLine(), FileOutputStream(), available(),
String(),Close(),delete(),exit(),parseInt(), length(),
getBytes(), write()
Classes used : htps
Loops used : do..while
Condition : if,switch
Exceptions used : try catch , throws */
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
54
PrintWriter commandsToServer=new
PrintWriter([Link](),true);
BufferedReader input=new BufferedReader(new
InputStreamReader([Link]));
[Link]("CONNECTED TO SERVER");
int choice;
do
{
[Link]("COMMANDS");
[Link]("[Link] [Link] [Link]
[Link] [Link]");
[Link]("ENTER YOUR CHOICE:");
choice=[Link]([Link]());
byte line[]=null;
String file;
switch(choice)
{
case 1:[Link]("1");
[Link]("ENTER FILE NAME TO
GET THE HEADER:");
file=[Link]();
[Link](file);
String type=[Link]();
String
length=[Link]();
[Link]("FILE:"+file+"TYPE:"+type+"LENHTH:"
+length);
break;
case 2:[Link]("2");
[Link]("ENTER TEXT TO POST:");
file=[Link]();
[Link](file);
break;
case 3:[Link]("3");
[Link]("ENTER FILE NAME TO
GET:");
file=[Link]();
[Link](file);
55
[Link]("ENTER FILE NAME TO
SAVE:");
file=[Link]();
FileOutputStream fos=new
FileOutputStream(file);
while(true)
{
String s=[Link]();
if([Link]("***"))
break;
int count=[Link]();
if(count<1024)
line=new byte[count];
else
line=new byte[1024];
line=[Link]();
[Link](line);
}
[Link]();
break;
case 4:[Link]("4");
[Link]("ENTER FILE NAME TO
DELETE:");
file=[Link]();
[Link](file);
break;
default:[Link]("5");
[Link](0);
}
}
while(choice<=4);
[Link]();
}
}
56
Output Screen:
57
8. MAIL CLIENTS- POST OFFICE PROTOCOL
(POP)
&
58
SIMPLE MAIL TRANSFER PROTOCOL
(SMTP)
Problem Description:
59
8.1. MAIL CLIENT- POP ( COMMAND
PROMPT)
//pop mail
60
PrintWriter osToServer=new
PrintWriter([Link](
),true);
BufferedReader br=new
BufferedReader(new
InputStreamReader([Link]));
[Link]("user "+"user1");
[Link]("USER RESPONSE
IS:"+[Link]());
[Link]("pass "+"user1");
[Link]("PASSWORD
RESPONSE IS:"+[Link]());
[Link]([Link]());
while(true)
{
[Link]("ENTER THE POP
COMMAND:");
String c=[Link]();
if([Link]()==0) break;
[Link](c);
String s=[Link]();
while()
{
[Link](s);
s=[Link]();
}
}
[Link]();
}
catch(Exception e)
{
[Link]("ERROR:"+e);
}
}
}
61
8.2. MAIL CLIENT- SMTP ( COMMAND
PROMPT)
// smtp client
Program name : [Link]
Packages used : [Link].*,[Link].*,[Link].*,
Objects used :
connecttoserver,isfromserver,osToserver,br,c,s,e
Methods : Socket(), BufferedReader(),
InputStreamReader(), getInputStream(),
getOutputStream(), PrintWriter(), readLine(), close()
Classes used : POPMAIL
Loops used : while
Condition : if
Exceptions used : try catch */
import [Link].*;
import [Link].*;
import [Link].*;
62
public class SMTPClient
{
63
case 3: [Link]("Enter receivers
address");
msg=[Link]();
[Link]("RCPT TO: "+
msg);break;
case 4:[Link]("Enter data to
send");
msg=[Link]();[Link]("DATA");
[Link](msg);[Link](".");
[Link]("QUIT");break;
default :[Link](0);
}
}while(ch<5);
}
catch(Exception e)
{
[Link](e);
}
}
}
64