Computer networks -- 2008-2009 -- info.uvt.ro/Course 3
Appearance
Quick links: front; agenda; courses 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13; examination.
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:
- byte streams;
- character streams:
- object streams:
- serialization / deserialization;
- object graph;
- object identity (vs value);
- singleton serialization;
- operations:
- read / write;
- flush;
- skip;
- message boundry;
- buffering;
- line-oriented protocols:
- line ending;
- line wrapping;
Java IO generic API
[edit]- java.io:
- InputStream -- is a binary (byte) stream that we can read from:
- OutputStream -- is a binary (byte) stream that we can write to:
- write(byte, int, int);
- flush() -- in case there is a buffer holding information, force the send;
- close();
- InputStreamReader -- adapts (transforms) a binary input stream into a character output stream;
- OutputStreamWriter -- adapts (transforms) a binary output stream into a character output stream;
- BufferedInputStream;
- BufferedOutputStream;
- BufferedReader:
- readLine() -- used to read an entire line as a string;
- BufferedWriter:
- ObjectInputStream:
- readObject() -- used to deserialize an object;
- ObjectOutputStream:
- writeObject(Object) -- used to serialize an object;
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;
- either one way or both ways:
- 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]- please consult Client example from Laboratory 2 from previous years;
Java API
[edit]- java.net:
- Socket:
- bind(SocketAddress) -- used to establish the socket's local address;
- connect(SocketAddress) -- used to establish the socket's remote address, and try to establish a connection;
- shutdownInput();
- shutdownOutput();
- close() -- used to terminate the connection;
- getInputStream() -- used to obtain the input -- incoming -- byte stream;
- getOutputStream() -- used to obtain the output -- outgoing -- byte stream;
- getLocalSocketAddress() -- used to obtain the local socket address;
- getRemoteSocketAddress() -- used to obtain the remote socket address -- who is on the other side;
- Socket:
- please see also the API presented in the sections:
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;
- the application waits for incoming connections; each connection is in the form of a (client) socket; for each connection the application either:
- 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]- please consult Server example from Laboratory 2 from previous years;
- also you could consult Server (from assignment) from the same Laboratory 2 as above;
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;
- ServerSocket:
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)
-
- it is used to identify (uniquely) a resource;
- syntax: "urn:" + <namespace identifier> + ":" + <namespace specific string>;
- examples: urn:isbn:0451450523 or urn:ietf:rfc:2648;
- 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>];
- hierarchical: absolute or relative;
- 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
- references
- RFC 1630 -- Universal Resource Identifiers in WWW;
- IANA assigned port numbers;
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:
- serial (one-by-one) handling;
- concurrent threads;
- concurrent processes;
- event driven;
RPC
[edit]- concepts:
- server;
- client;
- interface;
- proxy / stub;
- skeleton;
- references:
Message queues
[edit]- references:
The current page is (in parts) a recompilation of the following pages (from previous year):
- Computer networks -- 2007-2008 -- info.uvt.ro/Laboratory 1;
- Computer networks -- 2007-2008 -- info.uvt.ro/Laboratory 2;
- Web technologies -- Laboratory 2 -- 2007-2008 -- info.uvt.ro;
- conf. dr. Victoria Iordan, iordan@info.uvt.ro
- Ciprian Dorin Craciun, ccraciun@info.uvt.ro
- Marian Neagul, marian@info.uvt.ro