DBI Representation and Management of Data on the
DBI Representation and Management of Data on the Internet
Networking in Java 2
Client-Server Model Port 80 Client application 132. 65. 32. 29 Server application 64. 208. 34. 100 www. google. com Client application 132. 68. 7. 11
Clients • Client - initiates connection – retrieves data – displays data – responds to user input – requests more data • Examples: – Web Browser – Chat Program – PC accessing files
Servers • Server - responds to connection – receives request for data – looks it up – delivers it • Examples: – Web Server – Database Server – Domain Name Server
Networking Basics • Communication layers: Application (HTTP, FTP, Telnet) Transport (TCP, UDF) Network (IP) Link (device, driver, …) Our programs
TCP and UDP • TCP (Transmission Control Protocol) – connection-based protocol – provides a reliable flow of data between two computers • UDP (User Datagram Protocol) – a protocol that sends independent packets of data, called datagrams, from one computer to another – arrival of datagrams is not gaurantees – UDP is not connection-based like TCP
How to Choose TCP or UDP • Use TCP when reliability is critical: – HTTP – FTP – Telnet • Use UDP when reliability is not critical: – Ping – Clock – Audio transmission – Video transmission
Making a Connection • Q: How do we know where the host is located? • A: By its IP address • Q: There can be many applications that are in the same time connected to the same host, – for example, many browser and one search engine How do we send the right transmission to each client? • A: By the Port
Host and Port • Destination in the Internet is identified by host + port – a 32 bits IP-address – a 16 bits port • Q: Why don’t we specify the port in a Web browser? • A: Port 80 is considered the default • Ports 0 -1023 are restricted – Do not use them
Known Ports • Some known ports are – 20, 21: FTP – 23: telnet – 25: SMTP – 43: whois – 80: HTTP – 119: NNTP
Internet Addresses • Inet. Address – a final class that represents Internet protocol (IP) adresses • Getting the Inet. Adddress – get. Local. Host • Returns the local host – get. By. Name(String host) • For the given host name one of its IP addresses is returned – get. All. By. Name(String host) • For a given host name all its IP addresses are returned
Methods of Inet. Address • get. Host. Address – Returns the IP address of the host – The address is in the form “%d. %d. %d” • get. Host. Name – Returns the name of the host
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
Creating URLs • The class URL is defined in the package java. net • Basic constructor: URL w 3 c = new URL("http: //www. w 3. org/"); • Relative links: – Are created from base. URL + relative. URL amaya = new URL(w 3 c, “Amaya/Amaya. html”); URL jigsaw = new URL(w 3 c, “Jigsaw/#Getting”);
(. Creating URLs (cont • The following two are equivalent: URL dbi. Notes = new URL(“http: //www. cs. huji. ac. il: 80/” + ”~dbi/main. html#notes”); URL dbi. Notes = new URL(“http”, “www. cs. huji. ac. il”, 80, “~dbi/main. html#notes”); • Construction of URLs can throw Malformed. URLException
Why Do We Need the URL ? Class • The main usage of URL is for parsing URLs – getting the protocol – getting the host – getting the port – getting the file name – getting the reference • An example of parsing a URL
Running the Example URLInfo http: //www. cs. huji. ac. il Java URLInfo Java http: //www. cs. huji. ac. il: 80/~dbi/main. html#notes Protocol: http Host: www. cs. huji. ac. il File name: /~dbi/main. html Port: 80 -1 Reference: notes null
Reading From A URL url open. Stream Input. Stream. Reader Buffered. Reader read. Line Page Content
Reading From a URL • Example of reading from a given URL: Url. Reader. java • For reading a URL using a proxy, we do the following: Java –Dhttp. proxy. Host=wwwproxy. cs. huji. ac. il –Dhttp. proxy. Port=8080 Url. Reader url
… Reading Java Url. Reader url The content of the file
Connecting to A URL url open. Connection URLConnection get. Output. Stream get. Input. Stream println Print. Writer Input. Stream. Reader Page Buffered. Reader read. Line
Interacting with a CGI script 1. 2. 3. 4. Create a URL Open a connection to the URL Set output capability on the URLConnection Get an output stream from the connection 1. This output stream is connected to the standard input stream of the cgi-bin script on the server 5. Write to the output stream 6. Close the output stream
HTTP connections • You can create a connection to an HTTP server with an object Http. URLConnection • This object extends the URLConnection object – get. Request. Code – get. Response. Method – get. Responce. Message • Look in the Java API
URL Connection Example • The next example connects to a CGI script on www. walla. co. il - a search tool is given a word to search. Walla
Socket • A socket is one end-point of a two-way communication link between two programs running on the network • A socket is bound to a port number for the use of TCP (why? ) • Class Socket – implements the client side of the connection • Class Server. Socket – implements the server side of the connection
Sockets • The server has a socket that listen to a known port, e. g. , Web server and port 80 • The server waits (listening to the socket) till a client requests a connection • A client requests a connection on the known host and port of the server • Q: What should the server do so that it can serve other clients as well?
Client Requests for a Connection Port 2030 Port 80 Client application 132. 65. 32. 29 Server application 64. 208. 34. 100 www. google. com Client application 132. 68. 7. 11
Using a Socket host Socket port get. Input. Stream. Reader Buffered. Reader read. Line Page Content
Using a Socket // Constructors (partial list) public Socket(Inet. Address address, int port); public Socket(String host, int port); // Methods (partial list) public void close(); public Inet. Address get. Inet. Address(); public int get. Local. Port(); public Input. Stream get. Input. Stream(); public Output. Stream get. Output. Stream(); public int get. Port(); public String to. String();
(Using a Socket (client • A client: 1. Open a socket 2. Open an input stream and output stream to the socket 3. Read from and write to the stream according to the client’s protocol 4. Close the streams 5. Close the socket
(. Using a Socket (cont • A server: 1. Open a socket 2. Open an input stream and output stream to the socket 3. Read from and write to the stream according to the server's protocol 4. Close the streams 5. Close the socket
A Client Example • The following is a client that connects to a Time Server (port 13) and returns the curret time Time. Client
Running the Time. Client java Time. Client Tue Mar 13 12: 42: 09 2001
Server. Socket // Constructors (partial list) public Server. Socket(int port); public Server. Socket(int port, int count); // Methods (partial list) public Socket accept(); public void close(); public Inet. Address get. Inet. Address(); public int get. Local. Port(); public String to. String();
A Server Example • The following is a time server that returns the time: Time. Server • Note: When the Server. Socket constructor is given port number 0, a random free port is chosen
More on Server Socket • A Server. Socket waits for requests to come in over the network • It performs some operation based on that request, and then possibly returns a result to the requester • The actual work of the Server. Socket is performed by an instance of the Socket. Impl class • The abstract class Socket. Impl is a common superclass of all classes that actually implement sockets • It is used to create both client and server sockets
Server side import java. net. *; import java. io. *; // A server that says 'hello' class Hello. Server { public static void main(String[] args) { int port = Integer. parse. Int(args[0]); Server. Socket server = null; try { server = new Server. Socket(port); } catch (IOException ioe) { System. err. println("Couldn't run"+ "server on port "+port); return; }
Server side while(true) { try { Socket connection = server. accept(); Buffered. Reader reader = new Buffered. Reader( new Input. Stream. Reader( connection. get. Input. Stream())); Print. Writer writer = new Print. Writer( new Output. Stream. Writer( connection. get. Output. Stream())); String client. Name = reader. read. Line(); writer. println("Hello "+client. Name); writer. flush(); } catch (IOException ioe 1) {} }
Client side import java. net. *; import java. io. *; // A client of an Hello. Server class Hello. Client { public static void main(String[] args) { String hostname = args[0]; int port = Integer. parse. Int(args[1]); Socket connection = null; try { connection = new Socket(hostname, port); } catch (IOException ioe) { System. err. println("Connection failed"); return; }
Client side try { Buffered. Reader reader = new Buffered. Reader( new Input. Stream. Reader( connection. get. Input. Stream())); Print. Writer writer = new Print. Writer( new Output. Stream. Writer( connection. get. Output. Stream())); writer. println(args[2]); // client name String reply = reader. read. Line(); System. out. println("Server reply: "+reply); writer. flush(); } catch (IOException ioe 1) { } }
Java Net Classes Class Description Datagram. Packet This class represents a datagram packet. Datagram. Socket This class represents a socket for sending and receiving datagram packets. Inet. Address This class represents an Internet Protocol (IP) address. Multicast. Socket The multicast datagram socket class is useful for sending and receiving IP multicast packets. Server. Socket This class implements server sockets. Socket This class implements client sockets (also called just "sockets"). URL A pointer to a "resource" on the World Wide Web. URLConnection The superclass of all classes that represent a communications link between an application and a URL.
- Slides: 43