Socket, serveurs, TCP/IP Sockets, cycle de connexion UDP, Internet Protocol
Lecture
Résumé
Sommaire
Extraits
sur 46
Résumé du document
Comment réaliser un dialogue d'ordinateurs ? Comment sont-ils interconnectés ? Datagramme (paquet) service Best-effort : - Pertes, - Ré-alignement, - Duplication, - Délai. Transport Protocols : - Offre des service sur IP, - User Datagram Protocol (UDP), - checksum, - Best-effort, - Transmission Control Protocol (TCP).
Sommaire
I. Computer Chat A. Internet Protocol (IP) B. IP Address C. Transport Protocols D. Ports
II. Comment parler TCP/IP? A. Sockets B. TCP/IP Sockets C. Unix Descriptor Table D. Rappel: type de données E. Addresses Génerique de la Socket
III. Clients et serveurs A. Synoptique du cycle en mode connecté (TCP) B. Cycle de connexion UDP C. TCP Client/Server: Interaction
IV. Association d'une adresse à une socket
Extraits
[...] . TCP/IP Sockets en Computer Chat Comment réaliser un dialogue d'ordinateurs? Comment sont ils interconnectés? [...]
[...] I'm Jane” TCP Client/Serveur: Interaction Client Créer la Socket TCP Etablir la connexion Communiquer quitter Serveur Créer la Socket serveur TCP Affecter un port à la Socket Mettre la Socket à l'écoute Boucle: Accepter nouv.conn Communiquer quitter Le serveur commence par se préparer à recevoir la requête client Students should follow along with source code in the book Synoptique du cycle en mode connecté(TCP) client socket(); server socket(); bind(); connect(); send(); close(); bind(); listen(); accept(); receive(); close(); socket() crée la socket et retourne un nombre (=file descriptor) si pas d'erreur bind() associe le port local à la socket connect() associe l'@ IP dest et le N° port avec la socket et envoie et envoie le segment SYN send() envoie un bloc de données vers la destination listen() bloque la reception jusqu'a la reception de SYN pour ce N° de port local. Crée une nouvelle socket (en rose) et retourne le descripteur de fichier relatif à cette nouvelle socket receive() blocage jusqu'a un bloc de données est prêt. Vous devez spécifier comme argument la taille max du Buffer. [...]
[...] Cycle de Connexion UDP client socket(); bind(); sendto(); close(); serveur socket(); bind(); rcvfrom(); socket() crée la socket et retourne un nombre (=file descriptor) si pas d'erreur bind() associe le port local à la socket sendto() fournit l'@ IPdest, N°port et le message à envoyer recvFrom() blocage jusqu'a reception de données sur ce port. Retourne l'@ IPdest, N°port et le message TCP Client/Server: Interaction Client Créer Etablir Comm Quitter Serveur Créer Aff sock au port Sock écoute Boucle: Accepter Communiquer quitter Create socket for incoming connections if ((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) DieWithError("socket() failed"); Students should follow along with source code in the book TCP Client/Server Interaction Client Créer Socket Etablir connexion Communiquer quitter Server Créer socket Affecter socket au port Socket à l'écoute Boucle: Accepter nouv.conn Communiquer quitter echoServAddr.sin_family = AF_INET; Internet address family echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY);/* Any incoming interface echoServAddr.sin_port = htons(echoServPort); Local port if (bind(servSock, (struct sockaddr &echoServAddr, sizeof(echoServAddr)) < 0) DieWithError("bind() failed"); Students should follow along with source code in the book TCP Client/Server Interaction Client Créer Etablir Communiquer quit Serveur Créer Affecter Socket écoute Boucle: Accepter Comm quit Mark the socket so it will listen for incoming connections if (listen(servSock, MAXPENDING) < 0) DieWithError("listen() failed"); Students should follow along with source code in the book TCP Client/Server Interaction Client Créer Etablir Comm Quit Serveur Créer Affecter Ecoute Boucle: Accepter nouv.conn Communiquer quit for Run forever { clntLen = sizeof(echoClntAddr); if ((clntSock=accept(servSock,(struct sockaddr *)&echoClntAddr,&clntLen)) < 0) DieWithError("accept() failed"); Students should follow along with source code in the book TCP Client/Server: Interaction Client Créer Etablir Comm Quit Serveur Créer TCP socket Affecter Ecoute Boucle: Accepter nouv.conn Communicate quit Le Serveur est bloqué et attend la demande d'un client Students should follow along with source code in the book TCP Client/Server Interaction Client Créer Socket Etablir conn Comm quit Serveur Créer Affecter Ecouter Boucle: Accepter nouv.conn Communicate Close the connection Create a reliable, stream socket using TCP if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) DieWithError("socket() failed"); Students should follow along with source code in the book TCP Client/Server Interaction Client Créer Socket Etablir conn Communiquer quit Serveur Créer affecter ecouter Boucle: Accepter nouv.conn Comm quit echoServAddr.sin_family = AF_INET; Internet address family echoServAddr.sin_addr.s_addr = inet_addr(servIP); Server IP address echoServAddr.sin_port = htons(echoServPort); Server port if (connect(sock, (struct sockaddr &echoServAddr, sizeof(echoServAddr)) < 0) DieWithError("connect() failed"); Students should follow along with source code in the book A new socket is created on the server that is connected to the client socket TCP Client/Server Interaction Client Créer Etablir connexion Communiquer quit Serveur Créer Affecter Ecouter Boucle: Accepter nouv.conn Comm quit if ((clntSock=accept(servSock,(struct sockaddr *)&echoClntAddr,&clntLen)) < 0) DieWithError("accept() failed"); Students should follow along with source code in the book TCP Client/Server Interaction Client Créer connexion Communiquer Quit Serveur Créer Affecter Ecouter Boucle: Accepter nou.conn Communiquer quit echoStringLen = strlen(echoString); Determine input length Send the string to the server if (send(sock, echoString, echoStringLen, = echoStringLen) DieWithError("send() sent a different number of bytes than expected"); Students should follow along with source code in the book TCP Client/Server Interaction Client Créer connexion Communiquer quit Serveur Créer Affecter Ecouter Boucle: Accepter nouv.conn Communiquer quit Receive message from client if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, < 0) DieWithError("recv() failed"); Students should follow along with source code in the book TCP Client/Server Interaction Client Créer connexion Communiquer Quit Server Créer Affecter Ecouter Boucle: Accepter nouv.conn Communicaquer Quit close(sock); close(clntSocket) Students should follow along with source code in the book Note that server closes connection socket, not listening socket Conditions Client send(“Hello Bob”) recv() “Hi Jane” Server recv() “Hello ” recv() “Bob” send(“Hi ”) send(“Jane”) Client connait @IP et N°port serveur Serveur connait son port pas de correlation entre send() and recv() The client must know the server address and port The server discovers the client's address and port at connection Libération de la connexion close() delimite la communication Similaire à EOF Echo Client send(string) while (not received entire string) recv(buffer) print(buffer) close(socket) Echo Serveur recv(buffer) while(client has not closed connection) send(buffer) recv(buffer) close(client socket) In echo application, how does server know when client is done considering it doesn't know echo string length? [...]
[...] Création d'une socket Fonction socket() Socket: Crée une socket. Syntaxe : int socket(int PF, int TYPE, int PROTOCOL) ; L'entier retourné est le descripteur de la socket nouvellement créée Par cet appel. Librairies requises : PF : spécifie la famille de protocole (“Protocol Family”) à utiliser avec la socket - PF_INET (IPv4), PF_INET6 (IPv6), PF_APPLETALK .TYPE : spécifie le type de communication désiré - SOCK_STREAM (Mode connecté), SOCK_DGRAM (Mode non connecté), SOCK_RAW (Mode direct avec la couche IP:ICMP) PROTOCOL : spécifie le protocole à utiliser -IPPROTO_TCP IPPROTO_UDP (UDP) En général, PROTOCOL vaut 0 car l'association de la famille de protocole et du type de communication définit explicitement le protocole de transport Structure sock_addr Structure générique sockaddr struct sockaddr { unsigned short sa_family ; char sa_data[14] ; } ; Famille PF_INET : sockaddr_in, in_addr struct sockaddr_in { short sin_family ; PF_INET unsigned short sin_port ; N° de port. [...]
[...] In WinSock, Socket handles and file handles are not the same thing. Unix Descriptor Table Descriptor Table Data structure for file 0 Data structure for file 1 Data structure for file 2 Socket Descriptor Data Structure Descriptor Table Family: PF_INET Service: SOCK_STREAM Local IP: Remote IP: Local Port: 2249 Remote Port: 3726 sin_len sa_len sa_family sa_data AF_INET sin_port sin_addr sin_zero sockaddr sockaddr_in Rappel: type de données int8_t signed 8bit int uint8_t unsigned 8 bit int int16_t signed 16 bit int uint16_t unsigned 16 bit int int32_t signed 32 bit int uint32_t unsigned 32 bit int u_char, u_short, u_int, u_long Type de données dans les Sockets sa_family_t address family socklen_t length of struct in_addr_t IPv4 address in_port_t IP port number ‘h' : host byte order ‘n' : network byte order ‘s' : short (16bit) ‘l' : long (32bit) uint16_t htons(uint16_t); uint16_t ntohs(uint_16_t); uint32_t htonl(uint32_t); uint32_t ntohl(uint32_t); Fonctions d'ordre Réseau Addresses Génerique de la Socket struct sockaddr { uint8_t sa_len; sa_family_t sa_family; char sa_data[14]; sa_family specifie le type d'adresse. [...]