TCPIP Sockets in Java Practical Guide for Programmers

  • Slides: 27
Download presentation
TCP/IP Sockets in Java: Practical Guide for Programmers Kenneth L. Calvert Michael J. Donahoo

TCP/IP Sockets in Java: Practical Guide for Programmers Kenneth L. Calvert Michael J. Donahoo

Computer Chat n How do we make computers talk? n How are they interconnected?

Computer Chat n How do we make computers talk? n How are they interconnected? Internet Protocol (IP)

Internet Protocol (IP) n n Datagram (packet) protocol Best-effort service n n n Loss

Internet Protocol (IP) n n Datagram (packet) protocol Best-effort service n n n Loss Reordering Duplication Delay Host-to-host delivery

IP Address n 32 -bit identifier Dotted-quad: 192. 118. 56. 25 www. mkp. com

IP Address n 32 -bit identifier Dotted-quad: 192. 118. 56. 25 www. mkp. com -> 167. 208. 101. 28 n Identifies a host interface (not a host) n n 192. 18. 22. 13 209. 134. 16. 123

Transport Protocols Best-effort not sufficient! n n Add services on top of IP User

Transport Protocols Best-effort not sufficient! n n Add services on top of IP User Datagram Protocol (UDP) n n n Data checksum Best-effort Transmission Control Protocol (TCP) n n n Data checksum Reliable byte-stream delivery Flow and congestion control

Ports Identifying the ultimate destination n IP addresses identify hosts Host has many applications

Ports Identifying the ultimate destination n IP addresses identify hosts Host has many applications Ports (16 -bit identifier) Application WWW Port 80 E-mail Telnet 25 23 192. 18. 22. 13

Sockets n n Identified by protocol and local/remote address/port Applications may refer to many

Sockets n n Identified by protocol and local/remote address/port Applications may refer to many sockets

Clients and Servers n Client: Initiates the connection Client: Bob Server: Jane “Hi. I’m

Clients and Servers n Client: Initiates the connection Client: Bob Server: Jane “Hi. I’m Bob. ” “Hi, Bob. I’m Jane” “Nice to meet you, Jane. ” n Server: Passively waits to respond

TCP Client/Server Interaction Server starts by getting ready to receive client connections… 1. 2.

TCP Client/Server Interaction Server starts by getting ready to receive client connections… 1. 2. 3. Client Create a TCP socket Communicate Close the connection Server 1. 2. Create a TCP socket Repeatedly: a. Accept new connection b. Communicate c. Close the connection

TCP Client/Server Interaction Server. Socket serv. Sock = new Server. Socket(serv. Port); 1. 2.

TCP Client/Server Interaction Server. Socket serv. Sock = new Server. Socket(serv. Port); 1. 2. 3. Client Create a TCP socket Communicate Close the connection Server 1. 2. Create a TCP socket Repeatedly: a. Accept new connection b. Communicate c. Close the connection

TCP Client/Server Interaction for (; ; ) { Socket clnt. Sock = serv. Sock.

TCP Client/Server Interaction for (; ; ) { Socket clnt. Sock = serv. Sock. accept(); 1. 2. 3. Client Create a TCP socket Communicate Close the connection Server 1. 2. Create a TCP socket Repeatedly: a. Accept new connection b. Communicate c. Close the connection

TCP Client/Server Interaction Server is now blocked waiting for connection from a client 1.

TCP Client/Server Interaction Server is now blocked waiting for connection from a client 1. 2. 3. Client Create a TCP socket Communicate Close the connection Server 1. 2. Create a TCP socket Repeatedly: a. Accept new connection b. Communicate c. Close the connection

TCP Client/Server Interaction Later, a client decides to talk to the server… 1. 2.

TCP Client/Server Interaction Later, a client decides to talk to the server… 1. 2. 3. Client Create a TCP socket Communicate Close the connection Server 1. 2. Create a TCP socket Repeatedly: a. Accept new connection b. Communicate c. Close the connection

TCP Client/Server Interaction Socket socket = new Socket(server, serv. Port); 1. 2. 3. Client

TCP Client/Server Interaction Socket socket = new Socket(server, serv. Port); 1. 2. 3. Client Create a TCP socket Communicate Close the connection Server 1. 2. Create a TCP socket Repeatedly: a. Accept new connection b. Communicate c. Close the connection

TCP Client/Server Interaction Output. Stream out = socket. get. Output. Stream(); out. write(byte. Buffer);

TCP Client/Server Interaction Output. Stream out = socket. get. Output. Stream(); out. write(byte. Buffer); 1. 2. 3. Client Create a TCP socket Communicate Close the connection Server 1. 2. Create a TCP socket Repeatedly: a. Accept new connection b. Communicate c. Close the connection

TCP Client/Server Interaction Socket clnt. Sock = serv. Sock. accept(); 1. 2. 3. Client

TCP Client/Server Interaction Socket clnt. Sock = serv. Sock. accept(); 1. 2. 3. Client Create a TCP socket Communicate Close the connection Server 1. 2. Create a TCP socket Repeatedly: a. Accept new connection b. Communicate c. Close the connection

TCP Client/Server Interaction Input. Stream in = clnt. Sock. get. Input. Stream(); recv. Msg.

TCP Client/Server Interaction Input. Stream in = clnt. Sock. get. Input. Stream(); recv. Msg. Size = in. read(byte. Buffer); 1. 2. 3. Client Create a TCP socket Communicate Close the connection Server 1. 2. Create a TCP socket Repeatedly: a. Accept new connection b. Communicate c. Close the connection

TCP Client/Server Interaction close(sock); 1. 2. 3. 4. Client Create a TCP socket Establish

TCP Client/Server Interaction close(sock); 1. 2. 3. 4. Client Create a TCP socket Establish connection Communicate Close the connection close(clnt. Socket) 1. 2. 3. 4. Server Create a TCP socket Bind socket to a port Set socket to listen Repeatedly: a. Accept new connection b. Communicate c. Close the connection

TCP Tidbits n n Client knows server address and port No correlation between send()

TCP Tidbits n n Client knows server address and port No correlation between send() and recv() Client out. write(“Hello Bob”) Server in. read() -> “Hello ” in. read() -> “Bob” out. write(“Hi ”) out. write(“Jane”) in. read() -> “Hi Jane”

Closing a Connection n n close() used to delimit communication Analogous to EOF Client

Closing a Connection n n close() used to delimit communication Analogous to EOF Client Server out. write(string) while (not received entire string) in. read(buffer) out. write(buffer) in. read(buffer) while(client has not closed connection) out. write(buffer) in. read(buffer) close(socket) close(client socket)

Constructing Messages …beyond simple strings

Constructing Messages …beyond simple strings

TCP/IP Byte Transport n TCP/IP protocols transports bytes Application byte stream TCP/IP n Here

TCP/IP Byte Transport n TCP/IP protocols transports bytes Application byte stream TCP/IP n Here are some bytes. I don’t know what they mean. TCP/IP I’ll pass these to the app. It knows what to do. Application protocol provides semantics

Application Protocol n n n Encode information in bytes Sender and receiver must agree

Application Protocol n n n Encode information in bytes Sender and receiver must agree on semantics Data encoding n n Primitive types: strings, integers, and etc. Composed types: message with fields

Primitive Types n String n n Character encoding: ASCII, Unicode, UTF Delimit: length vs.

Primitive Types n String n n Character encoding: ASCII, Unicode, UTF Delimit: length vs. termination character 0 3 77 0 111 0 109 M o m 77 111 109 0 10 n

Primitive Types n Integer n Strings of character encoded decimal digits 49 55 57

Primitive Types n Integer n Strings of character encoded decimal digits 49 55 57 57 56 55 48 10 ‘ 1’ ‘ 7’ ‘ 9’ ‘ 8’ ‘ 7’ ‘ 0’ n n Advantage: n Disadvantage: 1. 2. Human readable Arbitrary size Inefficient Arithmetic manipulation

Primitive Types n Integer n Native representation Little-Endian 0 0 92 246 23, 798

Primitive Types n Integer n Native representation Little-Endian 0 0 92 246 23, 798 Big-Endian n 246 92 0 0 4 -byte two’s-complement integer Network byte order (Big-Endian) n n Use for multi-byte, binary data exchange htonl(), htons(), ntohl(), ntohs()

Message Composition n Message composed of fields n Fixed-length fields integer n M short

Message Composition n Message composed of fields n Fixed-length fields integer n M short Variable-length fields i k e 1 2 n