image/svg+xml $ $ ing$ ing$ ces$ ces$ Res Res ea ea Res->ea ou ou Res->ou r r ea->r ch ch ea->ch r->ces$ r->ch ch->$ ch->ing$ T T T->ea ou->r

Le protocole TCP

TCP (Transport Control Protocol)

Contrairement à UDP, TCP (RFC 793) présente les caractéristiques suivantes :

Conséquences :

A mi-chemin entre UDP et TCP : SCTP (Stream Control Transmission Protocol)

Fonctionnement de TCP : les états

États TCP

Source du schéma

Fonctionnement de TCP : l'envoi et l'acquittement de paquets

Format de segment TCP

Sockets TCP

Socket client TCP

Initialisation d'une socket client

En Java, socket TCP représentée par une instance de java.net.Socket

Constructeurs et méthodes peuvent lever une IOException

Flots de Socket

Pour communiquer entres sockets TCP (locale et distante), on utilise des flots binaires.

Quelques remarques (et rappels) :

Configuration de Socket

Remarque : existence de getters relatifs à ces méthodes (pour obtenir la configuration courante)

Fermeture de Socket

Exemple : un client TCP qui envoie un fichier

public static void main(String[] args)
{
	if (args.length < 3) throw new IllegalArgumentException("Not enough arguments");
	try ( Socket s = new Socket(args[0], Integer.parseInt(args[1]));
		InputStream is = new FileInputStream(args[2]);
		OutputStream os = new BufferedOutputStream(s.getOutputStream()) )
	{
		s.shutdownInput(); // We are not interested in the answer of the server
		transfer(is, os);
		os.flush();
	}
}

Socket serveur TCP

Principe de fonctionnement d'une socket serveur

java.net.ServerSocket

Construction
Acceptation de connexion

Socket accept() retourne la plus ancienne socket de service en attente.
Cette méthode est bloquante.
Timeout instaurable avec setSoTimeout(int timeoutInMillis)

Modèle itératif d'utilisation de ServerSocket

Limitation du modèle : 1 seul client traité à la fois (sinon plusieurs threads nécessaires)

Utilisation d'une socket de service

Exemple : un serveur TCP qui réceptionne des fichiers

public class FileReceiverServer
{
	// After one minute with no new-connection, the server stops
	public static final int ACCEPT_TIMEOUT = 60000;

	private final File directory;
	private final int port;
	
	public FileReceiverServer(File directory, int port)
	{
		this.directory = directory;
		this.port = port;
	}

	public void launch() throws IOException
	{
		try (ServerSocket ss = new ServerSocket(port))
		{
			ss.setSoTimeout(ACCEPT_TIMEOUT);
			while (true)
			{
				try (Socket s = ss.accept();
					OutputStream os = new BufferedOutputStream(new FileOutputStream(
						new File(directory, s.getInetAddress() + "_" + s.getPort())) )
				{
					// Save the data in a file named with the address and port of the client
					transfer(s.getInputStream(), os);
				} catch (SocketTimeoutException e)
				{
					System.err.println("The server will shutdown because no new connections are available");
					break;
				} catch (IOException e)
				{
					e.printStackTrace();
					continue; // Treat the next incoming client
				}
			}
		}
	}
		
	public static void main(String[] args)
	{
		new FileReceiverServer(
			new File(args[0]), Integer.parseInt(args[1])).launch();
	}
}