0% found this document useful (0 votes)
36 views12 pages

17 Client Server

The document discusses clients and servers, URLs, HTTP, sockets, and how to write client and server programs in Java. Some key points: - A URL has a protocol, hostname, port, and path. The URL class represents URLs and can open connections to resources. - HTTP is a request-response protocol with a request line, headers, and optional body. HTTP 1.1 supports proxy servers. - Sockets represent connections between programs. Servers use ServerSocket to listen on a port and accept client Socket connections. - To write a server, use ServerSocket to accept clients, get input/output streams to communicate, and handle each client in a separate thread. To write

Uploaded by

Sameer Gulia
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views12 pages

17 Client Server

The document discusses clients and servers, URLs, HTTP, sockets, and how to write client and server programs in Java. Some key points: - A URL has a protocol, hostname, port, and path. The URL class represents URLs and can open connections to resources. - HTTP is a request-response protocol with a request line, headers, and optional body. HTTP 1.1 supports proxy servers. - Sockets represent connections between programs. Servers use ServerSocket to listen on a port and accept client Socket connections. - To write a server, use ServerSocket to accept clients, get input/output streams to communicate, and handle each client in a separate thread. To write

Uploaded by

Sameer Gulia
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

Clients and Servers

5-May-12

URL review

A URL has the syntax: protocol://hostname:port/path#anchor import java.net.*;

This is the package that defines sockets, URLs, etc. Constructs a URL object from a text string This exception is thrown if the given String cannot be parsed by newURL(String)

URL url = new URL(String);

MalformedURLException

We have used URLs to display a page in an applet: appletContext.showUrl(URL)


2

HTTP review

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

URLConnection c = url.openConnection();

The URLConnection is the basic way to access the resource information Returns the value of the named header field (as a String) Frequently used fields have shorthand methods, for example, c.getLastModified() = c.getHeaderField("last-modified") Returns the value of the int-th header field (as a String) The 0-th header field is the status line Returns an InputStream containing the content of the resource url.openStream() is shorthand for url.openConnection().getInputStream()
4

c.getHeaderField(name)

getHeaderField(int)

c.getInputStream()

Socket review

A socket is a low-level software device for connecting two programs (possibly on different computers) together new Socket(String host, int port)

Creates a client socket and makes the connection Methods include getInputStream(), getOutputStream(), and close()

new ServerSocket(int port)

Creates a server socket that listens on the specified port accept() returns a Socket that can be used for I/O

accept() is a blocking method, so multithreading is highly desirable


5

How to write a server

ServerSocket server = new ServerSocket(port)

The port should be a number above 1024 accept() blocks while it waits for a connection InputStreamReader reader = new InputStreamReader(inStream); BufferedReader input = new BufferedReader(reader); char ch = input.read(), String s = input.readLine()

Socket client = server.accept();

InputStream inStream = client.getInputStream();


OutputStream outStream = client.getOutputStream();

PrintWriter output = new PrintWriter(outStream, true);

true is so that you auto-flush, that is, dont fill the buffer

output .print(X), output .println(X), output .println()


6

input.close(), output.close(), server.close(), client.close()

How to write a client

Socket server = new Socket(ip_address, port) The ip_address can be the String "localhost"

This method makes the actual connection As on the previous slide As on the previous slide As on the previous slide

InputStream inStream = server.getInputStream();

OutputStream outStream = server.getOutputStream();

input.close(), output.close(), server.close()

How to write an HTTP server

An HTTP server is just a server that follows the HTTP protocol (request/status line, header, blank line, body)

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 accomodate proxy servers

There are two versions of HTTP: 1.0 and 1.1

The client and server must agree which version of HTTP is being used

Most HTTP servers can use both


8

Proxy servers

Proxies are important because they allow more than one server to use the same IP address

There arent 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
server
has IP address

client client client

client client proxy


has IP address

server

Without a proxy

client
client client

server

With a proxy

Multithreading

server.accept() is a blocking call--Java stops and waits for a response before it continues

This is only acceptable if the server never has more than one client A server needs to have a separate thread for each client Write a class that extends Thread

There are two ways to create a Thread:

Override the public void run() method Create an instance of your class and call its (inherited) start() method 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 objects start() method
10

Write a class that implements Runnable

Synchronization

While an object is being modified by one thread, no other thread should try to access it

This leads to unpredictable (and difficult to debug) results


synchronized (obj) { code that uses/modifies obj } synchronized is a statement type, like if or while No other code can use or modify this object at the same time synchronized void addOne(arg1, arg2, ...) { 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 doesnt restrict other, non-synchronized methods) It can be very difficult to make a program both safe and efficient
11

You can synchronize an object:


You can synchronize a method:


Synchronization can really hurt efficiency (and response time)

The End

12

You might also like