Networking with Java 1:Networking with Java 1:The Client SideThe Client Side
Introduction to NetworkingIntroduction to Networking
HUJI-CSHUJI-CSDBI 2008DBI 2008 3
ProtocolsProtocols
HiTCP connectionreplyGot the
time? GET http://www.cs.huji.ac.il/~dbi
2:00<file>
time
Client
Server
Hi TCP connection request
HUJI-CSHUJI-CSDBI 2008DBI 2008 4
Networking as LayersNetworking as Layers
Application (HTTP, FTP) DATA
Transport (TCP,UDP) HEADER DATA
Network (IP) HEADER HEADER DATA
Link (LINK) HEADER HEADER HEADER DATA
HUJI-CSHUJI-CSDBI 2008DBI 2008 5
TCP (TCP (Transmission-Control Protocol)Transmission-Control Protocol)
• Enables symmetric byte-stream transmission between two endpoints (applications)
• Reliable communication channel
• TCP perform these tasks:– connection establishment by handshake (relatively
slow)
– division to numbered packets (transferred by IP)
– error correction of packets (checksum)
– acknowledgement and retransmission of packets
– connection termination by handshake
HUJI-CSHUJI-CSDBI 2008DBI 2008 6
UDP (User Datagram Protocol)UDP (User Datagram Protocol)
• Enables direct datagram (packet) transmission from one endpoint to another
• No reliability (except for data correction)– sender does not wait for acknowledgements
– arrival order is not guaranteed
– arrival is not guaranteed
• Used when speed is essential, even in cost of reliability– e.g., streaming media, games, Internet telephony, etc.
HUJI-CSHUJI-CSDBI 2008DBI 2008 7
PortsPorts
• A computer may have several applications that communicate with applications on remote computers through the same physical connection to the network
• When receiving a packet, how can the computer tell which application is the destination?
• Solution: each channel endpoint is assigned a unique port that is known to both the computer and the other endpoint
HUJI-CSHUJI-CSDBI 2008DBI 2008 8
Ports (cont)Ports (cont)
• Thus, an endpoint application on the Internet is identified by– A host name → 32 bits IP-address
– A 16 bits port
• Why don’t we specify the port in a Web browser?
HUJI-CSHUJI-CSDBI 2008DBI 2008 9
Known PortsKnown Ports
• Some known ports are
– 20, 21: FTP
– 22: SSH
– 23: TELNET
– 25: SMTP
– 110: POP3
– 80: HTTP
– 119: NNTP
21 23 25 110 80 119
Client Application
web browsermail client
HUJI-CSHUJI-CSDBI 2008DBI 2008 10
SocketsSockets
• A socket is a construct that represents one end-point of a two-way communication channel between two programs running on the network
• Using sockets, the OS provides processes a file-like access to the channel– i.e., sockets are allocated a file descriptor, and
processes can access (read/write) the socket by specifying that descriptor
• A specific socket is identified by the machine's IP and a port within that machine
HUJI-CSHUJI-CSDBI 2008DBI 2008 11
Sockets (cont)Sockets (cont)
• A socket stores the IP and port number of the other end-point computer of the channel
• When writing to a socket, the written bytes are sent to the other computer and port (e.g., over TCP/IP)– That is, remote IP and port are attached to the packets
• When OS receives packets on the network, it uses their destination port to decide which socket should get the received bytes
Java SocketsJava Sockets
Low-Level Networking
HUJI-CSHUJI-CSDBI 2008DBI 2008 13
Java SocketsJava Sockets
• Java wraps OS sockets (over TCP) by the objects of class java.net.Socket
• new Socket(String remoteHost, int remotePort) creates a TCP socket and connects it to the remote host on the remote port (hand shake)
• Write and read using streams:– InputStream getInputStream()
– OutputStream getOutputStream()
HUJI-CSHUJI-CSDBI 2008DBI 2008 14
A Socket ExampleA Socket Example
import java.net.*;import java.io.*;
public class SimpleSocket {
public static void main(String[] args) throws IOException { ... next slide ...
}}
HUJI-CSHUJI-CSDBI 2008DBI 2008 15
Socket socket = new Socket("www.cs.huji.ac.il", 80); InputStream istream = socket.getInputStream(); OutputStream ostream = socket.getOutputStream();
String request = "GET /~dbi/admin.html HTTP/1.1\r\n" + "Host: www.cs.huji.ac.il\r\n" + "Connection: close\r\n\r\n"; ostream.write(request.getBytes());
byte[] response = new byte[4096]; int bytesRead = -1;
while ((bytesRead = istream.read(response)) >= 0) { System.out.write(response, 0, bytesRead); } socket.close();
Needed for forwarding for
example
HUJI-CSHUJI-CSDBI 2008DBI 2008 16
TimeoutTimeout
• You can set timeout values to blocking method read() of Socket
• Use the method socket.setSoTimeout(milliseconds)
• If timeout is reached before the method returns, java.net.SocketTimeoutException is thrown
Read more about Socket Class
Java Sockets and HTTPJava Sockets and HTTP
HUJI-CSHUJI-CSDBI 2008DBI 2008 18
HTTP Message StructureHTTP Message Structure
• A HTTP message has the following structure:
Request/Status-Line \r\nHeader1: value1 \r\nHeader2: value2 \r\n...HeaderN: valueN \r\n\r\n
Message-BodyMessage-Body
HUJI-CSHUJI-CSDBI 2008DBI 2008 19
Reading HTTP MessagesReading HTTP Messages
• Several ways to interpret the bytes of the body– Binary: images, compressed files, class files, ...
– Text: ASCII, Latin-1, UTF-8, ...
• Commonly, applications parse the headers of the message, and process the body according to the information supplied by the headers
– E.g., Content-Type, Content-Encoding, Transfer-Encoding
HUJI-CSHUJI-CSDBI 2008DBI 2008 20
An ExampleAn Example
HUJI-CSHUJI-CSDBI 2008DBI 2008 21
Parsing the HeadersParsing the Headers
• So how are the headers themselves represented?
• Headers of a HTTP message must be in US-ASCII format (1 byte per character)
HUJI-CSHUJI-CSDBI 2008DBI 2008 22
Example: Extracting the HeadersExample: Extracting the HeadersSocket socket = new Socket(argv[0], 80); InputStream istream = socket.getInputStream();OutputStream ostream = socket.getOutputStream();
String request = "GET / HTTP/1.1\r\n" + "Host: " + argv[0] + "\r\n" + "Connection: close\r\n\r\n"; ostream.write(request.getBytes());
StringBuffer headers = new StringBuffer(); int byteRead = 0;while ( !endOfHeaders(headers) && (byteRead = istream.read()) >= 0) { headers.append((char) byteRead); }System.out.print(headers);socket.close();
HUJI-CSHUJI-CSDBI 2008DBI 2008 23
Example: Extracting the Headers (cont)Example: Extracting the Headers (cont)
public static boolean endOfHeaders(StringBuffer headers) { int lastIndex = headers.length() - 1;
if (lastIndex < 3 || headers.charAt(lastIndex) != '\n') return false;
return (headers.substring(lastIndex - 3, lastIndex + 1) .equals("\r\n\r\n")); }
• Why did we (inefficiently) read one byte at a time?• Is there any way to avoid this inefficiency?
HUJI-CSHUJI-CSDBI 2008DBI 2008 24
Persistent ConnectionsPersistent Connections
• According to HTTP/1.1, a server does not have to close the connection after fulfilling your request
• One connection (socket) can be used for several requests and responses send more requests – even while earlier responses are being transferred
(pipelining)
– saves “slow start” time
• How can the client know when one response ends and a new one begins?
• To avoid persistency, require explicitly by the header Connection: close
Parsing URLsParsing URLs
HUJI-CSHUJI-CSDBI 2008DBI 2008 26
Query
ProtocolHost
NamePort
Number
File Nam
e
Reference
Working with URLsWorking with URLs
• URL (Uniform/Universal Resource Locator): a reference (address) to a resource on the Internet
http://www.cs.huji.ac.il:80/~dbi/main.html#notes
http://www.google.com/search?hl=en&q=dbi+huji&btnG=Search
HUJI-CSHUJI-CSDBI 2008DBI 2008 27
The Class URLThe Class URL
• The class URL is used for parsing URLs
• Constructing URLs:– URL w3c1 = new URL("http://www.w3.org/TR/");
– URL w3c2 = new URL("http","www.w3.org",80,"TR/");
– URL w3c3 = new URL(w3c2, "xhtml1/"); • If the string is not an absolute URL, then it is
considered relative to the URL
HUJI-CSHUJI-CSDBI 2008DBI 2008 28
Parsing URLsParsing URLs
• The following methods of URL can be used for parsing URLs
getProtocol(), getHost(), getPort(), getPath(), getFile(), getQuery(), getRef()
Read more about URL Class
HUJI-CSHUJI-CSDBI 2008DBI 2008 29
URLEncoderURLEncoder
• Contains a utility method encode for converting a string into an encoded format (used in URLs, e.g. for searches)
• To convert a string, each char is examined in turn: – Space is converted into a plus sign +– a-z, A-Z, 0-9, ., -, * and _ remain the same. – The bytes of all special characters are replaced by
hexadecimal numbers, preceded with %
• To decode an encoded string, use decode() of the class URLDecoder
Read more about URLEncoder Class
Class URLConnectionClass URLConnection
High-Level Networking
HUJI-CSHUJI-CSDBI 2008DBI 2008 31
The class URLConnectionThe class URLConnection
• To establish the actual resource, we can use the object URLConnection obtained by url.openConnection()
• If the protocol of the URL is HTTP, the returned object is of class HttpURLConnection
• This class encapsulates all socket management and HTTP directions required to obtain the resource
Read more about URLConnection Class
HUJI-CSHUJI-CSDBI 2008DBI 2008 32
public class ContentExtractor {
public static void main(String[] argv) throws Exception { URL url = new URL(argv[0]); System.out.println("Host: " + url.getHost()); System.out.println("Protocol: " + url.getProtocol()); System.out.println("----"); URLConnection con = url.openConnection(); InputStream stream = con.getInputStream(); byte[] data = new byte[4096]; int bytesRead = 0; while((bytesRead=stream.read(data))>=0) { System.out.write(data,0,bytesRead);}}}
HUJI-CSHUJI-CSDBI 2008DBI 2008 33
About URLConnectionAbout URLConnection
• The life cycle of a URLConnection object has two parts:– Before actual connection establishment
• Connection configuration
– After actual connection establishment• Content retrieval
• Passage from the first phase to the second is implicit– A result of calling some committing methods, like
getDate()
HUJI-CSHUJI-CSDBI 2008DBI 2008 34
About HttpURLConnectionAbout HttpURLConnection
• The HttpURLConnection class encapsulates all HTTP transaction over sockets, e.g.,– Content decoding
– Redirection
– Proxy indirection
• You can control requests by its methods
– setRequestMethod, setFollowRedirects, setRequestProperty, ...
Read more about HttpURLConnection Class
Top Related