Java S
Andrei Vancea -
Sockets
April 17th, 2008
Sockets Java S Andrei Vancea - Conne Conne ected / ected / - - PDF document
April 17th, 2008 Sockets Java S Andrei Vancea - Conne Conne ected / ected / Disconnec Disconnec ted Modes ted Modes Connected mode: p path chosen and packets arrive all, i n correct order (e.g. Phone) Disconnected mod
Andrei Vancea -
April 17th, 2008
packets arrive all, i Phone)
Disconnected mod all, some packets m not guaranteed (e g not guaranteed (e.g
2
path chosen and n correct order (e.g. e: path not chosen for e: path not chosen for may be missing, order g IP)
2
potentially distribut potentially distribut
port number (RFC port number (RFC
3
to have two processes, ted, interact ted, interact CP/IP or UDP/IP f an IP address and a 793) 793)
3
Implements serve
javax net ssl SSLS javax.net.ssl.SSLS
4
e client socket cket er sockets er sockets ts from clients
erverSocket erverSocket
4
datagram packets datagram packets
multicast packets multicast packets
5
Socket g and receiving s (UDP) s (UDP)
g and receiving IP s
5
t d
Guarantees to deliv sent from one host duplication or losing duplication or losing
guarantee reliability
6
li i livery service ver a stream of data ver a stream of data to another without g data g data
f f y of packet transfers
6
Server
socket() bind() li t () listen() accept accept read() write() () close()
7
Client Client
k t() socket()
connect() connect()
write() read() () close()
7
how 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
socket??? socket???
8
how 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
socket??? (WTH?) socket??? (WTH?)
< 0)
9
Server
socket() bind() li t () new ServerSocket(…) listen() accept accept ServerSocket.accept() read() write() Socket.getInputStream(), Socket.getOutputStream() () close() Socket.close()
1
Client Client
k t() socket()
connect()
new Socket(…)
connect()
write() read() Socket.getInputStream() Socket.getOutputStream() () close()
Socket.close()
1
1
12
2
()
getInputStream()
()
1
3
Let s forget the C e
14
example... ;) example... ;)
4
1
5
1
6
setSoTimeout()
g ()
1
7
1
8
1
9
2
21
22
y the underlying netw
it P t ti
messages by a
ti ti
Confidentiality
between client
2
urity protections over y p work transport protocol
st modification of st
data being sent and server
3
All constructor are protect p SSLSocketFactory is use SSLSocket instances
24
SSLSocket instances
ed ed for constructing
4
2
5
26
appear duplicated,
that do not need gu that do not need gu
2
e reliability or ordering i t f d rive out of order,
fficient, for applications uaranteed delivery uaranteed delivery
7
2
8
send(DatagramPac
( g
2
cket) cket) Packet)
9
3
31
32
multicast packet
with additional capa with additional capa "groups" of other m internet internet.
3
nd receiving IP is a DatagramSocket, abilities for joining abilities for joining multicast hosts on the
3
D IP (224.0.0.0 to 2 address and by a s number
When one sends a multicast group, all to that host and por to that host and por message
34
s specified by a class 239.255.255.255) standard UDP port message to a message to a l subscribing recipients rt receive the rt receive the
4
3
5
leaveGroup
3
6
37
server?
How would you ma server?
3
ake a small Web ake a small FTP ake a small FTP
8