Sockets Java S Andrei Vancea - Conne Conne ected / ected / - - PDF document

sockets java s
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Java S

Andrei Vancea -

Sockets

April 17th, 2008

slide-2
SLIDE 2

Conne Conne Disconnec Disconnec

  • Connected mode: p

packets arrive all, i Phone)

  • Disconnected mod

Disconnected mod all, some packets m not guaranteed (e g not guaranteed (e.g

2

ected / ected / ted Modes ted Modes

path chosen and n correct order (e.g. e: path not chosen for e: path not chosen for may be missing, order g IP)

  • g. IP)

2

slide-3
SLIDE 3

S k Sock

  • Sockets are used t

potentially distribut potentially distribut

  • Sockets can be TC
  • The combination of

port number (RFC port number (RFC

3

k t ? kets?

to have two processes, ted, interact ted, interact CP/IP or UDP/IP f an IP address and a 793) 793)

3

slide-4
SLIDE 4

Java S Ja a S

  • In java.net package
  • java.net.Socket – c
  • java.net.ServerSoc
  • Implements serve

Implements serve

  • Waits for request
  • javax.net.ssl.SSLS

javax net ssl SSLS javax.net.ssl.SSLS

  • Secured sockets

4

Sockets Soc ets

e client socket cket er sockets er sockets ts from clients

  • cket,

erverSocket erverSocket

4

slide-5
SLIDE 5

J S Java S

  • java.net.DatagramS
  • socket for sendin

datagram packets datagram packets

  • java.net.MulticastS
  • useful for sending

multicast packets multicast packets

5

S k t Sockets

Socket g and receiving s (UDP) s (UDP)

  • cket

g and receiving IP s

5

slide-6
SLIDE 6

TC TC

  • R li bl

t d

  • Reliable stream de
  • Guarantees to deliv

Guarantees to deliv sent from one host duplication or losing duplication or losing

  • Uses positive ackn

guarantee reliability

6

CP CP

li i livery service ver a stream of data ver a stream of data to another without g data g data

  • wledgment o

f f y of packet transfers

6

slide-7
SLIDE 7

TC

Server

socket() bind() li t () listen() accept accept read() write() () close()

7

CP

Client Client

k t() socket()

connect() connect()

write() read() () close()

7

slide-8
SLIDE 8

TC TC

  • how to create a s

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

CP CP

socket??? socket???

8

slide-9
SLIDE 9

TC TC

  • how to create a s

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

CP CP

socket??? (WTH?) socket??? (WTH?)

< 0)

9

slide-10
SLIDE 10

TCP

Server

socket() bind() li t () new ServerSocket(…) listen() accept accept ServerSocket.accept() read() write() Socket.getInputStream(), Socket.getOutputStream() () close() Socket.close()

1

  • Java

Client Client

k t() socket()

connect()

new Socket(…)

connect()

write() read() Socket.getInputStream() Socket.getOutputStream() () close()

Socket.close()

slide-11
SLIDE 11

TC TC

  • in Java:

1

CP CP

1

slide-12
SLIDE 12

Soc

12

k t cket

2

slide-13
SLIDE 13

M th d Methods

  • l

()

  • close()
  • getInputStream()

getInputStream()

  • getOutputStream()
  • setSoTimeout()
  • S Ti

()

  • getSoTimeout()

1

t k s to know

3

slide-14
SLIDE 14

S S ServerS

  • Let’s forget the C e

Let s forget the C e

  • client/server.
  • Binds to a port.

14

S k t Socket

example... ;) example... ;)

4

slide-15
SLIDE 15

Exam

1

l mple

5

slide-16
SLIDE 16

S S ServerS

1

S k t Socket

6

slide-17
SLIDE 17

M th d Methods

  • close()
  • accept()
  • setSoTimeout()

setSoTimeout()

  • getSoTimeout()

g ()

1

t k s to know

7

slide-18
SLIDE 18

Exam

  • Telnet?

1

l mple

8

slide-19
SLIDE 19

S k tI t SocketInter

1

t (1/3) ractor (1/3)

9

slide-20
SLIDE 20

S k tI t SocketInter

2

t (2/3) ractor (2/3)

slide-21
SLIDE 21

3) (3/3 tor ( erac tInte cket Soc

21

slide-22
SLIDE 22

ver) Serv e (S mple Exam E

22

slide-23
SLIDE 23

Secure Soc Secure Soc

  • Add a layer of secu

y the underlying netw

  • I t

it P t ti

  • Integrity Protectio
  • Protects agains
  • tects aga s

messages by a

  • A th

ti ti

  • Authentication
  • Confidentiality

Confidentiality

  • SSL encrypts d

between client

2

ckets (SSL) ckets (SSL)

urity protections over y p work transport protocol

  • n

st modification of st

  • d cat o
  • an active wiretapper

data being sent and server

3

slide-24
SLIDE 24

SSLS SSLS

All constructor are protect p SSLSocketFactory is use SSLSocket instances

24

SSLSocket instances

S k t Socket

ed ed for constructing

4

slide-25
SLIDE 25

M th d Methods

  • startHandshake()

2

t k s to know

5

slide-26
SLIDE 26

le mpl Exa E

26

slide-27
SLIDE 27

UD UD

  • User Datagram Pro
  • Does not guarantee
  • D t
  • Datagrams may arr

appear duplicated,

  • Faster and more ef

that do not need gu that do not need gu

2

DP DP

  • tocol

e reliability or ordering i t f d rive out of order,

  • r go missing

fficient, for applications uaranteed delivery uaranteed delivery

7

slide-28
SLIDE 28

D t Datagram

2

S k t mSockets

8

slide-29
SLIDE 29

M th d Methods

  • close()
  • connect(...)
  • send(DatagramPac

send(DatagramPac

  • receive(DatagramP

( g

2

t k s to know

cket) cket) Packet)

9

slide-30
SLIDE 30

Exam

  • Echo server/client

3

l mple

slide-31
SLIDE 31

ver) Serv e (S mple Exam E

31

slide-32
SLIDE 32

nt) Clien le (C ampl Exa E

32

slide-33
SLIDE 33

M lti t Multicast

  • Used for sending a

multicast packet

  • A MulticastSocket i

with additional capa with additional capa "groups" of other m internet internet.

3

t S k t t Sockets

nd receiving IP is a DatagramSocket, abilities for joining abilities for joining multicast hosts on the

3

slide-34
SLIDE 34

M lti t Multicast

  • A multicast group is

D IP (224.0.0.0 to 2 address and by a s number

  • When one sends a

When one sends a multicast group, all to that host and por to that host and por message

34

t S k t t Sockets

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

slide-35
SLIDE 35

M lti Multicas

3

tS k t stSocket

5

slide-36
SLIDE 36

M th d Methods

  • joinGroup
  • leaveGroup

leaveGroup

3

t k s to know

6

slide-37
SLIDE 37

ple amp Exa

37

slide-38
SLIDE 38

W t Want m

  • How would you ma

server?

  • How would you ma

How would you ma server?

3

? more?

ake a small Web ake a small FTP ake a small FTP

8