sockets java s
play

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


  1. April 17th, 2008 Sockets Java S Andrei Vancea -

  2. 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 Disconnected mod e: path not chosen for e: path not chosen for all, some packets m may be missing, order not guaranteed (e.g not guaranteed (e g g. IP) g IP) 2 2

  3. S Sock k k t ? kets? • Sockets are used t to have two processes, potentially distribut potentially distribut ted, interact ted, interact • Sockets can be TC CP/IP or UDP/IP • The combination of f an IP address and a port number (RFC port number (RFC 793) 793) 3 3

  4. Ja a S Java S Sockets Soc ets • In java.net package e • java.net.Socket – c client socket • java.net.ServerSoc cket • Implements serve Implements serve er sockets er sockets • Waits for request ts from clients • javax.net.ssl.SSLS ocket, javax net ssl SSLS javax.net.ssl.SSLS erverSocket erverSocket • Secured sockets 4 4

  5. J Java S S Sockets S k t • java.net.DatagramS Socket • socket for sendin g and receiving datagram packets datagram packets s (UDP) s (UDP) • java.net.MulticastS ocket • useful for sending g and receiving IP multicast packets multicast packets s 5 5

  6. TC TC CP CP • Reliable stream de • R li bl t d livery service li i • Guarantees to deliv Guarantees to deliv ver a stream of data ver a stream of data sent from one host to another without duplication or losing duplication or losing g data g data • Uses positive ackn owledgment o guarantee reliability y of packet transfers f f 6 6

  7. TC CP Server Client Client socket() bind() li t listen() () socket() k t() accept accept connect() connect() read() write() write() () read() () close() close() 7 7

  8. TC TC CP CP • how to create a s how to create a s socket??? socket??? int sockfd, portno, n; struct sockaddr_in serv_addr; struct hostent *server; t t h t t * 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 8

  9. TC TC CP CP • how to create a s how to create a s socket??? (WTH?) socket??? (WTH?) 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)) < 0) error("ERROR connecting"); 9 9

  10. TCP - Java Server Client Client socket() bind() new ServerSocket(…) li t listen() () socket() k t() new Socket(…) accept accept connect() connect() ServerSocket.accept() read() write() Socket.getInputStream() Socket.getInputStream(), Socket.getOutputStream() Socket.getOutputStream() write() () read() () Socket.close() Socket.close() close() close() 1 0

  11. CP CP TC TC 1 1 • in Java:

  12. k t cket Soc 12 2

  13. M th d Methods s to know t k • • close() l () • getInputStream() getInputStream() • getOutputStream() • setSoTimeout() • getSoTimeout() • S Ti () 1 3

  14. S ServerS S S Socket k t • Let’s forget the C e Let s forget the C e example... ;) example... ;) • client/server. • Binds to a port. 14 4

  15. mple l Exam 5 1

  16. Socket k t S ServerS S 6 1 S

  17. M th d Methods s to know t k • close() • accept() • setSoTimeout() setSoTimeout() • getSoTimeout() g () 1 7

  18. mple l Exam 8 1 • Telnet?

  19. ractor (1/3) (1/3) t SocketInter 9 1 k tI t S

  20. ractor (2/3) (2/3) t SocketInter 0 2 k tI t S

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

  22. 22 ver) Serv e (S mple Exam E

  23. Secure Soc Secure Soc ckets (SSL) ckets (SSL) • Add a layer of secu y urity protections over y p the underlying netw work transport protocol • I t • Integrity Protectio it P t ti on • Protects agains otects aga s st modification of st od cat o o messages by a an active wiretapper • Authentication • A th ti ti • Confidentiality Confidentiality • SSL encrypts d data being sent between client and server 2 3

  24. SSLS SSLS S Socket k t All constructor are protect p ed SSLSocketFactory is use ed for constructing SSLSocket instances SSLSocket instances 24 4

  25. s to know k t 5 Methods 2 • startHandshake() M th d

  26. 26 le mpl Exa E

  27. UD UD DP DP • User Datagram Pro otocol • Does not guarantee e reliability or ordering • Datagrams may arr • D t rive out of order, i t f d appear duplicated, or go missing • Faster and more ef fficient, for applications that do not need gu that do not need gu uaranteed delivery uaranteed delivery 2 7

  28. mSockets k t S Datagram 8 2 D t

  29. Methods M th d s to know t k • close() • connect(...) • send(DatagramPac send(DatagramPac cket) cket) • receive(DatagramP ( g Packet) 2 9

  30. mple l Exam 0 3 • Echo server/client

  31. 31 ver) Serv e (S mple Exam E

  32. 32 nt) Clien le (C ampl Exa E

  33. M lti Multicast t t Sockets t S k t • Used for sending a nd receiving IP multicast packet • A MulticastSocket i is a DatagramSocket, with additional capa with additional capa abilities for joining abilities for joining "groups" of other m multicast hosts on the internet. internet 3 3

  34. M lti Multicast t Sockets t t S k t • A multicast group is s specified by a class D IP (224.0.0.0 to 2 239.255.255.255) address and by a s standard UDP port number • When one sends a When one sends a message to a message to a multicast group, all l subscribing recipients to that host and por to that host and por rt receive the rt receive the message 34 4

  35. k t stSocket tS Multicas 5 3 M lti

  36. M th d Methods s to know t k • joinGroup • leaveGroup leaveGroup 3 6

  37. 37 ple amp Exa

  38. W Want m t more? ? • How would you ma ake a small Web server? • How would you ma How would you ma ake a small FTP ake a small FTP server? 3 8

Recommend


More recommend