SOCKETS
PROGRAMMAZIONE CONCORRENTE E DISTR.
Università degli Studi di Padova
Dipartimento di Matematica
Corso di Laurea in Informatica, A.A. 2015 – 2016
rcardin@math.unipd.it
Programmazione concorrente e distribuita
SUMMARY
 Introduction
 Sockets
 Client program
 Server program
 Protocol
2Riccardo Cardin
Programmazione concorrente e distribuita
INTRODUCTION
 Client-server applications
 The server provides some service
 The client uses the server provided by the server
 The communication must be reliable
 TCP provides a reliable, point-to-point
communication chanel over the Internet
 Each program binds a socket to its end of connection
 The communication is realized reading and writing
from and to the socket bound to the connection
3Riccardo Cardin
Programmazione concorrente e distribuita
INTRODUCTION
 A server runs on a specific host and has a socket
bound to a specific port
 The server waits, listening to the socket
 The client knows the hostname and port
number on which the server is listening
 Tries to randezvous with the server and binds a local
port number to use during connection
4Riccardo Cardindistribuita
Programmazione concorrente e distribuita
INTRODUCTION
5Riccardo Cardin
Programmazione concorrente e distribuita
SOCKETS
 An endpoint is a combination of an IP address and a
port number
 Every TCP connection is uniquely identified by its two
endpoints
 The class java.net.Socket implements the client
side of a two-way connection
 Sits on top of a platform-dependent implementation
 The class java.net.ServerSocket implements the
server side, that listen for connections to clients
6Riccardo Cardin
A socket is one endpoint of a two-way communication link between
two programs running on the network. A socket is bound to a port
number so that the TCP layer can identify the application that data is
destined to be sent to.
Programmazione concorrente e distribuita
EXAMPLE: ECHO PROGRAM
 Let’s implement an example program
 The EchoClient writes to and reads from the socket
 Open a socket.
 Open an input stream and output stream to the socket.
 Read from and write to the stream according to the server's
protocol.
 Close the streams.
 Close the socket.
7Riccardo Cardin
The example program implements a client, EchoClient, that
connects to an echo server. The echo server receives data from its
client and echoes it back. The example EchoServer implements an
echo server. (Alternatively, the client can connect to any host that
supports the Echo Protocol.)
Programmazione concorrente e distribuita
CLIENT PROGRAM
 First of all, let’s open the socket
8Riccardo Cardin
// The client has the hostname and port of the server as inputs
String hostName = args[0];
int portNumber = Integer.parseInt(args[1]);
try (
// Open a socket connection to a host and a port number
Socket echoSocket = new Socket(hostName, portNumber);
// Build structures that write to the socket
PrintWriter out =
new PrintWriter(echoSocket.getOutputStream(), true);
// Build structures that read from the socket
BufferedReader in =
new BufferedReader(
new InputStreamReader(echoSocket.getInputStream()));
// Read user input from console
BufferedReader stdIn =
new BufferedReader(
new InputStreamReader(System.in))
)
Programmazione concorrente e distribuita
CLIENT PROGRAM
 The java.net.Socket class implements a
client socket
 A socket is binded to an hostname and a port during
building process
 Sockets implements AutoCloseable
 For reading from and writing to a socket we need
input and output streams
 The try-with-resources statement closes the streams and the
socket in the right order
 The server socket must be ready to accept incoming
connection
 Otherwise, the client socket will thrown an exception
9Riccardo Cardin
Programmazione concorrente e distribuita
CLIENT PROGRAM
 The protocol have to be implemented manually
 How the socket information are interpreted is
dependent from which stream is used to read from it
 A Ctrl+C is interpreted as an end-of-input
 The communication protocol is totally custom
 For example, talking to an HTTP server will be more
complicated
10Riccardo Cardin
String userInput;
// Reading user input until Ctrl+C is read
while ((userInput = stdIn.readLine()) != null) {
// Writing information to socket
out.println(userInput);
// Reading information to socket
System.out.println("echo: " + in.readLine());
}
Programmazione concorrente e distribuita
CLIENT PROGRAM
11Riccardo Cardin
Programmazione concorrente e distribuita
SERVER PROGRAM
 To the other end of endpoint a server is listening
to some incoming messages
12Riccardo Cardin
// The port number on which the server will listening
int portNumber = Integer.parseInt(args[0]);
try (
// A ServerSocket waits a client’s message on a specific port
ServerSocket serverSocket = new ServerSocket(portNumber);
// Once a message has arrived, a socket is created to manage
// the connection with the client
Socket clientSocket = serverSocket.accept();
// Structure to write to the socket
PrintWriter out =
new PrintWriter(clientSocket.getOutputStream(), true);
// Structure to read from the socket
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
)
Programmazione concorrente e distribuita
SERVER PROGRAM
13Riccardo Cardin
Programmazione concorrente e distribuita
SERVER PROGRAM
 Using java.net.ServerSocket a server can
accept a connection from a client
 The accept method waits until a client request a
connection to the host and port of the server
 A Socket is created on the same port, to managed the
connection with the new client
 It is possible to have a «multiple client» server
 For each new connection, create a dedicated Thread
 The communication is manage using streams
14Riccardo Cardin
while (true) {
// accept a connection
// create a thread to deal with the client
}
Programmazione concorrente e distribuita
SERVER PROGRAM
15Riccardo Cardin
Programmazione concorrente e distribuita
PROTOCOL
 Who speaks first?
 The problem with socket communication is that it is a
low level type of communication
 The protocol is custom for each type of
implementation
 Usually a dedicated class is used to implement the
protocol
 Given a received message, it returns the next action to do
 Server port is part of the protocol
 The server MUST be already listening for incoming
connection when clients try to communicate with it
 Only the accept method of ServerSocket is blocking
16Riccardo Cardin
Programmazione concorrente e distribuita
EXAMPLES
17Riccardo Cardin
https://github.com/rcardin/pcd-snippets
Programmazione concorrente e distribuita
REFERENCES
 Lesson: All About Socket
https://docs.oracle.com/javase/tutorial/networking/sockets/
 Echo Protocol http://tools.ietf.org/html/rfc862
 Does the port change when a TCP connection is accepted by a
server? http://stackoverflow.com/questions/2997754/does-the-
port-change-when-a-tcp-connection-is-accepted-by-a-server
18Riccardo Cardin

Java - Sockets

  • 1.
    SOCKETS PROGRAMMAZIONE CONCORRENTE EDISTR. Università degli Studi di Padova Dipartimento di Matematica Corso di Laurea in Informatica, A.A. 2015 – 2016 rcardin@math.unipd.it
  • 2.
    Programmazione concorrente edistribuita SUMMARY  Introduction  Sockets  Client program  Server program  Protocol 2Riccardo Cardin
  • 3.
    Programmazione concorrente edistribuita INTRODUCTION  Client-server applications  The server provides some service  The client uses the server provided by the server  The communication must be reliable  TCP provides a reliable, point-to-point communication chanel over the Internet  Each program binds a socket to its end of connection  The communication is realized reading and writing from and to the socket bound to the connection 3Riccardo Cardin
  • 4.
    Programmazione concorrente edistribuita INTRODUCTION  A server runs on a specific host and has a socket bound to a specific port  The server waits, listening to the socket  The client knows the hostname and port number on which the server is listening  Tries to randezvous with the server and binds a local port number to use during connection 4Riccardo Cardindistribuita
  • 5.
    Programmazione concorrente edistribuita INTRODUCTION 5Riccardo Cardin
  • 6.
    Programmazione concorrente edistribuita SOCKETS  An endpoint is a combination of an IP address and a port number  Every TCP connection is uniquely identified by its two endpoints  The class java.net.Socket implements the client side of a two-way connection  Sits on top of a platform-dependent implementation  The class java.net.ServerSocket implements the server side, that listen for connections to clients 6Riccardo Cardin A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to.
  • 7.
    Programmazione concorrente edistribuita EXAMPLE: ECHO PROGRAM  Let’s implement an example program  The EchoClient writes to and reads from the socket  Open a socket.  Open an input stream and output stream to the socket.  Read from and write to the stream according to the server's protocol.  Close the streams.  Close the socket. 7Riccardo Cardin The example program implements a client, EchoClient, that connects to an echo server. The echo server receives data from its client and echoes it back. The example EchoServer implements an echo server. (Alternatively, the client can connect to any host that supports the Echo Protocol.)
  • 8.
    Programmazione concorrente edistribuita CLIENT PROGRAM  First of all, let’s open the socket 8Riccardo Cardin // The client has the hostname and port of the server as inputs String hostName = args[0]; int portNumber = Integer.parseInt(args[1]); try ( // Open a socket connection to a host and a port number Socket echoSocket = new Socket(hostName, portNumber); // Build structures that write to the socket PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true); // Build structures that read from the socket BufferedReader in = new BufferedReader( new InputStreamReader(echoSocket.getInputStream())); // Read user input from console BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)) )
  • 9.
    Programmazione concorrente edistribuita CLIENT PROGRAM  The java.net.Socket class implements a client socket  A socket is binded to an hostname and a port during building process  Sockets implements AutoCloseable  For reading from and writing to a socket we need input and output streams  The try-with-resources statement closes the streams and the socket in the right order  The server socket must be ready to accept incoming connection  Otherwise, the client socket will thrown an exception 9Riccardo Cardin
  • 10.
    Programmazione concorrente edistribuita CLIENT PROGRAM  The protocol have to be implemented manually  How the socket information are interpreted is dependent from which stream is used to read from it  A Ctrl+C is interpreted as an end-of-input  The communication protocol is totally custom  For example, talking to an HTTP server will be more complicated 10Riccardo Cardin String userInput; // Reading user input until Ctrl+C is read while ((userInput = stdIn.readLine()) != null) { // Writing information to socket out.println(userInput); // Reading information to socket System.out.println("echo: " + in.readLine()); }
  • 11.
    Programmazione concorrente edistribuita CLIENT PROGRAM 11Riccardo Cardin
  • 12.
    Programmazione concorrente edistribuita SERVER PROGRAM  To the other end of endpoint a server is listening to some incoming messages 12Riccardo Cardin // The port number on which the server will listening int portNumber = Integer.parseInt(args[0]); try ( // A ServerSocket waits a client’s message on a specific port ServerSocket serverSocket = new ServerSocket(portNumber); // Once a message has arrived, a socket is created to manage // the connection with the client Socket clientSocket = serverSocket.accept(); // Structure to write to the socket PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); // Structure to read from the socket BufferedReader in = new BufferedReader( new InputStreamReader(clientSocket.getInputStream())); )
  • 13.
    Programmazione concorrente edistribuita SERVER PROGRAM 13Riccardo Cardin
  • 14.
    Programmazione concorrente edistribuita SERVER PROGRAM  Using java.net.ServerSocket a server can accept a connection from a client  The accept method waits until a client request a connection to the host and port of the server  A Socket is created on the same port, to managed the connection with the new client  It is possible to have a «multiple client» server  For each new connection, create a dedicated Thread  The communication is manage using streams 14Riccardo Cardin while (true) { // accept a connection // create a thread to deal with the client }
  • 15.
    Programmazione concorrente edistribuita SERVER PROGRAM 15Riccardo Cardin
  • 16.
    Programmazione concorrente edistribuita PROTOCOL  Who speaks first?  The problem with socket communication is that it is a low level type of communication  The protocol is custom for each type of implementation  Usually a dedicated class is used to implement the protocol  Given a received message, it returns the next action to do  Server port is part of the protocol  The server MUST be already listening for incoming connection when clients try to communicate with it  Only the accept method of ServerSocket is blocking 16Riccardo Cardin
  • 17.
    Programmazione concorrente edistribuita EXAMPLES 17Riccardo Cardin https://github.com/rcardin/pcd-snippets
  • 18.
    Programmazione concorrente edistribuita REFERENCES  Lesson: All About Socket https://docs.oracle.com/javase/tutorial/networking/sockets/  Echo Protocol http://tools.ietf.org/html/rfc862  Does the port change when a TCP connection is accepted by a server? http://stackoverflow.com/questions/2997754/does-the- port-change-when-a-tcp-connection-is-accepted-by-a-server 18Riccardo Cardin