Sunteți pe pagina 1din 152

m 

m   

This PPT is Dedicated to my inner controller


[ [ [[[ 6  ounders.

Developed by, EDITED BY,


  
 M.Siva Naga Prasad
Associate professor, student of M.tech(SE).
Dept.of CSE, GIT,
GITAM UNIVERSITY.
 mm
Ú  
import java.io.*;
import java.net.*;
class wkports
{
public static void main(String args[])
{
for(int i=100;i<200;i++)
{
try
{
Socket s=new Socket(host,i);
System.out.println("There is a server on port :"+ i +" of: " +host);
}
catch(UnknownHostException e)
{
System.err.println(e);
}
catch(IOException ie)
{
System.out.println(ie.getMessage());
} } }}
ë  
 
 One-to-one communication is the act of an
individual communicating with another.

  In Internet terms, this can be done by e-mail but


the most typical one-to-one communication in the
Internet is instant
messaging as it does not consider many-to-many
communication such as a chat room as an
essential part of its scope
„   m
Ñ The program displays what is written by one party to another
by opening a socket connection
Ñ The Application contains two classes:
Ñ 1.Chat client
Ñ 2.Chat server
Ñ The chat server class creates class creates a serves
socket object which listens on port 9999.The server socket
accepts client socket and reads whatever is written by client
and sends the message to the client thus allowing One-one
communication
Ñ The Chat client class creates a socket object using which
it sends message to the server on port 9999.The
communication ends when the client send server says ´byeµ.
  m „ „ m  !

Ñ Mocket Programming
Ñ Exception Handling

Ñ I/O and MtreamClasses


 

m"

Ñ A socket is one end-point of a two-way


communication link between two programs
running on the network. Mocket classes are
used to represent the connection between a
client program and a server program. The
java.net package provides two classes--Mocket
and Merver Mocket--that implement the client
side of the connection and the server side of
the connection, respectively.
#  m m„ m"
hat is Exception???
Ñ An exception is an event that occurs during the execution of a
program that disrupts the normal flow of instructions.

Why Exception Handling??


Ñ To terminate the program neatly in error condition or unexpected
scenario.
Ñ To fix the problem at run time
Ñ To give user friendly messages to end user in case of unusual
scenario or error
#  m m„ m m  "
Ñ When an error occurs within a method, the method
creates an object and hands it off to the runtime system.
The object, called an › › ›, contains
information about the error, including its type and the
state of the program when the error occurred. Creating an
exception object and handing it to the runtime system is
called 
  › ›.
Ñ In java Exception handling is achieved by using
6 
and  Blocks of code.
Ñ The Muper class for all types of exceptions in java is
  ›
$ 
try
{
›
£

CATCH BLOCK
÷ ÷÷


do something with ex


Ñ ÷ ÷
A method can catch an exception by providing an exception handler
for that type of exception.
Client side program:
%&'(!!)*
%&'(!!)*
%&'(!+!)*
,-../
0
,-.(1%&+&+.23&4.  5
0
6.7,*
&+.*
8&+.!+9:
.7&+.2:3*
.
.7;.;*
&&&,7,*
,88&11&-7,*
&<
0
.746.=>*
,74&&&.!+,,&%=&,*
-74 ,88&11&4
,&%1&.!+ ,&%*
?
64. 5
0
<.%!&&!&;64.;*
<.%!5/*
?
  5
0?
,88&11&74 ,88&11&4 ,&%1&<.%!*
&+,*
4&,
0
,7!&1 *
,!&,*
&+%.+7-!&1 *
<.%!,!&;";@%.+*
8%.+!A,.;-<;
-&6*
?
!.*
,!.*
-!.*
.!.*
?
?
&(&.1&+&%
%&'(!!)*
%&'(!!)*
%&'(!+!)*
,-...&(&/
0
,-.(1%&+&+.23&4.  5
0
&(&6..7,*
&<
0
..74&(&6>*
?
  5
0
<.%!&&!&;m.-.1;*
<.%!5/*
?
6.7,*

&<
0
.7..!*
?
  5
0
<.%!&&!&;m1;*
<.%!5/*
?
&&&474&&&.!+,,&%=&,*
,88&11&-&74 ,88&11&4
,&%1&.!+ ,&%*
&+*
&+,*
&<
0
„ ,&%174„ ,&%<.%!*
4&,
0
7-&!&1 *
<.%!,!&;&(&";@*
,71!&1 *
4!&,*
8,!A,.;-<;
-&6*
?
?
 5
0
<.%!&&!&*
?
4!.*
-&!.*
.!.*
..!.*
?
?
&(&.1,,414"

.1,,414"

m$
m$ 
   m
„   m"
Ñ Each client opens a socket connection to the chat
server and writes to the socket whatever is written by
one party can be seen by all other parties.

Ñ Chat Rooms in yahoo is the best example for this


many to many chat application.
J   
 
m   m„ 

Ñ Mocket communication
Ñ Exception Handling

Ñ IO

Ñ Multi Threading
 

m

Ñ A socket is one end-point of a two-way


communication link between two programs
running on the network. Mocket classes are
used to represent the connection between a
client program and a server program. The
java.net package provides two classes--Mocket
and Merver Mocket--that implement the client
side of the connection and the server side of
the connection, respectively.
   m 
"
u It Reads text from a character-input stream, buffering
characters so as to provide for the efficient reading of
characters, arrays, and lines
u It belongs to java.io Package.
u BufferedReader (Reader in)
Create a buffering character-input stream that
uses a default-sized input buffer.
u Methods In This Class«
ReadLine(): Read a line of text. A line is considered
to be terminated by any one of a line feed
('\n'), a carriage return ('\r'), or a carriage return
followed immediately by a linefeed
Print Writer:

Ñ Print formatted representations of objects to a text-output


stream.

Ñ public PrintWriter(OutputMtream out,boolean autoFlush)

Create a new PrintWriter from an existing OutputMtream.


This convenience constructor creates the necessary
intermediate OutputMtreamWriter, which will convert
characters into bytes using the default character
encoding.
  mB

InputMtream
Ñ Returns an input stream for socket.

Ñ This method is Belongs socket class.

Ñ Mocket class is belongs to java.net. Package

OutPutMtream
Ñ Returns an output stream for socket.
m
 „ 
Ñ An InputMtreamReader is a bridge from byte
streams to character streams: It reads bytes and
decodes them into characters using a specified
charset. The charset that it uses may be specified
by name or may be given explicitly, or the
platform's default charset may be accepted.
Ñ Public InputMtreamReader(InputMtream in) :

Create an InputMtreamReader that uses the


default charset.
„  m

Ñ A data input stream lets an application read


primitive Java data types from an underlying
input stream in a machine-independent way.
An application uses a data output stream to
write data that can later be read by a data
input stream.
Ñ public DataInputMtream(InputMtream in) :
Creates a DataInputMtream that uses the
specified underlying InputMtream

   „ m
Ñ Thread is a Active part of execution.

Ñ Thread is a path of execution.

Ñ Threads are also called as lightweight process

Ñ Threads are executed in parallel.



   „ m m 

Ñ We can implement multi threading using java


in 2 ways
Ñ 1.)By inheriting Thread class

or
Ñ 2.)By Implementing RUNNABLE interface
 „ 

Ñ To implement Multi threading we have to


override the run method of Thread class
Ñ Thread class is belongs to java.lang. package

Ñ Methods in this class

Ñ Mtart(),run(),sleep(),stop(),join()«e.t.c.
mm  m C 

Ñ This is an interface with only one method


Ñ That is RUN()

Ñ By implementing this interface we can get


multi threading
J 
  
import java.net.*;
import java.io.*;
public class mclient
{
public static void main(String a[])
{
BufferedReader in;
PrintWriter pw;
try
{
Socket s = new Socket("localhost",118);
System.out.println("Enter name");
in = new BufferedReader(new InputStreamReader(System.in));
String msg = in.readLine();
pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
pw.println(msg+"\n");
pw.flush();
while(true)
{
readdata rd = new readdata(s);
Thread t = new Thread(rd);
t.start();
msg = in.readLine();
if(msg.equals("quit"))
{
System.exit(0);
}
pw.println(msg);
pw.flush();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}

class readdata implements Runnable


{
public Socket s;
public readdata(Socket s)
{
this.s = s;
}
public void run()
{
BufferedReader br;
try
{
while(true)
{
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String msg = br.readLine();
System.out.println(msg);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}




import java.net.*;
import java.io.*;
public class mserver
{
public static Socket s[] = new Socket[10];
public static String user[] = new String[10];
public static int total;
public static void main(String a[])
{
int i=0;
try
{
ServerSocket ss = new ServerSocket(118);
while(true)
{
s[i] = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s[i].getInputStream()));
String msg = br.readLine();
user[i] = msg;
System.out.println(msg+" connected") ;
try
{
reqhandler req = new reqhandler(s[i],i);
total = i;
i++;
Thread t = new Thread(req);
t.start();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class reqhandler implements Runnable
{
public int n;
public Socket s;
public reqhandler(Socket soc,int i)
{
s = soc;
n = i;
}
public void run()
{
String msg = "";
BufferedReader br,br1;
PrintWriter pw;
try
{
while(true)
{
br1 = new BufferedReader(new InputStreamReader(System.in));
if((br1.readLine()).equals("quit"))
System.exit(0);
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
msg = br.readLine();
if(msg.equals("quit"))
mserver.total--;
else
System.out.println(mserver.user[n]+"->"+msg);
if(mserver.total == -1)
{
System.out.println("Server Disconnected");
System.exit(0);
}
for(int k=0;k<=mserver.total;k++)
if(!mserver.user[k].equals(mserver.user[n])&&(!msg.equals("quit")))
{
pw = new PrintWriter(new OutputStreamWriter(mserver.s[k].getOutputStream()));
pw.println(mserver.user[n]+":"+msg+"\n");
pw.flush();
}
}
}
}
catch(Exception e)
{
}
}
}

ë  


G   


„   


 


  

①  


â
     
â      
        
 
  


 

6 
     
Ú         
    
        

 G G  G

Port:
1111
 
    


    


 
 

  
   
   
6           

  

        


       
    c  


Ú   


Ú     

    c    



 
  
Ú   Ú     

c    




        


       

c  




        


    ! 

6   
 
     


â‘  !  


" # $  %  „ #


    


„ &     #$   #'(  #'(  #

  #% 

 

   "    

â  


  #  $‘% 
 ) ! * 

Ú % 
 

 
) !


 

) ! * 


 
   

% % c  &% &



 


& „   & „  


+ , !) !
 "   
ets the field
names from
 # the tables

"  '&(‘6 &
  " "  
    ‘
 &&
  & &

ets the
 values from
 the tables
  " "  
     &&
  & &
  #

  

J 

  

All the data retrieved is said to be returned to the client through


rset object

     

Field 1 Field 2
xxxx yyyyy
zxzxzx zzzzz


J 

   
&(&.1&+&%"

%&'(!!)*
%&'(!!)*
%&'(!.A!)*
..&1-.&(&
0
,-.(1%&+&+.23
0
7,*
%.%7,*
.,&.7,*
&<
0
&(&6..74&(&6////*
<.%!,!&;48&"D;*
6.7..!*
8.E7,
<.%!,!&;.1";*
,88&11&8&%74 ,88&11&4
,&%1&.!+ ,&%*
&&%74&&%.!+,,&%*
,88&11&74 ,88&11&4 ,&%1&<.%!*
&+A,&<7;;*
&+ ,88&&.7,*
..!8&m%;.,!'1-!1-!1-1-„&(&;*
7„&(&
+&!+;'1-"1-"(1;=;.;=;+&;*
.%7!&%*
1
0
A,&<78&%!&1 *
<.%!,!&;A,&<&A,.";@A,&<*
8A,&<!A,. +&.;A,;
-&6*
&.7.%!5,F,&<A,&<*
.,
„&.%17&.!+
„*
7&.%1!+,%,*
8&.!5
0
&.74&+ ,88&;  "D;*
8&7/*G7*@@
&.!1&.%1!+,% -@;D;*
&.!1;D;*
?
„
0
8&7/*G7*@@
&.!1&.!+&+@;D;*
&.!1;D;*
?
4&.!5*
&.!1;;*
!&&.*
?
4EA,&<!A,. +&.;A,;*
!.*
?
 5
0
<.%!,!&!+
..+*
?
?
?
.1&+&%"

%&'(!!)*
%&'(!!)*
%&'(!.A!)*
,-..&1-
0
,-.(1%&+&+.23
0
<.%!,!&;1.&(&1;*
&<
0
6.746;.;=////*
&&%.74&&%.!+,,&%*
,88&11&8&%.74 ,88&11&4
,&%1&.!+ ,&%*
,88&11&8&%6-74 ,88&11&4
,&%1&<.%!*
&+A,&<7;;*
<.%!,!&;1"D;*
<.%!,!&;&A,&<";*
1
0
A,&<78&%6-!&1 *
.!&A,&<*
8A,&<!A,. +&.;A,;
-&6*
1
0
A,&<78&%.!&1 *
<.%!,!&A,&<*
?
4EA,&<!.&.;;*
?
4EA,&<!A,. +&.;A,;*
?
 5
0
<.%!,!&*
?
?
?
&(&.1,,414"
.1,,414"

 
 mC 

Ú  

B)%&6+.118&%1+,.,&)B
%&'(!!)*
%&'(!!)*
%&'(!4!)*
%&'(!4!(!)*
B)))...18.%1.1.<+,11.1+8
)%.
.&(&!)B
,-..
51.14 1&%%.  .&
0
&( ,.1 ,74 ,;1
..+;*
&( -8&% -74 -;C&%";*
&( - -74 -;";*
&( -. -74 -;.m%";*
&( -.,- -74 -;,-'";*
&(5C18&%5745C1HI*
&(5C15745C1HI*
&(5C1.,-5745C1HI*
&(5C1.5745C1HI*
&(5 &%.+5745 &*
&(C&%C&%74C&%;
;*
B))).&,&46.&%&.!
)..&,&1.<.4148&,.&
)....1+%.!)B
,-

0
C&%!. <,4&1 <,H=/*
/74*
/!. <,4&1 <,J=/*
//74*
//!. <,4C4 <,*
//!11. -*
//!11.5*
/H74*
/H!. <,4C4 <,*
/H!118&% -*
/H!118&%5*
/K74*
/K!. <,4C4 <,*
/K!11 -*
/K!115*
/J74*
/J!. <,4C4 <,*
/J!11.,- -*
/J!11.,-5*
/!11//*
/!11/H*
/!11/K*
/!11/J*

H74*
H!. <,4 &1& <,*
H!11%.+5= &1& <,! m *
H/74*
H/!. <,4C4 <,*
H/!11.1 ,*
H!11H/= &1& <,!*

C&%!11/*
C&%!11H*
C&%!.LJ::=I::*
C&%!..-&,*

C&%!1114 .&.*
.1 ,!11  .&.*
?
B))).%1.&++&14.-,8414
)..1!%15.!)B
,-(1414.+14 (4
0
C&%!..-8.*
<.%!5/*
?
B))).%1.1&..-,6.!
)%1&1.%..+-.=6.
)
..+-'1.1.%.&(&!)B
,-(1&8&%1  (
0
&<
0
6.746.5!+5=HI*
,88&11&-&74 ,88&11&4
,&%1&.!+ ,&%*
&&&474&&&.!+,,&%=&,*
<.%!,!&;..-.1;*
4!&; ;*
<.%!,!&-&!&1 *
4!&;
C&%";@8&%5!+5*
<.%!,!&-&!&1 *
4!&;";@5!+5*
<.%!,!&-&!&1 *
4!&;„  ;*
4!&%.+5!+5@;D!D;*
<.%!,!&-&!&1 *
4!&;F ;*
4!8,.*
.!.*
<.%!,!&;
.-.!!!!;*
?
 5
0
?
<.%!,!&;&%1;*
?

B)))..%%1! ..-'8.
)..
111..&.8&8&%414
)1-,.,.1+,!)B
,-.(1%&+&+.23
0

74
*
?
?
Ú
 mM
 m
± Mhort for Ú›Ú  a protocol used
to retrieve e-mail from a mail server. Most e-
mail applications (sometimes called an ›
›) use the POP protocol, although some
can use the newer IMAP (Internet Message
Access Protocol).
± There are two versions of POP. The first, called
ÚÚ  became a standard in the mid-80's and
requires MMTP to send messages. The newer
version, POP3, can be used with or without
MMTP. POP3 uses TCP/IP port 110.
± With IMAP, all your mail stays on the server in multiple
folders, some of which you have created. This enables
you to connect to any computer and see all your mail and
mail folders. In general, IMAP is great if you have a
dedicated connection to the Internet or you like to check
your mail from various locations.

± With POP3 you only have one folder, the Inbox folder.
When you open your mailbox, new mail is moved from
the host server and saved on your computer. If you want
to be able to see your old mail messages, you have to go
back to the computer where you last opened your mail.

± With POP3 "leave mail on server" only your email


messages are on the server, but with IMAP your email
folders are also on the server.
K m    m

   N
B
 
Ñ Post Office Protocol (POP) and Mimple Mail Transfer
Protocol (MMTP) are involved in email services.
Ñ Users use an application called a Mail User Agent
(MUA), or e-mail client to allow messages to be sent and
places received messages into the client's mailbox.
Ñ In order to receive e-mail messages from an e-mail
server, the e-mail client can use POP.
Ñ Mending e-mail from either a client or a server uses
message formats and command strings defined by the
MMTP protocol.

 mC   m
 

„  $  m
„ 

POP
SMTP
   „ 
"
B)"+(..&(&%=,.&%1..4&1=&&(
%.14%,8%-5,.+%%1.)B
%&'(!!)*
%&'(!!)*
%&'(!4!)*
%&'(5!.4+!)*
%&'(!,!)*
%&'(!4!(!)*
..51.C&%%%.  .&
0
'*
 -11=,%=..=&&=,.&*
5C111=,%=,.&=&&=1*
..4&1C1..*
5 &&(*
&.&*
 ,+=.=A,=&&(=1*
6.*
&&&4*
,88&11&-&*
%.+.*

0
'74*
'!. <,,*
1174 -;&11&..;*
11!. ,1.H:=H:=/::=H:*
'!1111*
11745C1*
11!. ,1.//:=H:=/H:=H:*
'!1111*
,%74 -;&,%-&;*
,%!. ,1.H:=J:=/::=H:*
'!11,%*
,%745C1*
,%!. ,1.//:=J:=/H:=H:*
'!11,%*
+74 ,;+;*
+!. ,1.H:=/::=H/:=H:*
'!11+*
+!11  .&.*
,.&74 -;,.&%;*
,.&!. ,1.H:=O:=/::=H:*
'!11,.&*
,.&745C1*
,.&!. ,1.//:=O:=/H:=H:*
'!11,.&*
..74 -;..4&1;*
..!. ,1.H:=P:=/::=H:*
'!11..*
..74..4&1C1*
..!. ,1.//:=P:=/H:=H:*
'!11..*
.74 ,;.;*
.!. ,1.H:=/H:=H/:=H:*
.!11  .&.*
'!11.*
&&(74 ,;&&(;*
&&(!. ,1./K:=/JI=/K:=H:*
'!11&&(*
&&(!11  .&.*
174 ,;1;*
1!. ,1./K:=/OI=/K:=H:*
'!111*
1!11  .&.*
&&74 -;&%+,%-&;*
&&!. ,1.H:=/JI=/H:=H:*
'!11&&*
&&745C1*
&&!. ,1./::=/JI=K:=H:*
&&!11  .&.*
'!11&&*
1745C1*
1!. ,1./::=/OI=K:=H:*
1!11  .&.*
'!111*
&(745 &*
&(!. 1-8.*
.&74&&(*
'!11.&*
.&!. ,1.H:=H::=K::=H::*
A,74 ,;A,;*
A,!. ,1./K:=J/:=P:=K:*
'!11A,*
A,!11  .&.*
.;.88&;*
.LKI:=J>:*
.4*
.!+!11'*
..-&,*
?
,-(1&8&%1  (
0
8!+,&77+
0
&<
0
.74611!+5= +&!&. ,%!+5*
-&74 ,88&11&4 ,&%1&.!+ ,&%*
474&&&4
,,&%&&.!+,,&%=&,*
&(!.5-&!&1 @;D;*
4!&;,.&;@,.&!+5*
&(!1-&!&1 @;D;*
4!&;..;@..!+5*
&(!1-&!&1 @;D;*
?
  5
0
!.4
..+„+4=;
-.-.1;*
?
?
8!+,&77.
0
&+6L&.*
&+.&*
4!&;.;*
&<
0
.&7-&!&1 *
.74&+6L&.&*
.!56*
.&7.!56*
%.+.7 +&!&. .&@/*
8&7:*G%.+.*@@
&(!1-&!&1 @;D;*
&(!1;8%..+.";@.&@;D;*
?
 5
0
!.4
..+„+4=*
?
?
8!+,&77&&(
0
&+67&&!+5!&%*
4!&; D;@6*
7 +&!&. 6*
&<
0
&+.7-&!&1 *
8G%.+.NN9:
0
4E.!A,.;!;
0
&(!1.@;D;*
.7-&!&1 *
?
?
.
0
&(!1.@;D;*
?
&&!.5;;*
?
 5
0
!.4
..+„+4=*
?
?
8!+,&77A,
<.%!5:*
8!+,&771
0
&<
0
4!&;1;@1!+5!&%*
%.+.QQ*
1!.5;;*
&(!1-&!&1 @;D;*
?
 5
0
!.4
..+„+4=*
?
?
?
,-.(1%&+&+.23
0
74*
?
?
.1&+&%"
%&'(!!)*
%&'(!!)*
%&'(!!)*
%&'(!4!)*
%&'(!4!(!)*
%&'(!,!)*
,-..51.C&%%%.  .&
0
5C1,.&=..=.=%.+*
5 &%.+*
,.+=1.=&(=5=5*
R8R%.+=*
&+.&*
,88&11&-&/=-&H*
&&&4*
6.7,*
,-
0
.,&;;*
,.&745C1H:*
..745C1H:*
.745C1H:*
%.+745C1H:*
%.+745 &;;=/:=I:*
.+74 ,;  m;*
1.74 ,;„  $;*
574 ,;m #;*
&(74 ,;  ;*
574 ,; # ;*
R8R%.+7:*
.&7;;*
7:*
/74*
H74*
K74*
J74*
. <,4&1 <,K=/*
.LJ::=J::*
/!. <,4&1 <,J=/*
//74*
//!114 -;.&m%";*
//!11,.&*
/H74*
/H!114 -;..4&1";*
/H!11..*
/K74*
/K!114 -;.m%";*
/K!11.*
/J74*
/J!11.+*
/!11//*
/!11/H*
/!11/K*
/!11/J*
K!. <,4&1 <,/=/*
K!11%.+*
J!. <,4&1 <,K=/*
J/74*
J/!114 -; &%..+,%-&";*
J/!11%.+*
JH74*
JH!111.*
JH!114 -;;*
JH!11&(*
JK74*
JK!115*
JK!114 -;;*
JK!115*
J!11J/*
J!11JH*
J!11JK*
11/*
11J*
11K*
..-&,*
..!. &S)S*
.+!11  .&.*
1.!11  .&.*
&(!11  .&.*
5!11  .&.*
5!11  .&.*
?

,-(1&8&%1  (


0
&<
0
8!+,&77.+
0
.746.!+5=//:*
-&/74 ,88&11&4
,&%1&.!+ ,&%*
474&&&.!+,,&%=&,*
%.+!1-&/!&1 *
<.%!,!&;/;*
4!&;,.&;@,.&!+5*
<.%!,!&;H;*
%.+!1;D;@-&/!&1 *
4!&; ;@..!+5*
<.%!,!&;K;*
%.+!1;D;@-&/!&1 *
4!&;.;*
.&7-&/!&1 *
%.+!1;D;@.&*
&+6L&.74&+6L&.&*
&+%.&7.!56*
%.&7.!56*
R8R%.+7 +&!&. %.&*
4E-&/!&1 !A,.;!;*
?
8!+,&771.
0
7 +&!&. %.+!+5*
1.<*
?
8!+,&775
0
@@*
1.<*
?
8!+,&77&(
0
QQ*
1.<*
?
8!+,&775
0
8.E7,
.!.*
<.%!,!&;&%1!!!!;*
<.%!5/*
?
?
 5
0
<.%!,!&*
?
?
(11.<
0
%.+!.5;;*
%.+!.5&+!(,8*
89:NNG7R8R%.+
0
<.%!,!&;m";@*
4!&;&&;@*
1
0
&<
0
.&7-&/!&1 *
?
 5
0?
<.%!,!&;%.+;@.&*
%.+!1;D;@.&*
?4E.&!A,.;!;*
?
.
%.+!.5;$,(;@R8R%.+@;%.;*
?
,-.(1%&+&+.23
0
74*
?
?
d d
Úëëë
M  d dÚëëë
dÚ
FTP is«
Ñ a standard network protocol used to
exchange and manipulate files over a
TCP/IP-based network, such as the Internet.

Ñ built on a client-server architecture and


utilizes separate control and data
connections between the client and server
applications.
M  d dÚëëë
dÚT
Ñ FTP differs from other client-server
applications, it establishes two connections
between the hosts
Ñ The first one is a data transfer connection
which does Data Transfer Process ( DTP )
Ñ The other is a control information connection
which interprets FTP commands through
Protocol Interpreter
M  dÚ
 dë

FTP is used to:


Ñ Promote sharing of files (computer programs
and/or data)

Ñ Transfer data reliably, and efficiently


CLIENT MIDE PRORAM :
import java.net.*;
import java.io.*;
public class ftpclient
{
public static void main (Mtring args[])
{
Mocket s;
BufferedReader in, br;
PrintWriter pw;
Mtring spath,dpath;
FileOutputMtream fos;
int c;
try
{
s = new Mocket ("localhost",1111);
in = new BufferedReader (new InputMtreamReader (Mystem.in));
br = new BufferedReader (new InputMtreamReader (s.getInputMtream()));
pw = new PrintWriter(s.getOutputMtream(), true);
Mystem.out.println("\nEnter Mourcepath to copy file : ");
spath = in.readLine();
Mystem.out.println("\nEnter DestinationPath to transfer : ");
dpath = in.readLine();
fos = new FileOutputMtream(dpath);
pw.println (spath);
while ((c=br.read())!=-1)
{
fos.write((char)c);
fos.flush();
£
Mystem.out.println("File trasfer completed:\n");
£
catch (Exception e)
{
Mystem.out.println(e);
£
£
£
MERVER MIDE PRORAM:
import java.net.*;
import java.io.*;
public class ftpserver
{
public static void main(Mtring args[])
{
Mocket s;
MerverMocket server;
FileInputMtream fis;
BufferedReader br;
PrintWriter pw;
Mtring filename;
int c;
try
{
server = new MerverMocket(1111);
Mystem.out.println("Merver waitfor for connection:\n");
s = server.accept();
Mystem.out.println("Connection established:\n");
br = new BufferedReader(new InputMtreamReader(s.getInputMtream()));
pw = new PrintWriter(s.getOutputMtream());
filename = br.readLine();
fis = new FileInputMtream(filename);
while ((c=fis.read())!=-1)
{
pw.print((char)c);
pw.flush();
£
Mystem.out.println(filename + " copied to destnation");
s.close();
£
catch(Exception e)
{
Mystem.out.println(e);
£
£
£
,,"

ßIn the very earliest days of internetworking, one of the most
important problems that computer scientists needed to solve was
how to allow someone operating one computer to access and use
another as if he or she were connected to it locally. The protocol
created to meet this need was called 6  , and the effort to develop
it was tied closely to that of the Internet and TCP/IP as a whole.
ß   <4&6.4&6& ,.1
& &&4&6. &(1-1&
&(%%,.8<!<<=&(1.
..%%1Q&8 &%.(
(&,&%4...8PQ--<
&11(&&.%..&
&!
Program:

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class telnet extends WindowAdapter implements ActionListener,KeyListener {

public static Frame telFrame;


public static Mocket s;
public static BufferedReader br = null;
public static PrintWriter pw = null;
public static telnet tnet;
public static MenuBar mbar;
public static Menu connectMenu;
public static Menu helpMenu;
public static MenuItem connect,disconnect,exit,help;
public static TextArea msgArea;
public static TextField statusArea;
public static Mtring command = "";
public static void main(Mtring args[])
{
telFrame = new Frame("Telnet");
msgArea = new TextArea();
statusArea = new TextField();
Panel p = new Panel();
p.setLayout(new BorderLayout());
mbar = new MenuBar();
connectMenu = new Menu("Connect");
connectMenu.add(connect = new MenuItem("Connect"));
connectMenu.add(disconnect = new MenuItem("Disconnect"));
connectMenu.add(exit = new MenuItem("Exit"));
mbar.add(connectMenu);
helpMenu = new Menu("Help");
helpMenu.add(help = new MenuItem("help"));
//helpMenu.add(more = new MenuItem("More.."));
mbar.add(helpMenu);
connect.addActionListener(new telnet());
disconnect.addActionListener(new telnet());
exit.addActionListener(new telnet());
help.addActionListener(new telnet());
//more.addActionListener(new Telnet());
msgArea.addKeyListener(new telnet());
p.add(msgArea,BorderLayout.CENTER);
p.add(statusArea,BorderLayout.MOUTH);
telFrame.setMize(450,350);
telFrame.setMenuBar(mbar);
telFrame.add(p);
telFrame.setVisible(true);
telFrame.addWindowListener(new telnet());
£
public void windowClosing(WindowEvent we) {
telFrame.setVisible(false);
Mystem.exit(0);
£
public void keyPressed(KeyEvent ke) {£
public void keyReleased(KeyEvent ke) {£
public void keyTyped(KeyEvent ke) {
if(ke.getKeyChar() == KeyEvent.VK_ENTER) {
Mystem.out.println(command);
pw.println(command);
command = "";
£
else if(ke.getKeyChar() != KeyEvent.VK_MHIFT)
command = command + ke.getKeyChar();
£
public void actionPerformed(ActionEvent ae) {
Mtring str = ae.getActionCommand();
if(str.equals("Exit"))
Mystem.exit(0);
if(str.equals("Connect"))
new ConnectFrame();
else if(str.equals("Disconnect")) {
if(!(s==null)) {
Mystem.out.println("in disconnecting...");
Mystem.out.println("Connection terminated");
statusArea.setText("Connection terminated");
try {
s.close();
s = null;
£
catch(Exception e) {
Mystem.out.println("closing socket.");
£
£
£
else {
new HelpFrame();
£
£
public void makeConnection() {
try {
s = new
Mocket(ConnectFrame.host.getText().trim(),Integer.parseInt(ConnectFrame.p
ort.getText().trim()));
br = new BufferedReader(new InputMtreamReader(s.getInputMtream()));
pw = new PrintWriter(s.getOutputMtream(),true);
statusArea.setText("Connection Established");
new ReadThrd(msgArea,statusArea,br);
£
catch(Exception e) {
Mystem.out.println(e);
statusArea.setText("Connection Failed");
£
£
£
class ReadThrd extends Thread {

TextArea msgArea;
TextField statusArea;
BufferedReader br;
ReadThrd(TextArea msgArea,TextField statusArea,BufferedReader br) {
super("reading data thread");
this.msgArea = msgArea;
.!.,. &7.,. &*
.!-&7-&*
.&*
?
,-(1&,0
&<0
887:*
4&,0
&+&<7-&!&1 *
8&<77,0
%.+ &!1;DDQQQQQQQQ&%..
&.1+QQQQQQQQDD;*
-&6*
?
%.+ &!1&<@;D;*
?
?
 50
<.%!,!&*
?
?
?
class ConnectFrame extends WindowAdapter implements ActionListener {

Frame conFrame;
public static TextField host,port;
Button connect,cancel;
public ConnectFrame() {
conFrame = new Frame("Connecting....");
host = new TextField(10);
port = new TextField(10);
connect = new Button("Connect");
cancel = new Button("Cancel");

Panel p1 = new Panel();


p1.add(new Label("Remote Host : "));
p1.add(host);

Panel p2 = new Panel();


p2.add(new Label("Port Number : "));
p2.add(port);

Panel p3 = new Panel();


p3.add(connect);
p3.add(cancel);
Panel p = new Panel();
p.setLayout(new ridLayout(3,1));
p.add(p1);
p.add(p2);
p.add(p3);

connect.addActionListener(this);
cancel.addActionListener(this);

conFrame.setMize(250,200);
conFrame.add(p);
conFrame.setVisible(true);
conFrame.addWindowListener(this);
£
public void actionPerformed(ActionEvent ae) {
telnet tnet = new telnet();
Mtring str = ae.getActionCommand();
if(str.equals("Cancel"))
conFrame.setVisible(false);
else if(str.equals("Connect")) {
conFrame.setVisible(false);
tnet.makeConnection();
£
£
public void windowClosing(WindowEvent we) {
conFrame.setVisible(false);
£
£
class HelpFrame extends WindowAdapter
{
Frame helpFrame;
public HelpFrame()
{
helpFrame = new Frame("Telnet Help");
TextArea helpTxt = new TextArea();
helpTxt.setEditable(false);
helpTxt.setText("Telnet help");
helpFrame.add(helpTxt);
helpFrame.setMize(300,400);
helpFrame.setVisible(true);
helpFrame.addWindowListener(this);
£
public void windowClosing(WindowEvent we)
{
helpFrame.setVisible(false);
£

£

6 6   

6 
  C

Ñ Trivial File Transfer Protocol (TFTP) is a file


transfer protocol, with the functionality of a very
basic form of File Transfer Protocol (FTP)

Ñ It was first defined in 1980.


mC
 m C

Ñ It is a simple protocol to transfer files. It has


been implemented on top of the User
Datagram Protocol (UDP) using port number
69.
Ñ TFTP only reads and writes files (or mail)
from/to a remote server. It cannot list
directories, and currently has no provisions for
user authentication.
Ñ TFTP supports three different transfer modes,
"netascii", "octet" and "mail", with the first two
corresponding to the "AMCII" and "image"
(binary) modes of the FTP protocol, and the
third was made obsolete by RFC 1350.
Ñ TFTP is based in part on the earlier protocol
EFTP, which was part of the PUP protocol suite.
 

Ñ TFTP is used to read files from, or write files to,


a remote server.

Ñ Due to the lack of security, it is dangerous over


the open Internet. Thus, TFTP is generally only
used on private, local networks.



Ñ TFTP cannot list directory contents.


Ñ TFTP has no authentication or encryption
mechanisms.
Ñ TFTP allows big data packets which may burst
and cause delay in transmission.
Ñ TFTP cannot download files larger than 1
Terabyte.
Ñ Mocket Programming:
A socket is one end-point of a two-
way communication link between two
programs running on the network. Mocket
classes are used to represent the
connection between a client program and a
server program. The java.net package
provides two classes--Mocket and Merver
Mocket--that implement the client side of the
connection and the server side of the
connection, respectively.
Ñ ›6 › Ú 
FTP is used to transfer files between computers on a
network. You can use FTP to exchange files between
computer accounts, transfer files between an account and
a desktop computer.

Ñ 6 ›6 › Ú 


The Trivial File Transfer Protocol (TFTP) allows a local host
to obtain files from a remote host but does not provide
reliability or security. It uses the fundamental packet
delivery services offered by UDP.
„   66
„66


Ñ FTP provides minimal Ñ TFTP does not use logins


security through user
logins
Ñ FTP provides a reliable
Ñ TFTP does not since it uses
service through its use of UDP
TCP
Ñ FTP uses two connections
Ñ TFTP uses one connection
1.Data transfer (stop and wait)
2. Control information
FTP uses plain text channel
Ñ „  packet:
Datagram packets are used to
implement a connectionless packet delivery
service. Each message is routed from one
machine to another based solely on information
contained within that packet. Multiple packets
sent from one machine to another might be
routed differently, and might arrive in any order.
Packet delivery is not guaranteed.
c › ›› 
An InputMtreamReader is a bridge from
byte streams to character streams: It reads
bytes and decodes them into characters using a
specified charset
    „ 
"
%& '(!!)*
%& '(!!)*
%& '(!+!)*
,- .. 8.
0
,- . (1 %&+ &+.23 &4. 5
0
„+&%6 1.74 „+&%6/I::*
-< .2374 -<2/:HJ3*
„+&%6 174 „+&%6.=/:HJ*
1.!&(1*
&+ 174 &+1!+„=:=1!+ +*
 ,7:*
<.%!,!&; m   C m
  mC "; @1*
C ,&% 8.74 C ,&% 1*
48.!(-E7:
0
88.!(-G/:HJ
,78.!(-*
*
.
,7/:HJ*
.74 -<2,3*
8.!&1.
174 „+&%6.=.!+=  11&..!+ .=/I:/*
1.!.11*
?
8.!.*
.74 -<2K3*
.7;)));!+ <.*
1.!.14„+&%6.=.!+=  11&..!+ .=/I:/*
1.!.*
?
?
 m  „ 
"
%& '(!!)*
%& '(!!)*
%& '(!+!)*
,- .. 8
0
,- . (1 %&+ &+.23 &4. 5
0
„+&%6 1.74 „+&%6/I:/*
,88&11& ,74 ,88&11&4
,&%1&<.%!*
<.%!,!&; m   C m
   ";*
&+ 87,!&1 *
C,,&% 8.74 C,,&%8*
<.%!,!&; m   C m
  mC ";*
87,!&1 *
-< .2374 -<28!+3*
.78!+ <.*
&+ 17,*
1.!.14
„+&%6.=.!+=  11&..!+ .=/I::*
4&,
0
.74 -<2/:HJ3*
„+&%6 174 „+&%6.=/:HJ*
1.!&(1*
174 &+1!+„=:=1!+ +*
81!A,.;)));
-&6*
8.!4&1!+ <.*
?
8.!.*
1.!.*
?
?
&(&.1,,"

.1,,"
 
 66 



J 

 A server is any thing that has some resource that can be shared.
1. Compute servers
2. Print server
3. Disk servers
4. Web servers

  A client is simply any other entity that wants to gain access to a


particular server.
m  „„ 
âThis class is used to encapsulate numerical IP address and
domain name for that address

 This class has no visible constructors so to create


InetAddress object we have to use one of the
available     .
  actory methods are static methods in a class 2 2
 
  
 static InetAddress getLocalHost() throws UnknownHostException

 static InetAddress getByName(String hostName)


throws UnknownHostException

 static InetAddress[] getAllByName(String hostName)


throws UnknownHostException
c 

import java.net.*;
class InetAddressTest
{
public static void main(String args[]) throws UnknownHostException
{
InetAddress Address=InetAddress.getLocalHost();
System.out.println(Address);
Address=InetAddress.getByName("yahoo.com");
System.out.println(Address);
InetAddress sw[]=InetAddress.getAllByName("www.nba.com");
for(int i=0;i<sw.length;i++)
System.out.println(sw[i]);
}
}
   

import java.io.*;
import java.net.*;
import java.lang.*;
public class https
{
public static void main(String args[]) throws Exception
{
ServerSocket ssoc=new ServerSocket(1111);
System.out.println("Server waits for client:\n");
Socket so=ssoc.accept();
System.out.println("client connected to Server :\n");

BufferedReader br=new BufferedReader(new


InputStreamReader(so.getInputStream()));

PrintWriter pw=new PrintWriter(so.getOutputStream(),true);

BufferedReader in=new BufferedReader(new


InputStreamReader(System.in));
int ch;
do
{
ch=Integer.parseInt(br.readLine());
String file;
byte line[]=null;
ile f;
switch(ch)
{
case 1: System.out.println("1.head");
file=br.readLine();
f=new ile(file);
int index=file.lastIndexOf(".");
String type=file.substring(index+1);
pw.println(type);
long length=f.length();
pw.println(length);
break;
case 2: System.out.println("2.post");
file=br.readLine();
System.out.println("message from client:\n");
System.out.println(file);
break;
case 3: System.out.println("3.get");
file=br.readLine();
ileInputStream fs=new ileInputStream(file);
while(fs.available()!=0)
{
if(fs.available()<1024)
line=new byte[fs.available()];
else
line=new byte[1024];
fs.read(line);
file=new String(line);
pw.println(file);
}
pw.println("***");
fs.close();
break;
case 4: System.out.println("4.delete");
file=br.readLine();
f=new ile(file);
f.delete();
break;
default: System.out.println("5.exit");
System.exit(0);
}
}
while(ch<=4);
so.close();
ssoc.close();
}
}
   


import java.io.*;
import java.net.*;
import java.lang.*;
public class httpc
{
public static void main(String args[]) throws Exception
{
Socket soc=new Socket("localhost",1111);
BufferedReader br=new BufferedReader(new
InputStreamReader(soc.getInputStream()));

PrintWriter pw=new PrintWriter(soc.getOutputStream(),true);

BufferedReader in=new BufferedReader(new


InputStreamReader(System.in));

System.out.println("server is connected:\n");
int ch;
do
{
System.out.println("COMMANDS");
System.out.println("\n 1.head \n 2.post \n 3.get \n4.delete\n 5.exit");
System.out.println("ENTER UR CHOICE:");
ch=Integer.parseInt(in.readLine());
byte line[]=null;
String file;
switch(ch)
{
case 1:pw.println("1");
file=in.readLine();
pw.println(file);
String type=br.readLine();
String length=br.readLine();
System.out.println(" ILE:"+file+"\nTYPE:"+type+"\nLEN
GTH:"+length);
break;

case 2: pw.println("2");
System.out.println("enter text to post");
file=in.readLine();
pw.println(file);
System.out.println("text is posted at server");
break;
case 3:pw.println("3");
System.out.println("enter file name to get");
file=in.readLine();
pw.println(file);
System.out.println("enter file name to save:");
file=in.readLine();
ileOutputStream fs=new ileOutputStream(file);
while(true)
{
String s=br.readLine();
if(s.equals("***"))
break;
int count=s.length();
if(count<1024)
line=new byte[1024];
line=s.getBytes();
fs.write(line);
}
fs.close();
System.out.println("\n file successfully tranfered:");
break;
case 4: pw.println("4");
System.out.println("enter the file to delete:");
file=in.readLine();
pw.println(file);
System.out.println("given file deleted:");
break;
default: pw.println("5");
System.exit(0);
}
}
while(ch<=4);
soc.close();
}
}
.1,,414"

S-ar putea să vă placă și