Practical socketjava

686 views
601 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
686
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Host-to-host delivery means that IP takes packets from one host to another host. Datagram service analogy to the post office.
  • We need some way to identify computers on an internetwork The “dotted-quad” representation is just a more readable version of a real 32-bit number
  • Ports are analogous to phone extensions from main switchboard
  • Students should follow along with source code in the book
  • Students should follow along with source code in the book Server specified “well-known” port
  • Students should follow along with source code in the book
  • Students should follow along with source code in the book
  • Students should follow along with source code in the book
  • Students should follow along with source code in the book Client must specify server address (name or IP) and port
  • Students should follow along with source code in the book Socket has OutputStream for writing
  • Students should follow along with source code in the book accept( ) returns a new, connected Socket instance
  • Students should follow along with source code in the book Socket has InputStream for reading
  • Students should follow along with source code in the book Note that server closes connection socket, not listening socket
  • In echo application, how does server know when client is done considering it doesn’t know echo string length? Role of closer can (and is in HTTP) be reversed
  • TCP does not examine or modify the bytes
  • Inefficient: Need only 4 bits to represent values 0-9 Arithmetic manipulation: Cannot add and subtract strings. Must convert to primitive type. If you must convert then you lose arbitrary size advantage.
  • If the byte order is already big-endian, then the [hn]to[ns][sl]( ) functions are no-ops.
  • Practical socketjava

    1. 1. TCP/IP Sockets in Java: Practical Guide for Programmers Kenneth L. Calvert Michael J. Donahoo
    2. 2. Computer Chat <ul><li>How do we make computers talk? </li></ul><ul><li>How are they interconnected? </li></ul><ul><ul><li>Internet Protocol (IP) </li></ul></ul>
    3. 3. Internet Protocol (IP) <ul><li>Datagram (packet) protocol </li></ul><ul><li>Best-effort service </li></ul><ul><ul><li>Loss </li></ul></ul><ul><ul><li>Reordering </li></ul></ul><ul><ul><li>Duplication </li></ul></ul><ul><ul><li>Delay </li></ul></ul><ul><li>Host-to-host delivery </li></ul>
    4. 4. IP Address <ul><li>32-bit identifier </li></ul><ul><li>Dotted-quad: 192.118.56.25 </li></ul><ul><li>www.mkp.com -> 167.208.101.28 </li></ul><ul><li>Identifies a host interface (not a host) </li></ul>192.18.22.13 209.134.16.123
    5. 5. Transport Protocols <ul><li>Best-effort not sufficient! </li></ul><ul><li>Add services on top of IP </li></ul><ul><li>User Datagram Protocol (UDP) </li></ul><ul><ul><li>Data checksum </li></ul></ul><ul><ul><li>Best-effort </li></ul></ul><ul><li>Transmission Control Protocol (TCP) </li></ul><ul><ul><li>Data checksum </li></ul></ul><ul><ul><li>Reliable byte-stream delivery </li></ul></ul><ul><ul><li>Flow and congestion control </li></ul></ul>
    6. 6. Ports <ul><li>Identifying the ultimate destination </li></ul><ul><li>IP addresses identify hosts </li></ul><ul><li>Host has many applications </li></ul><ul><li>Ports (16-bit identifier) </li></ul>192.18.22.13 Port 80 25 23 Application WWW E-mail Telnet
    7. 7. Sockets <ul><li>Identified by protocol and local/remote address/port </li></ul><ul><li>Applications may refer to many sockets </li></ul>
    8. 8. <ul><li>Client: Initiates the connection </li></ul><ul><li>Server: Passively waits to respond </li></ul>Clients and Servers Client: Bob “ Hi. I’m Bob.” “ Nice to meet you, Jane.” Server: Jane “ Hi, Bob. I’m Jane”
    9. 9. TCP Client/Server Interaction <ul><li>Client </li></ul><ul><li>Create a TCP socket </li></ul><ul><li>Communicate </li></ul><ul><li>Close the connection </li></ul><ul><li>Server </li></ul><ul><li>Create a TCP socket </li></ul><ul><li>Repeatedly: </li></ul><ul><ul><li>Accept new connection </li></ul></ul><ul><ul><li>Communicate </li></ul></ul><ul><ul><li>Close the connection </li></ul></ul>Server starts by getting ready to receive client connections…
    10. 10. TCP Client/Server Interaction <ul><li>Client </li></ul><ul><li>Create a TCP socket </li></ul><ul><li>Communicate </li></ul><ul><li>Close the connection </li></ul><ul><li>Server </li></ul><ul><li>Create a TCP socket </li></ul><ul><li>Repeatedly: </li></ul><ul><ul><li>Accept new connection </li></ul></ul><ul><ul><li>Communicate </li></ul></ul><ul><ul><li>Close the connection </li></ul></ul>ServerSocket servSock = new ServerSocket(servPort);
    11. 11. TCP Client/Server Interaction <ul><li>Client </li></ul><ul><li>Create a TCP socket </li></ul><ul><li>Communicate </li></ul><ul><li>Close the connection </li></ul><ul><li>Server </li></ul><ul><li>Create a TCP socket </li></ul><ul><li>Repeatedly: </li></ul><ul><ul><li>Accept new connection </li></ul></ul><ul><ul><li>Communicate </li></ul></ul><ul><ul><li>Close the connection </li></ul></ul>for (;;) { Socket clntSock = servSock.accept();
    12. 12. TCP Client/Server Interaction <ul><li>Client </li></ul><ul><li>Create a TCP socket </li></ul><ul><li>Communicate </li></ul><ul><li>Close the connection </li></ul><ul><li>Server </li></ul><ul><li>Create a TCP socket </li></ul><ul><li>Repeatedly: </li></ul><ul><ul><li>Accept new connection </li></ul></ul><ul><ul><li>Communicate </li></ul></ul><ul><ul><li>Close the connection </li></ul></ul>Server is now blocked waiting for connection from a client
    13. 13. TCP Client/Server Interaction <ul><li>Client </li></ul><ul><li>Create a TCP socket </li></ul><ul><li>Communicate </li></ul><ul><li>Close the connection </li></ul><ul><li>Server </li></ul><ul><li>Create a TCP socket </li></ul><ul><li>Repeatedly: </li></ul><ul><ul><li>Accept new connection </li></ul></ul><ul><ul><li>Communicate </li></ul></ul><ul><ul><li>Close the connection </li></ul></ul>Later, a client decides to talk to the server…
    14. 14. TCP Client/Server Interaction <ul><li>Client </li></ul><ul><li>Create a TCP socket </li></ul><ul><li>Communicate </li></ul><ul><li>Close the connection </li></ul><ul><li>Server </li></ul><ul><li>Create a TCP socket </li></ul><ul><li>Repeatedly: </li></ul><ul><ul><li>Accept new connection </li></ul></ul><ul><ul><li>Communicate </li></ul></ul><ul><ul><li>Close the connection </li></ul></ul>Socket socket = new Socket(server, servPort);
    15. 15. TCP Client/Server Interaction <ul><li>Client </li></ul><ul><li>Create a TCP socket </li></ul><ul><li>Communicate </li></ul><ul><li>Close the connection </li></ul><ul><li>Server </li></ul><ul><li>Create a TCP socket </li></ul><ul><li>Repeatedly: </li></ul><ul><ul><li>Accept new connection </li></ul></ul><ul><ul><li>Communicate </li></ul></ul><ul><ul><li>Close the connection </li></ul></ul>OutputStream out = socket.getOutputStream(); out.write(byteBuffer);
    16. 16. TCP Client/Server Interaction <ul><li>Client </li></ul><ul><li>Create a TCP socket </li></ul><ul><li>Communicate </li></ul><ul><li>Close the connection </li></ul><ul><li>Server </li></ul><ul><li>Create a TCP socket </li></ul><ul><li>Repeatedly: </li></ul><ul><ul><li>Accept new connection </li></ul></ul><ul><ul><li>Communicate </li></ul></ul><ul><ul><li>Close the connection </li></ul></ul>Socket clntSock = servSock.accept();
    17. 17. TCP Client/Server Interaction <ul><li>Client </li></ul><ul><li>Create a TCP socket </li></ul><ul><li>Communicate </li></ul><ul><li>Close the connection </li></ul><ul><li>Server </li></ul><ul><li>Create a TCP socket </li></ul><ul><li>Repeatedly: </li></ul><ul><ul><li>Accept new connection </li></ul></ul><ul><ul><li>Communicate </li></ul></ul><ul><ul><li>Close the connection </li></ul></ul>InputStream in = clntSock.getInputStream(); recvMsgSize = in.read(byteBuffer);
    18. 18. TCP Client/Server Interaction <ul><li>Client </li></ul><ul><li>Create a TCP socket </li></ul><ul><li>Establish connection </li></ul><ul><li>Communicate </li></ul><ul><li>Close the connection </li></ul><ul><li>Server </li></ul><ul><li>Create a TCP socket </li></ul><ul><li>Bind socket to a port </li></ul><ul><li>Set socket to listen </li></ul><ul><li>Repeatedly: </li></ul><ul><ul><li>Accept new connection </li></ul></ul><ul><ul><li>Communicate </li></ul></ul><ul><ul><li>Close the connection </li></ul></ul>close(sock); close(clntSocket)
    19. 19. TCP Tidbits <ul><li>Client </li></ul><ul><li>out.write(“Hello Bob”) </li></ul><ul><li>in.read() -> “Hi Jane” </li></ul><ul><li>Server </li></ul><ul><li>in.read() -> “Hello ” </li></ul><ul><li>in.read() -> “Bob” </li></ul><ul><li>out.write(“Hi ”) </li></ul><ul><li>out.write(“Jane”) </li></ul><ul><li>Client knows server address and port </li></ul><ul><li>No correlation between send() and recv() </li></ul>
    20. 20. Closing a Connection <ul><li>Client </li></ul><ul><li>out.write( string ) </li></ul><ul><li>while (not received entire string) </li></ul><ul><li>in.read( buffer ) </li></ul><ul><li>out.write( buffer ) </li></ul><ul><li>close( socket ) </li></ul><ul><li>Server </li></ul><ul><li>in.read( buffer ) </li></ul><ul><li>while(client has not closed connection) </li></ul><ul><li>out.write( buffer ) </li></ul><ul><li>in.read( buffer ) </li></ul><ul><li>close( client socket ) </li></ul><ul><li>close() used to delimit communication </li></ul><ul><li>Analogous to EOF </li></ul>
    21. 21. Constructing Messages …beyond simple strings
    22. 22. TCP/IP Byte Transport <ul><li>TCP/IP protocols transports bytes </li></ul><ul><li>Application protocol provides semantics </li></ul>Application TCP/IP byte stream Application TCP/IP byte stream Here are some bytes. I don’t know what they mean. I’ll pass these to the app. It knows what to do.
    23. 23. Application Protocol <ul><li>Encode information in bytes </li></ul><ul><li>Sender and receiver must agree on semantics </li></ul><ul><li>Data encoding </li></ul><ul><ul><li>Primitive types: strings, integers, and etc. </li></ul></ul><ul><ul><li>Composed types: message with fields </li></ul></ul>
    24. 24. Primitive Types <ul><li>String </li></ul><ul><ul><li>Character encoding: ASCII, Unicode, UTF </li></ul></ul><ul><ul><li>Delimit: length vs. termination character </li></ul></ul>M o m 3 77 111 109 0 77 0 111 0 109 0 10
    25. 25. <ul><li>Integer </li></ul><ul><ul><li>Strings of character encoded decimal digits </li></ul></ul><ul><ul><ul><li>Advantage: 1. Human readable </li></ul></ul></ul><ul><ul><ul><li>2. Arbitrary size </li></ul></ul></ul><ul><ul><ul><li>Disadvantage: 1. Inefficient </li></ul></ul></ul><ul><ul><ul><li>2. Arithmetic manipulation </li></ul></ul></ul>Primitive Types 49 55 57 57 56 55 48 10 ‘ 1’ ‘7’ ‘9’ ‘9’ ‘8’ ‘7’ ‘0’
    26. 26. Primitive Types <ul><li>Integer </li></ul><ul><ul><li>Native representation </li></ul></ul><ul><ul><li>Network byte order (Big-Endian) </li></ul></ul><ul><ul><ul><li>Use for multi-byte, binary data exchange </li></ul></ul></ul><ul><ul><ul><li>htonl(), htons(), ntohl(), ntohs() </li></ul></ul></ul>0 0 92 246 4-byte two’s-complement integer 23,798 246 92 0 0 Big-Endian Little-Endian
    27. 27. Message Composition <ul><li>Message composed of fields </li></ul><ul><ul><li>Fixed-length fields </li></ul></ul><ul><ul><li>Variable-length fields </li></ul></ul>short short integer 1 2 e i k M

    ×