Networking with Java Introduction to Networking Protocols Hi

  • Slides: 50
Download presentation
Networking with Java

Networking with Java

Introduction to Networking

Introduction to Networking

Protocols Hi TCP connection request Hi TCP connection reply Got the time? GET http:

Protocols Hi TCP connection request Hi TCP connection reply Got the time? GET http: //www. cs. huji. ac. il/~dbi 2: 00 <file> time

Internet Architecture Model Application (HTTP, FTP) DATA Transport (TCP, UDP) Network (IP) Link (LINK)

Internet Architecture Model Application (HTTP, FTP) DATA Transport (TCP, UDP) Network (IP) Link (LINK) HEADER DATA HEADER DATA

TCP (Transmission-Control Protocol) • Enables symmetric byte-stream transmission between two endpoints (applications) • Reliable

TCP (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

UDP (User Datagram Protocol) • Enables direct datagram (packet) transmission from one endpoint to

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.

Ports • A computer may have several applications that communicate with applications on remote

Ports • 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

Ports (cont) • Thus, an endpoint application on the Internet is identified by -

Ports (cont) • Thus, an endpoint application on the Internet is identified by - A host name → 32 bits IP-address - A 16 bits port • Q: Why don’t we specify the port in a Web browser?

Known Ports Client Application • Some known ports are - 20, 21: FTP mail

Known Ports Client Application • Some known ports are - 20, 21: FTP mail client - 23: TELNET web browser - 25: SMTP - 110: POP 3 - 80: HTTP - 119: NNTP 21 23 25 110 80 119

Sockets • A socket is a construct that represents one end-point of a two-way

Sockets • 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

Sockets (cont) • A socket stores the IP and port number of the other

Sockets (cont) • A socket stores the IP and port number of the other endpoint 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

Client-Server Model • A common paradigm for distributed applications • Asymmetry in connection establishment:

Client-Server Model • A common paradigm for distributed applications • Asymmetry in connection establishment: - Server waits for client requests (daemon) at a well known address (IP+port) - Connection is established upon client request • Once the connection is made, it can be either symmetric (TELNET) or asymmetric (HTTP) • For example: Web servers and browsers

Client-Server Interaction

Client-Server Interaction

Client-Server Interaction

Client-Server Interaction

Client-Server Interaction

Client-Server Interaction

Client-Server Interaction

Client-Server Interaction

Client-Server Interaction

Client-Server Interaction

Java Sockets Low-Level Networking

Java Sockets Low-Level Networking

Java Sockets • Java wraps OS sockets (over TCP) by the objects of class

Java Sockets • Java wraps OS sockets (over TCP) by the objects of class java. net. Socket • new Socket(String remote. Host, int remote. Port) creates a TCP socket and connects it to the remote host on the remote port (hand shake) • Write and read using streams: - Input. Stream get. Input. Stream() - Output. Stream get. Output. Stream()

A Socket Example import java. net. *; import java. io. *; public class Simple.

A Socket Example import java. net. *; import java. io. *; public class Simple. Socket { public static void main(String[] args) throws IOException { . . . next slide. . . } }

Socket socket = new Socket("www. cs. huji. ac. il", 80); Input. Stream istream =

Socket socket = new Socket("www. cs. huji. ac. il", 80); Input. Stream istream = socket. get. Input. Stream(); Output. Stream ostream = socket. get. Output. Stream(); String request = "GET /~dbi/admin. html HTTP/1. 1rn" + "Host: www. cs. huji. ac. ilrn" + "Connection: closern"; ostream. write(request. get. Bytes()); byte[] response = new byte[4096]; int bytes. Read = -1; while ((bytes. Read = istream. read(response)) >= 0) { System. out. write(response, 0, bytes. Read); } socket. close();

Java Server. Socket • Server. Socket represents a socket that listens and waits for

Java Server. Socket • Server. Socket represents a socket that listens and waits for requests from clients • Construction: - new Server. Socket(int port) - Why do want to specify the port? • Listen and accept incoming connections - Socket accept() - returns a new socket (with a new port) for the new channel - blocks until connection is made

public class Echo. Server { public static void main(String[] args) throws IOException { Server.

public class Echo. Server { public static void main(String[] args) throws IOException { Server. Socket server. Socket = new Server. Socket(8000); Socket socket = null; while (true) { try {. . . next slide. . . } catch (IOException exp) {. . . } finally { try {if (!socket. is. Closed()) socket. close(); } catch (IOException e) {} } }}}

socket = server. Socket. accept(); String client. Name = socket. get. Inet. Address(). get.

socket = server. Socket. accept(); String client. Name = socket. get. Inet. Address(). get. Host. Name(); Buffered. Reader reader = new Buffered. Reader( new Input. Stream. Reader(socket. get. Input. Stream())); Print. Stream writer = new Print. Stream(socket. get. Output. Stream()); writer. println("Hello " + client. Name + "!"); writer. flush(); String line. Read = null; while ((line. Read = reader. read. Line()) != null) { writer. println("You wrote: " + line. Read); writer. flush(); }

Accepting Connections • Usually, the accept() method is executed within an infinite loop -

Accepting Connections • Usually, the accept() method is executed within an infinite loop - i. e. , while(true){. . . } • Whenever accept() returns, a new thread is launched to handle that interaction • Hence, the server can handle several requests concurrently

Timeout • You can set timeout values to these blocking methods: - read() of

Timeout • You can set timeout values to these blocking methods: - read() of Socket - accept() of Server. Socket • Use the method set. So. Timeout(milliseconds) • If timeout is reached before the method returns, java. net. Socket. Timeout. Exception is thrown

Java Sockets and HTTP

Java Sockets and HTTP

HTTP Message Structure • A HTTP message has the following structure: Request/Status-Line rn Header

HTTP Message Structure • A HTTP message has the following structure: Request/Status-Line rn Header 1: value 1 rn Header 2: value 2 rn. . . Header. N: value. N rn Message-Body

Reading HTTP Messages • Several ways to interpret the bytes of the body -

Reading 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

An Example

An Example

Parsing the Headers • So how are the headers themselves are represented? • Answer:

Parsing the Headers • So how are the headers themselves are represented? • Answer: The headers of a HTTP message must be in US -ASCII format (1 byte per character) • However, in order to parse the headers, it may be impossible to use read. Line() of Buffered. Reader, since the body should be later read as binary bytes • Hence, we convert bytes to characters

Example: Extracting the Headers Socket socket = new Socket(argv[0], 80); Input. Stream istream =

Example: Extracting the Headers Socket socket = new Socket(argv[0], 80); Input. Stream istream = socket. get. Input. Stream(); Output. Stream ostream = socket. get. Output. Stream(); String request = "GET / HTTP/1. 1rn" + "Host: " + argv[0] + "rn" + "Connection: closern"; ostream. write(request. get. Bytes()); String. Buffer headers = new String. Buffer(); int byte. Read = 0; while ( !end. Of. Headers(headers) && (byte. Read = istream. read()) >= 0) { headers. append((char) byte. Read); } System. out. print(headers); socket. close();

Example: Extracting the Headers (cont) public static boolean end. Of. Headers(String. Buffer headers) {

Example: Extracting the Headers (cont) public static boolean end. Of. Headers(String. Buffer headers) { int last. Index = headers. length() - 1; if (last. Index < 3 || headers. char. At(last. Index) != 'n') return false; return ( headers. substring(last. Index - 3, last. Index + 1). equals("rn")); }

Persistent Connections • According to HTTP/1. 1, a server does not have to close

Persistent 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) • Question: how can the client know when one response ends and a new one begins? • To avoid persistency, require explicitly by the header: Connection: close

URL and URLConnection High-Level Networking

URL and URLConnection High-Level Networking

Working with URLs • URL (Uniform Resource Locator): a reference (an address) to a

Working with URLs • URL (Uniform Resource Locator): a reference (an address) to a resource on the Internet http: //www. cs. huji. ac. il: 80/~dbi/main. html#notes Protocol Host Name Port Number File Name Reference

The Class URL • The class URL is used for parsing URLs • Constructing

The Class URL • The class URL is used for parsing URLs • Constructing URLs: - URL w 3 c 1 = new URL("http: //www. w 3. org/TR/"); - URL w 3 c 2 = new URL("http", "www. w 3. org", 80, "TR/"); - URL w 3 c 3 = new URL(w 3 c 2, "xhtml 1/"); • If the string is not an absolute URL, then it is considered relative to the URL

Parsing URLs • The following methods of URL can be used for parsing URLs

Parsing URLs • The following methods of URL can be used for parsing URLs get. Protocol(), get. Host(), get. Port(), get. Path(), get. File(), get. Query(), get. Ref(), get. Query()

The class URLConnection • To establish the actual resource, we can use the object

The class URLConnection • To establish the actual resource, we can use the object URLConnection obtained by url. open. Connection() • If the protocol of the URL is HTTP, the returned object is of class Http. URLConnection • This class encapsulates all socket management and HTTP directions required to obtain the resource

public class Content. Extractor { public static void main(String[] argv) throws Exception { URL

public class Content. Extractor { public static void main(String[] argv) throws Exception { URL url = new URL(argv[0]); System. out. println("Host: " + url. get. Host()); System. out. println("Protocol: " + url. get. Protocol()); System. out. println("----"); URLConnection con = url. open. Connection(); Input. Stream stream = con. get. Input. Stream(); byte[] data = new byte[4096]; int bytes. Read = 0; while((bytes. Read=stream. read(data))>=0) { System. out. write(data, 0, bytes. Read); }}}

About URLConnection • The life cycle of a URLConnection object has two parts: -

About 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 get. Date()

About Http. URLConnection • The Http. URLConnection class encapsulates all HTTP transaction over sockets,

About Http. URLConnection • The Http. URLConnection class encapsulates all HTTP transaction over sockets, e. g. , - Content decoding - Redirection - Proxy indirection • However, you can control requests by its methods - set. Request. Method, set. Follow. Redirects, set. Request. Property, . . .

Sending POST Requests • In order to send POST requests with Http. URLConnection, you

Sending POST Requests • In order to send POST requests with Http. URLConnection, you have to do the following: - Enable connection output: con. set. Do. Output(true); - Get the output stream: con. get. Output. Stream() • This changes the method from GET to POST - Write the message body into the stream - close the stream (important!)

URL url = new URL("http: //find. walla. co. il/"); URLConnection connection = url. open.

URL url = new URL("http: //find. walla. co. il/"); URLConnection connection = url. open. Connection(); connection. set. Do. Output(true); String query = "q=" + URLEncoder. encode(args[0], "UTF-8"); Output. Stream out = connection. get. Output. Stream(); out. write(query. get. Bytes()); out. close(); Input. Stream in = connection. get. Input. Stream(); int bytes. Read = -1; byte[] response = new byte[4096]; while ((bytes. Read = in. read(response)) >= 0) System. out. write(response, 0, bytes. Read); in. close();

Sent Headers java Search. Walla "Java networking" POST / HTTP/1. 1 User-Agent: Java/1. 5.

Sent Headers java Search. Walla "Java networking" POST / HTTP/1. 1 User-Agent: Java/1. 5. 0_01 Host: find. walla. co. il Accept: text/html, image/gif, image/jpeg, *; q=. 2, */*; q=. 2 Connection: keep-alive Content-type: application/x-www-form-urlencoded Content-Length: 18 q=Java+networking

URLEncoder • Contains a utility method encode for converting a string into an encoded

URLEncoder • Contains a utility method encode for converting a string into an encoded format (used in URLs) • To convert a string, each character 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

Defining Default Proxy • For reading a URL using a proxy, we run java

Defining Default Proxy • For reading a URL using a proxy, we run java with the environment variables for http. proxy. Host and http. proxy. Port set properly: java -Dproxy. Host=wwwproxy. huji. ac. il –Dproxy. Port=8080. . . • Another option is to set the environment variables in the program itself System. get. Properties(). put( "proxy. Host", "wwwproxy. huji. ac. il" ); System. get. Properties(). put( "proxy. Port", "8080" );

Not Covered: UDP Connections • The URL, URLConnection, Socket and Server. Socket classes all

Not Covered: UDP Connections • The URL, URLConnection, Socket and Server. Socket classes all use the TCP protocol to communicate over the network • Java programs can also use UDP protocol for communication • For that, use Datagram. Packet, Datagram. Socket, and Multicast. Socket classes