Computer networks -- 2008-2009 -- info.uvt.ro/Course 3

Quick links: front; agenda; courses 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13; examination.

Important! Please note that the current work serves mainly as general guidance and discussion topics, and is by no means the reference material for the course. For further information please consult the dedicated section.

Prerequisites

edit

Streams

edit
  • types:
  • operations:
    • read / write;
    • flush;
    • skip;
  • message boundry;
  • buffering;
  • line-oriented protocols:
    • line ending;
    • line wrapping;

Java IO generic API

edit

TCP sockets

edit
  • like in the case of UDP sockets, applications using TCP sockets could be split into two categories:
    clients;
    servers;
  • unlike in the case of UDP sockets, in the case of TCP sockets we do need two types of sockets (related to each category of application):
    server sockets
    used on the server side;
    client sockets
    used on the client side;
  • and just as in the case of UDP sockets, we do need (the same) addressing information;

TCP client sockets

edit

Life-cycle

edit
  • operations:
    creation
    a client socket object is created;
    binding
    the socket's local address is established;
    connection
    the socket's remote address is established and a connection is made;
    data exchange;
    shutting down the channel
    • either one way or both ways:
      • if we shutdown the input -- incoming -- stream we notify the other peer that we are not accepting any other data;
      • if we shutdown the output -- outgoing -- stream we notify the other peer that we shall not send any more data;
    • each stream can be shutdown only once;
    • receiving the remaining data after shutdown;
    closing
    releasing all committed resources;

Data exchange

edit
  • resembling the way we do data exchange with UDP sockets;
  • the main difference is the absence of datagrams, but the buffers remain;
  • we just use the buffers to read or write data just like any other file stream;

Snippet examples

edit
  • creating the socket:
Socket socket = new Socket ();
  • binding the socket on any local available IP address and on a random port:
InetAddress localIpAddress = InetAddress.getByName ("0.0.0.0");
int localIpPort = 0;
SocketAddress localSocketAddress = new InetSocketAddress (localIpAddress, localIpPort);
socket.bind (localSocketAddress);
  • connecting to the remote socket address:
InetAddress remoteIpAddress = InetAddress.getByName ("www.info.uvt.ro");
int remoteIpPort = 80;
SocketAddress remoteSocketAddress = new InetSocketAddress (remoteIpAddress, remoteIpPort);
socket.connect (remoteSocketAddress);
  • receiving and / or sending data through input and output streams:
InputStream input = socket.getInputStream ();
OutputStream output = socket.getOutputStream ();
... buffer creation ...
input.read (buffer, offset, size); 
output.write (buffer, offset, size);
output.flush ();
  • shutting-down the input and output streams:
socket.shutdownInput ();
socket.shutdownOutput ();
  • closing the socket:
socket.close ();

Complete examples

edit

Java API

edit

TCP server sockets

edit

Life-cycle

edit
  • operations:
    creation
    a server socket is created;
    binding
    the socket's local address is established;
    listening
    the socket is put into a state that accepts incoming connections;
    accepting
    • the application waits for incoming connections; each connection is in the form of a (client) socket; for each connection the application either:
      • handles it, closes it, and continues to accept other connections;
      • creates a new process or thread which will handle the connection, and the current process or thread continues to accept other connections;
    closing
    the socket stops accepting incoming connections, and all committed resources are released; (closing the server socket, does not close any of the accepted incoming sockets;)

Snippet examples

edit
  • creating the server socket:
ServerSocket socket = new ServerSocket ();
  • binding to the local socket address -- this is the one the clients should be connecting to:
InetAddress localIpAddress = InetAddress.getByName ("0.0.0.0");
int localIpPort = 80;
SocketAddress localSocketAddress = new InetSocketAddress (localIpAddress, localIpPort);
socket.bind (localSocketAddress);
  • for each connection accepting a client socket, and:
Socket client = socket.accept ();
  • the following operations are identical with the ones presented for the TCP client sockets;
    • receiving and / or sending data;
    • shutting-down the inbound and outbound streams;
    • closing the client socket;
  • closing the server socket;
socket.close ();

Complete examples

edit

Java API

edit
  • java.net:
    • ServerSocket:
      • bind(SocketAddress) -- this establishes the socket's local address -- the one that the client must be aware of; this also puts the socket in a listening state;
      • accept() -- waits for an incoming connection and returns a (client) socket representing that connection;
      • close() -- stops the acceptance of new connections and releases all the socket resources;
      • getLocalSocketAddress() -- used to retrieve the local socket address;

Resource addressing

edit
  • resource addressing:
    URI (Uniform Resource Identifier)
    • a string that identifies a resource on the Internet;
    • there are two kinds: URN and URL;
    • syntax: <scheme identifier> + ":" + <scheme specific string>;
    • design criteria (quoted from RFC 1630):
      extensible
      new naming schemes may be added later;
      complete
      it is possible to encode any naming scheme;
      printable
      it is possible to express any URI using 7-bit ASCII characters so that URIs may, if necessary, be passed using pen and ink;
    • design issues:
      memorizable
      they should be remembered easily;
      persistent
      once a resource is referenced by an identifier, it should always be referenced;
    URN (Uniform Resource Name)
    URL (Uniform Resource Locator)
    • it is used to find a resource;
    • types and syntaxes:
      • hierarchical: absolute or relative;
        • <scheme>:<hierarchy>[?<query>][#<fragment>];
        • <scheme>:<authority><path>[?<query>][#<fragment>];
      • opaque;
        • <scheme>:<scheme specific>[#<fragment>];
    • examples: http://www.google.com/search?q=something or mailto:me@example.com;
http://www.google.com:80/search?q=something#page_2
\__|   \_______________|\_____| \_________| \____|
 \      \                \       \           \
  scheme authority        path    query       fragment

Java API

edit

Miscellaneous

edit

I/O considerations

edit
  • efficiency:
    • reducing the number of system calls;
    • I/O buffering;
    • I/O poll-ing;
  • I/O operation classification:
    • synchronous:
    • blocking;
      • non-blocking;
    • asynchronous;
  • client handling:

Message queues

edit

The current page is (in parts) a recompilation of the following pages (from previous year):