java - sockets

18
SOCKETS PROGRAMMAZIONE CONCORRENTE E DISTR. Università degli Studi di Padova Dipartimento di Matematica Corso di Laurea in Informatica, A.A. 2015 – 2016 [email protected]

Upload: riccardo-cardin

Post on 09-Feb-2017

488 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Java - Sockets

SOCKETSPROGRAMMAZIONE CONCORRENTE E DISTR.Università degli Studi di Padova

Dipartimento di Matematica

Corso di Laurea in Informatica, A.A. 2015 – [email protected]

Page 2: Java - Sockets

2Programmazione concorrente e distribuita

SUMMARY Introduction Sockets Client program Server program Protocol

Riccardo Cardin

Page 3: Java - Sockets

3Programmazione concorrente e distribuita

INTRODUCTION Client-server applications

The server provides some serviceThe client uses the server provided by the serverThe communication must be reliable

TCP provides a reliable, point-to-point communication chanel over the InternetEach program binds a socket to its end of connectionThe communication is realized reading and writing

from and to the socket bound to the connection

Riccardo Cardin

Page 4: Java - Sockets

4Programmazione concorrente e distribuita

INTRODUCTION A server runs on a specific host and has a socket

bound to a specific portThe server waits, listening to the socket

The client knows the hostname and port number on which the server is listeningTries to randezvous with the server and binds a local

port number to use during connection

Riccardo Cardindistribuita

Page 5: Java - Sockets

5Programmazione concorrente e distribuita

INTRODUCTION

Riccardo Cardin

Page 6: Java - Sockets

6Programmazione 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

endpointsThe 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

Riccardo 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.

Page 7: Java - Sockets

7Programmazione 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.

Riccardo 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.)

Page 8: Java - Sockets

8Programmazione concorrente e distribuita

CLIENT PROGRAM First of all, let’s open the socket

Riccardo Cardin

// The client has the hostname and port of the server as inputsString 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)))

Page 9: Java - Sockets

9Programmazione concorrente e distribuita

CLIENT PROGRAM The java.net.Socket class implements a

client socketA socket is binded to an hostname and a port during

building processSockets 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 orderThe server socket must be ready to accept incoming

connection Otherwise, the client socket will thrown an exception

Riccardo Cardin

Page 10: Java - Sockets

10Programmazione 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

Riccardo Cardin

String userInput;// Reading user input until Ctrl+C is readwhile ((userInput = stdIn.readLine()) != null) { // Writing information to socket out.println(userInput); // Reading information to socket System.out.println("echo: " + in.readLine());}

Page 11: Java - Sockets

11Programmazione concorrente e distribuita

CLIENT PROGRAM

Riccardo Cardin

Page 12: Java - Sockets

12Programmazione concorrente e distribuita

SERVER PROGRAM To the other end of endpoint a server is listening

to some incoming messages

Riccardo Cardin

// The port number on which the server will listeningint 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()));)

Page 13: Java - Sockets

13Programmazione concorrente e distribuita

SERVER PROGRAM

Riccardo Cardin

Page 14: Java - Sockets

14Programmazione concorrente e distribuita

SERVER PROGRAM Using java.net.ServerSocket a server can

accept a connection from a clientThe 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 ThreadThe communication is manage using streams

Riccardo Cardin

while (true) { // accept a connection // create a thread to deal with the client}

Page 15: Java - Sockets

15Programmazione concorrente e distribuita

SERVER PROGRAM

Riccardo Cardin

Page 16: Java - Sockets

16Programmazione 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

Riccardo Cardin

Page 17: Java - Sockets

17Programmazione concorrente e distribuita

EXAMPLES

Riccardo Cardin

https://github.com/rcardin/pcd-snippets

Page 18: Java - Sockets

18Programmazione 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

Riccardo Cardin