Clients and Servers 08 Mar21 URL review n

  • Slides: 13
Download presentation
Clients and Servers 08 -Mar-21

Clients and Servers 08 -Mar-21

URL review n n A URL has the syntax: n protocol: //hostname: port/path#anchor import

URL review n n A URL has the syntax: n protocol: //hostname: port/path#anchor import java. net. *; n n URL url = new URL(String); n n Constructs a URL object from a text string Malformed. URLException n n This is the package that defines sockets, URLs, etc. This exception is thrown if the given String cannot be parsed by new. URL(String) We have used URLs to display a page in an applet: n applet. Context. show. Url(URL) 2

HTTP review n n HTTP is a protocol--a formal description of a language that

HTTP review n n HTTP is a protocol--a formal description of a language that computers use to communicate An HTTP message consists of three parts: – The request or the response line – A request line typically contains either GET or PUT – A response line contains the status code, such as 404 Not Found – A header section – Contains name-value pairs, such as Content-type: text/html – Ends with a blank line – The body of the message – The body is optional 3

Using a URL n URLConnection c = url. open. Connection(); n n c. get.

Using a URL n URLConnection c = url. open. Connection(); n n c. get. Header. Field(name) n n n Returns the value of the named header field (as a String) Frequently used fields have shorthand methods, for example, c. get. Last. Modified() = c. get. Header. Field("last-modified") get. Header. Field(int) n n n The URLConnection is the basic way to access the resource information Returns the value of the int-th header field (as a String) The 0 -th header field is the status line c. get. Input. Stream() n n Returns an Input. Stream containing the “content” of the resource url. open. Stream() is shorthand for url. open. Connection(). get. Input. Stream() 4

Sockets n n A socket is a low-level software device for connecting two programs

Sockets n n A socket is a low-level software device for connecting two programs (possibly on different computers) together new Socket(String host, int port) n n n Creates a client socket and makes the connection Methods include get. Input. Stream(), get. Output. Stream(), and close() new Server. Socket(int port) n n Creates a server socket that listens on the specified port accept() returns a Socket that can be used for I/O n accept() is a blocking method, so multithreading is highly desirable 5

How to write a server n Server. Socket server = new Server. Socket(port) n

How to write a server n Server. Socket server = new Server. Socket(port) n n Socket client = server. accept(); n n accept() blocks while it waits for a connection Input. Stream in. Stream = client. get. Input. Stream(); n n The port should be a number above 1024 Input. Stream. Reader reader = new Input. Stream. Reader(in. Stream); Buffered. Reader input = new Buffered. Reader(reader); char ch = input. read(), String s = input. read. Line() Output. Stream out. Stream = client. get. Output. Stream(); n Print. Writer output = new Print. Writer(out. Stream, true); n n n true is so that you auto-flush, that is, don’t fill the buffer output. print(X), output. println() input. close(), output. close(), server. close(), client. close() 6

How to write a client n Socket server = new Socket(ip_address, port) n The

How to write a client n Socket server = new Socket(ip_address, port) n The ip_address can be the String "localhost" n n Input. Stream in. Stream = server. get. Input. Stream(); n n As on the previous slide Output. Stream out. Stream = server. get. Output. Stream(); n n This method makes the actual connection As on the previous slide input. close(), output. close(), server. close() n As on the previous slide 7

How to write an HTTP server n An HTTP server is just a server

How to write an HTTP server n An HTTP server is just a server that follows the HTTP protocol (request/status line, header, blank line, body) n n There are two versions of HTTP: 1. 0 and 1. 1 n n n Since HTTP is a text-based protocol, compliance is easy HTTP 1. 0 is simpler and should be used if the special features of 1. 1 are not required The most important change in HTTP 1. 1 is that it can accommodate proxy servers The client and server must agree which version of HTTP is being used n Most HTTP servers can use both 8

Proxy servers n Proxies are important because they allow more than one server to

Proxy servers n Proxies are important because they allow more than one server to use the same IP address n n There aren’t enough IP addresses to go around If you have a lot of clients, you need a lot of servers--but the user should not have to try multiple IP addresses client server has IP address client Without a proxy client proxy has IP address server With a proxy client 9

Multithreading n server. accept() is a blocking call--Java stops and waits for a response

Multithreading n server. accept() is a blocking call--Java stops and waits for a response before it continues n n n This is only acceptable if the server never has more than one client A server needs to have a separate thread for each client There are two ways to create a Thread: n Write a class that extends Thread n n n Override the public void run() method Create an instance of your class and call its (inherited) start() method Write a class that implements Runnable n n Implement the public void run() method Create an instance of your class Create a Thread object with this instance as a parameter to the constructor Call the Thread object’s start() method 10

Synchronization n While an object is being modified by one thread, no other thread

Synchronization n While an object is being modified by one thread, no other thread should try to access it n n You can synchronize an object: n n synchronized (obj) { code that uses/modifies obj } synchronized is a statement type, like if or while No other code that is synchronized on this object can use or modify the object at the same time You can synchronize a method: n n This leads to unpredictable (and difficult to debug) results synchronized void add. One(arg 1, arg 2, . . . ) { code } synchronized is a method modifier, like public or abstract Only one synchronized method in a class can be used at a time (but this doesn’t restrict other, non-synchronized methods) Synchronization can really hurt efficiency (and response time) n It can be very difficult to make a program both safe and efficient 11

A synchronization analogy n Imagine that you have a building with two entrances n

A synchronization analogy n Imagine that you have a building with two entrances n n n Synchronization is like this n n n One entrance is always kept locked, and has a single key The other entrance is never locked Code that always uses the locked (synchronized) entrance has to wait for other code to exit and hand over the key Any code that uses the unlocked entrance can go into the building at any time, regardless of what other code may be there Thus, synchronizing code only protects you from other synchronized code! 12

The End 13

The End 13