Docsity

Understanding URLs, HTTP, and Sockets for Client-Server Communication, Slides of Computer Science

Partial preview of the text

Download Understanding URLs, HTTP, and Sockets for Client-Server Communication and more Slides Computer Science in PDF only on Docsity!

Clients and Servers

URL review

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

Using a URL

  • URLConnection c = url .openConnection();
    • The URLConnection is the basic way to access the resource information
  • c .getHeaderField( name )
    • 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")
  • getHeaderField( int )
    • Returns the value of the int -th header field (as a String)
    • The 0-th header field is the status line
  • c .getInputStream()
    • Returns an InputStream containing the “content” of the resource
    • url .openStream() is shorthand for url .openConnection().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

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
  • InputStream inStream = server .getInputStream();
  • As on the previous slide
  • OutputStream outStream = server .getOutputStream();
  • As on the previous slide
  • input .close(), output .close(), server .close()
  • As on the previous slide

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

• There are two versions of HTTP: 1.0 and 1.

– 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

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
  • There are two ways to create a Thread:
  • Write a class that extends Thread
  • Override the public void run() method
  • Create an instance of your class and call its (inherited) start() method
  • Write a class that implements Runnable
  • 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

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
  • You can synchronize an object:
  • 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
  • You can synchronize a method:
  • 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 doesn’t restrict other, non- synchronized methods)
  • Synchronization can really hurt efficiency (and response time)
  • It can be very difficult to make a program both safe and efficient