SUMMARY Introduction Sockets Client program Server program SOCKETS Protocol PROGRAMMAZIONE CONCORRENTE E DISTR. Università degli Studi di Padova Dipartimento di Matematica Corso di Laurea in Informatica, A.A. 2015 – 2016 [email protected] Programmazione concorrente e distribuita Riccardo Cardin INTRODUCTION INTRODUCTION Client‐server applications A server runs on a specific host and has a socket bound to a specific port The server provides some service The client uses the server provided by the server The communication must be reliable The client knows the hostname and port number on which the server is listening communication chanel over the Internet The server waits, listening to the socket TCP provides a reliable, point‐to‐point 2 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 Tries to randezvous with the server and binds a local port number to use during connection Programmazione concorrente e distribuita Riccardo Cardin 3 Programmazione concorrente e distribuita distribuita Riccardo Cardin 4 INTRODUCTION SOCKETS 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 so that the TCP layer can identify the application that data is number destined to be sent to. An endpoint is a combination of an IP address and a port number The class java.net.Socket implements the client side of a two‐way connection Programmazione concorrente e distribuita Riccardo Cardin 5 Every TCP connection is uniquely identified by its two endpoints Sits on top of a platform‐dependent implementation The class java.net.ServerSocket implements the server side, that listen for connections to clients Programmazione concorrente e distribuita EXAMPLE: ECHO PROGRAM CLIENT PROGRAM Let’s implement an example program First of all, let’s open the socket 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.) 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)) ) 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. Riccardo Cardin 6 // The client has the hostname and port of the server as inputs String hostName = args[0]; int portNumber = Integer.parseInt(args[1]); The EchoClient writes to and reads from the socket Programmazione concorrente e distribuita Riccardo Cardin 7 Programmazione concorrente e distribuita Riccardo Cardin 8 CLIENT PROGRAM CLIENT PROGRAM The java.net.Socket class implements a The protocol have to be implemented manually 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 userInput; String // 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()); } 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 How the socket information are interpreted is dependent from which stream is used to read from it The communication protocol is totally custom Otherwise, the client socket will thrown an exception Programmazione concorrente e distribuita Riccardo Cardin 9 CLIENT PROGRAM A Ctrl+C is interpreted as an end‐of‐input For example, talking to an HTTP server will be more complicated Programmazione concorrente e distribuita Riccardo Cardin 10 SERVER PROGRAM To the other end of endpoint a server is listening to some incoming messages // 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 Riccardo Cardin 11 Programmazione concorrente e distribuita Riccardo Cardin 12 SERVER PROGRAM 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 while (true) { // accept a connection // create a thread to deal with the client } Programmazione concorrente e distribuita Riccardo Cardin 13 SERVER PROGRAM For each new connection, create a dedicated Thread The communication is manage using streams 14 PROTOCOL Who speaks first? 15 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 Riccardo Cardin 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 Programmazione concorrente e distribuita Riccardo Cardin Programmazione concorrente e distribuita Only the accept method of ServerSocket is blocking Programmazione concorrente e distribuita Riccardo Cardin 16 EXAMPLES 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 https://github.com/rcardin/pcd‐snippets Programmazione concorrente e distribuita Riccardo Cardin 17 Programmazione concorrente e distribuita Riccardo Cardin 18