Upload
truongdat
View
225
Download
0
Embed Size (px)
Citation preview
Java SAndrei Vancea -
SocketsApril 17th, 2008
ConneConneDisconnecDisconnec• Connected mode: p
packets arrive all, iPhone)
• Disconnected modDisconnected modall, some packets mnot guaranteed (e gnot guaranteed (e.g
2
ected /ected / ted Modested Modespath chosen and n correct order (e.g.
e: path not chosen fore: path not chosen for may be missing, order g IP)g. IP)
2
S kSock
• Sockets are used tpotentially distributpotentially distribut
• Sockets can be TC
• The combination ofport number (RFCport number (RFC
3
k t ?kets?
to have two processes, ted, interactted, interact
CP/IP or UDP/IP
f an IP address and a 793)793)
3
Java SJa a S• In java.net package
• java.net.Socket – c
• java.net.ServerSoc
• Implements serveImplements serve
• Waits for request
• javax.net.ssl.SSLSjavax net ssl SSLSjavax.net.ssl.SSLS
• Secured sockets
4
SocketsSoc etse
client socket
cket
er socketser sockets
ts from clients
ocket, erverSocketerverSocket
4
J SJava S• java.net.DatagramS
• socket for sendindatagram packetsdatagram packets
• java.net.MulticastS
• useful for sendingmulticast packetsmulticast packets
5
S k tSocketsSocket
g and receiving s (UDP)s (UDP)
ocket
g and receiving IP ss
5
TCTC
• R li bl t d• Reliable stream de
• Guarantees to delivGuarantees to delivsent from one host duplication or losingduplication or losing
• Uses positive acknguarantee reliability
6
CPCP
li ilivery service
ver a stream of dataver a stream of data to another without g datag data
owledgment o f fy of packet transfers
6
TCServer
socket()
bind()
li t ()listen()
acceptaccept
read()
write()()
close()
7
CPClientClient
k t()socket()
connect()connect()
write()
read()()
close()
7
TCTC• how to create a show to create a s
int sockfd, portno, n;struct sockaddr_in serv_addr;t t h t t *struct hostent *server;
char buffer[256];if (argc < 3) {
fprintf(stderr "usage %s hostname port\n" argv[0]);fprintf(stderr, usage %s hostname port\n , argv[0]);exit(0);
}portno = atoi(argv[2]);sockfd = socket(AF INET SOCK STREAM 0);sockfd socket(AF_INET, SOCK_STREAM, 0);if (sockfd < 0)
error("ERROR opening socket");server = gethostbyname(argv[1]);if (server == NULL) {( ) {
fprintf(stderr,"ERROR, no such host\n");exit(0);
}
8
CPCPsocket???socket???
8
TCTC• how to create a show to create a s
bzero((char *) &serv_addr, sizeof(serv_addr));serv addr.sin family = AF INET;serv_addr.sin_family AF_INET;bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,server->h_length);
serv addr sin port = htons(portno);serv_addr.sin_port = htons(portno);if (connect(sockfd,&serv_addr,sizeof(serv_addr))
error("ERROR connecting");
9
CPCPsocket??? (WTH?)socket??? (WTH?)
< 0)
9
TCP Server
socket()
bind()
li t ()
new ServerSocket(…)
listen()
acceptacceptServerSocket.accept()
read()
write()
Socket.getInputStream(),Socket.getOutputStream()
()
close()Socket.close()
11
- JavaClientClient
k t()socket()
connect()
new Socket(…)
connect()
write()
read()
Socket.getInputStream()Socket.getOutputStream()
()
close() Socket.close()
00
TCTC• in Java:
1
CPCP
1
SSoc
12
k tcket
2
M th dMethods
• l ()• close()
• getInputStream()getInputStream()
• getOutputStream()
• setSoTimeout()
• S Ti ()• getSoTimeout()
1
t ks to know
3
S SServerS
• Let’s forget the C eLet s forget the C e
• client/server.
• Binds to a port.
14
S k tSocket
example... ;)example... ;)
4
EExam
1
lmple
5
S SServerS
1
S k tSocket
6
M th dMethods
• close()
• accept()
• setSoTimeout()setSoTimeout()
• getSoTimeout()g ()
1
t ks to know
7
EExam
• Telnet?
1
lmple
8
S k tI tSocketInter
1
t (1/3)ractor (1/3)
9
S k tI tSocketInter
2
t (2/3)ractor (2/3)
0
3)(3
/3to
r (er
actIn
teck
etS
oc
21
ver)
Ser
ve
(Sm
ple
Exa
mE
222
Secure SocSecure Soc• Add a layer of secuy
the underlying netw
• I t it P t ti• Integrity Protectio
• Protects againsotects aga smessages by a
• A th ti ti• Authentication
• ConfidentialityConfidentiality
• SSL encrypts dbetween client
2
ckets (SSL)ckets (SSL)urity protections over y pwork transport protocol
on
st modification of st od cat o oan active wiretapper
data being sent and server 3
SSLSSSLS
All constructor are protectpSSLSocketFactory is useSSLSocket instances
24
SSLSocket instances
S k tSocket
eded for constructing
4
M th dMethods
• startHandshake()
2
t ks to know
5
lem
plE
xaE
26
UDUD• User Datagram Pro
• Does not guarantee
• D t• Datagrams may arrappear duplicated,
• Faster and more efthat do not need guthat do not need gu
2
DPDPotocol
e reliability or ordering
i t f drive out of order, or go missing
fficient, for applications uaranteed deliveryuaranteed delivery
7
D tDatagram
2
S k tmSockets
8
M th dMethods
• close()
• connect(...)
• send(DatagramPacsend(DatagramPac
• receive(DatagramP( g
2
t ks to know
cket)cket)
Packet))
9
EExam
• Echo server/client
3
lmple
0
ver)
Ser
ve
(Sm
ple
Exa
mE
31
nt)
Clie
nle
(Cam
plE
xaE
322
M lti tMulticast
• Used for sending amulticast packet
• A MulticastSocket iwith additional capawith additional capa"groups" of other minternetinternet.
3
t S k tt Sockets
nd receiving IP
is a DatagramSocket, abilities for joiningabilities for joining
multicast hosts on the
3
M lti tMulticast• A multicast group is
D IP (224.0.0.0 to 2address and by a snumber
• When one sends aWhen one sends a multicast group, allto that host and porto that host and pormessage
34
t S k tt Socketss specified by a class 239.255.255.255) standard UDP port
message to amessage to a l subscribing recipients rt receive thert receive the
4
M ltiMulticas
3
tS k tstSocket
5
M th dMethods
• joinGroup
• leaveGroupleaveGroup
3
t ks to know
6
ple
amp
Exa
37
W tWant m
• How would you maserver?
• How would you maHow would you maserver?
3
?more?
ake a small Web
ake a small FTPake a small FTP
8