Documente Academic
Documente Profesional
Documente Cultură
m
Ñ Mocket Programming
Ñ Exception Handling
m"
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.
Ñ Mocket communication
Ñ Exception Handling
Ñ IO
Ñ Multi Threading
m
InputMtream
Ñ Returns an input stream for socket.
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) :
or
Ñ 2.)By Implementing RUNNABLE interface
Ñ Mtart(),run(),sleep(),stop(),join()«e.t.c.
mm
m C
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
6
" # $
%
#
) !
) !
*
&
&
+ , !) !
"
ets the field
names from
# the tables
" '&(6&
" "
&&
&&
ets the
values from
the tables
" "
&&
&&
#
J
Field 1 Field 2
xxxx yyyyy
zxzxzx zzzzz
J
&(&.1&+&%"
%&'(!!)*
%&'(!!)*
%&'(!.A!)*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&
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.
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&&(*
&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)*
,-..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 .&.*
?
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class telnet extends WindowAdapter implements ActionListener,KeyListener {
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");
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
.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
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()));
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"