English French
A detailed discussion of the socket API is outside the scope of this section and the references cited above provide a detailed discussion of all the details of the socket API. As a starting point, it is interesting to compare the socket API with the service primitives that we have discussed in the previous chapter. Let us first consider the connectionless service that consists of the following two primitives : Une discussion détaillée de l'API de socket sort du cadre de cette section et les références citées ci-dessus fournissent une discussion détaillée de tous les détails de l'API de socket. Pour commencer, il est intéressant de comparer l'API socket avec les primitives de service que nous avons abordées dans le chapitre précédent. Considérons d'abord le service sans connexion qui se compose des deux primitives suivantes :
An IPv6 address often identifies a computer and not a program running on the computer. In order to identify a specific program running on a specific computer, we use a *port number* in addition to the IPv6 address. A program using an IPv6 socket is this identified using : Une adresse IPv6 identifie souvent un ordinateur et non pas un programme exécuté sur cet ordinateur. Afin d'identifier un programme spécifique fonctionnant sur un ordinateur spécifique, nous utilisons un *numéro de port* en plus de l'adresse IPv6. Un programme utilisant un socket IPv6 est ainsi identifié par :
A program can use the ``socket`` system call to create a new socket. Un programme peut utiliser l'appel système ``socket`` pour créer un nouveau socket.
A program can use the ``struct sockaddr_in6`` to represent IPv6 socket addresses. The following program creates a ``struct sockaddr_in6`` that identifies the program that reserved the port number ``55555`` on the computer identified by the ``::1`` IPv6 address. Un programme peut utiliser la structure sockaddr_in6 pour représenter les adresses de socket IPv6. Le programme suivant crée une ``struct sockaddr_in6`` qui identifie le programme ayant réservé le numéro de port ``55555`` sur l'ordinateur identifié par l'adresse IPv6 ``::1``.
As the ``sendto`` function is generic, this function will work correctly independently from the fact that the peer's address is defined as a path on the computer filesystem or a network address. Comme la fonction ``sendto`` est générique, cette fonction fonctionnera correctement indépendamment du fait que l'adresse de l'homologue soit définie comme un chemin sur le système de fichiers de l'ordinateur ou une adresse réseau.
Besides 16 and 32 bit words, some applications need to exchange data structures containing bit fields of various lengths. For example, a message may be composed of a 16 bits field followed by eight, one bit flags, a 24 bits field and two 8 bits bytes. Internet protocol specifications will define such a message by using a representation such as the one below. In this representation, each line corresponds to 32 bits and the vertical lines are used to delineate fields. The numbers above the lines indicate the bit positions in the 32-bits word, with the high order bit at position `0`. Outre les mots de 16 et 32 bits, certaines applications doivent échanger des structures de données contenant des champs de bits de différentes longueurs. Par exemple, un message peut être composé d'un champ de 16 bits suivi de huit drapeaux d'un bit, d'un champ de 24 bits et de deux octets de 8 bits. Les spécifications du protocole Internet définiront un tel message en utilisant une représentation telle que celle qui suit. Dans cette représentation, chaque ligne correspond à 32 bits et les lignes verticales sont utilisées pour délimiter les champs. Les chiffres au-dessus des lignes indiquent la position des bits dans le mot de 32 bits, le bit de poids fort étant en position "0".
Besides character strings, some applications also need to exchange 16 bits and 32 bits fields such as integers. A naive solution would have been to send the 16- or 32-bits field as it is encoded in the host's memory. Unfortunately, there are different methods to store 16- or 32-bits fields in memory. Some CPUs store the most significant byte of a 16-bits field in the first address of the field while others store the least significant byte at this location. When networked applications running on different CPUs exchange 16 bits fields, there are two possibilities to transfer them over the transport service : Outre les chaînes de caractères, certaines applications doivent également échanger des champs de 16 et 32 bits tels que des entiers. Une solution naïve aurait été d'envoyer le champ de 16 ou 32 bits tel qu'il est codé dans la mémoire de l'hôte. Malheureusement, il existe différentes méthodes pour stocker les champs de 16 ou 32 bits en mémoire. Certains processeurs stockent l'octet le plus significatif d'un champ de 16 bits dans la première adresse du champ, tandis que d'autres stockent l'octet le moins significatif à cet endroit. Lorsque des applications en réseau fonctionnant sur des unités centrales différentes échangent des champs de 16 bits, il existe deux possibilités pour les transférer via le service de transport :
``connect``: connecting a socket to a remote address ``connect``:connecter un socket à une adresse distante
Creating a new socket to communicate through a network Création d'une nouvelle socket pour communiquer via un réseau
`DATA.indication(message)` is issued by the transport service to deliver a message to the application. In the socket API, this corresponds to the return of the ``recv`` method that is called by the application. `DATA.indication(message)` est émis par le service de transport pour délivrer un message à l'application. Dans l'API socket, cela correspond au retour de la méthode ``recv`` qui est appelée par l'application.
`DATA.request(destination,message)` is used to send a message to a specified destination. In this socket API, this corresponds to the ``send`` method. `DATA.request(destination,message)` est utilisé pour envoyer un message à une destination spécifiée. Dans cette API socket, cela correspond à la méthode ``send``.
Depending on the socket address family, the operating system might implicitly assign an address to an unbound socket upon a call to ``write``, ``send`` or ``sendto``. While this is a useful behavior, describing it precisely is out of the scope of this section. En fonction de la famille d'adresse de la socket, le système d'exploitation peut implicitement assigner une adresse à un socket non liée lors d'un appel à ``write``, ``send`` ou ``sendto``. Bien que ce comportement soit utile, sa description précise n'entre pas dans le cadre de cette section.
Endianness: exchanging integers between different computers Endianness : échange d'entiers entre différents ordinateurs
Exercises Exercices
Footnotes Notes de pied de page
For example, the ``htonl(3)`` (resp. ``ntohl(3)``) function the standard C library converts a 32-bits unsigned integer from the byte order used by the CPU to the network byte order (resp. from the network byte order to the CPU byte order). Similar functions exist in other programming languages. Par exemple, la fonction ``htonl(3)`` (resp. ``ntohl(3)``) de la bibliothèque C standard convertit un entier non signé de 32 bits de l'ordre des octets utilisé par le CPU à l'ordre des octets du réseau (resp. de l'ordre des octets du réseau à l'ordre des octets du CPU). Des fonctions similaires existent dans d'autres langages de programmation.
Here are some exercises that will help you to learn how to use sockets. Voici quelques exercices qui vous permettront d'apprendre à utiliser les sockets.
In the following example, a C program is sending the bytes ``'h'``, ``'e'``, ``'l'``, ``'l'`` and ``'o'`` to a remote process located at address ``peer_addr``, using the already created socket ``sock``. Dans l'exemple suivant, un programme C envoie les octets ``'h'``, ``'e'``, ``'l'``, ``'l'`` et ``'o'`` à un processus distant situé à l'adresse ``peer_addr``, en utilisant la socket ``sock`` déjà créée.
Message format Format du message
Networked applications were usually implemented by using the :term:`socket` :term:`API`. This API was designed when TCP/IP was first implemented in the `Unix BSD`_ operating system [Sechrest]_ [LFJLMT]_, and has served as the model for many APIs between applications and the networking stack in an operating system. Although the socket API is very popular, other APIs have also been developed. For example, the STREAMS API has been added to several Unix System V variants [Rago1993]_. The socket API is supported by most programming languages and several textbooks have been devoted to it. Users of the C language can consult [DC2009]_, [Stevens1998]_, [SFR2004]_ or [Kerrisk2010]_. The Java implementation of the socket API is described in [CD2008]_ and in the `Java tutorial <http://java.sun.com/docs/books/tutorial/networking/sockets/index.html>`_. In this section, we will use the C socket API to illustrate the key concepts. Les applications en réseau sont généralement mises en œuvre à l'aide de l'interface :term:`socket` :term:`API`. Cette API a été conçue lorsque TCP/IP a été implémenté pour la première fois dans le système d'exploitation `Unix BSD`_ [Sechrest]_ [LFJLMT]_, et a servi de modèle pour de nombreuses API entre les applications et la pile réseau dans un système d'exploitation. Bien que l'API socket soit très populaire, d'autres API ont également été développées. Par exemple, l'API STREAMS a été ajoutée à plusieurs variantes d'Unix System V [Rago1993]_. L'API socket est supportée par la plupart des langages de programmation et plusieurs manuels lui ont été consacrés. Les utilisateurs du langage C peuvent consulter [DC2009]_, [Stevens1998]_, [SFR2004]_ ou [Kerrisk2010]_. L'implémentation Java de l'API socket est décrite dans [CD2008]_ et dans le `tutoriel Java <http://java.sun.com/docs/books/tutorial/networking/sockets/index.html>`_. Dans cette section, nous utiliserons l'API socket C pour illustrer les concepts clés.