Vous êtes sur la page 1sur 13

Ch ROGET

Programmation Réseau TD Réalisation D'un Serveur en C/Linux AUX230


AUX230

S
TI
Créé par :@Algorithmics-c.roget

-I
IS
Programmation Réseau
Programmation Réseau TD Réalisation D'un Serveur en C/Linux

Phase 1: Création de Socket TCP


ET
- Identifier les 3 arguments de l'appel socket avec son prototype.
G
- Tester la création de Socket TCP:

- en affichant son descripteur (n° d'ouveture, ici sur l'interface 4/5 avec TCP)
IN

- en vérifiant si elle "visible/idntifiable" de l'extérieur.

#include <sys/socket.h>

Algorithmics Elearning 1 / 13
Ch ROGET

int socket (int domaine, int type, int protocole);

S
TI
Informations:

Domaine de Communication:

-I
AF_INET: IP, TCP, UDP, ICMP.

AF_INET6: protocole IPng, IPv6

IS
AF_UNIX / AF_LOCAL: communication limitée aux processus d'un même Système
(hors programme)

ET
AF_IPX Novell IPX

AF_X25 Protocole ITU-T X.25 / ISO-8208

AF_AX25 Protocole AX.25 radio amateur

AF_ATMPVC Accès direct ATM PVCs


G
AF_APPLETALK Appletalk

AF_NETLINK Interface utilisateur noyau


IN

AF_PACKET Interface paquet bas-niveau

Algorithmics Elearning 2 / 13
Ch ROGET

Type de socket:

S
SOCK_STREAM: dialogue/protocole en mode connecté, flux et circuit virtuel.

SOCK_DGRAM: dialogue/protocole sans connexion, par Datagram.

TI
SOCK_RAW : dialogue/protocole brut avec le protocole.

Domaine de Communication:

-I
Domaine Type Socket Protocole

AF_INET SOCK_STREAM TCP/IP IPPROTO_TCP

IS
AF_INET SOCK_DGRAM UDP/IP IPPROTO_UDP

int socket (int domaine, int type, int protocole);

Phase 2: Identification externe de la SocketET


G
Identifier une socket, c'est définir l'adresse complète à l'extrémité de la communication. Lui affecter un identificateur, un nom, ..., une
adresse afin de la retrouver "partout" et sans ambiguité.
IN

Elle est composée de l'adresse IP (Interface 2/3) et du numéro de port (Interface 4/5).

- Introduire dans votre programme,une structure de données pour identifier/stocker l'adresse complète de la socket. La structure
sockaddr générique et/ou la structure plus ciblée sockaddr_in pour les communications AF_INET.

Algorithmics Elearning 3 / 13
Ch ROGET

Si nécessaire, on peut "caster" (convertir à la volée) cette dernière pour revenir sur la structure générique à toutes les communications
réseau, par un pointeur de structure de type (struct sockaddr *)

S
TI
Informations:

Adresse complète de la socket type sockaddr_in:

-I
#include <netinet/in.h>

Champs Type commentaire

IS
sin_family short int AF_INET

sin_port unsigned short Numéro de port en BIG ENDIAN (ordre des octets du réseau)

sin_addr

ET
struct in_addr Adresse IP (s_addr en BIG ENDIAN).
G
IN

Informations supplémentaires:
gethostbyname, getprotobyname, getservbyname

struct sockaddr {

Algorithmics Elearning 4 / 13
Ch ROGET

unsigned char sa_len; /* longueur totale */

S
sa_family_t sa_family; /* famille d'adresse */

char sa_data[14]; /* valeur de l'adresse */

TI
};

struct sockaddr_in {

-I
uint8_t sin_len; /* longueur totale */

IS
sa_family_t sin_family; /* famille : AF_INET */

in_port_t sin_port; /* le numéro de port */

struct in_addr sin_addr; /* l'adresse internet */

unsigned char sin_zero[8]; /* un champ de 8 zéros */

};
ET
G
struct in_addr {
IN

in_addr_t s_addr;

};

Algorithmics Elearning 5 / 13
Ch ROGET

S
struct hostent *hostinfo = NULL;

TI
SOCKADDR_IN sin = { 0 }; /* initialise la structure avec des 0 */

const char *hostname = "www.developpez.com";

-I
hostinfo = gethostbyname(hostname); /* on récupère les informations de l'hôte auquel on veut se connecter */

if (hostinfo == NULL) /* l'hôte n'existe pas */

IS
{

fprintf (stderr, "Unknown host %s.n", hostname);

}
exit(EXIT_FAILURE);

ET
G
sin.sin_addr = *(IN_ADDR *) hostinfo->h_addr; /* l'adresse se trouve dans le champ h_addr de la structure hostinfo */

sin.sin_port = htons(PORT); /* on utilise htons pour le port */


IN

sin.sin_family = AF_INET;

if(connect(sock,(SOCKADDR *) &sin, sizeof(SOCKADDR)) == SOCKET_ERROR)

Algorithmics Elearning 6 / 13
Ch ROGET

S
perror("connect()");

exit(errno);

TI
}

-I
IS
{

int sock;
ET
int cree_socket_stream (const char * nom_hote, const char * nom_service, const char * nom_proto)
G
struct sockaddr_in adresse;

struct hostent * hostent;


IN

struct servent * servent;

struct protoent * protoent;

if ((hostent = gethostbyname(nom_hote)) == NULL)

Algorithmics Elearning 7 / 13
Ch ROGET

S
perror("gethostbyname");

return -1;

TI
}

if ((protoent = getprotobyname(nom_proto)) == NULL)

-I
perror("getprotobyname");

IS
return -1;

if ((servent = getservbyname(nom_service, protoent->p_name)) == NULL)

}
perror("getservbyname");

return -1;
ET
G
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); return -1; } memset(& adresse, 0, sizeof (struct sockaddr_in)); adresse.sin_family
= AF_INET; adresse.sin_port = servent->s_port; adresse.sin_addr . s_addr = ((struct in_addr *) (hostent->h_addr))->s_addr; if (bind(sock, (struct sockaddr
*) & adresse, sizeof(struct sockaddr_in)) < 0) { close(sock); perror("bind"); return -1; } return sock; }
IN

Algorithmics Elearning 8 / 13
Ch ROGET

sockaddr

S
The first structure is sockaddr that holds the socket information:

TI
struct sockaddr{

unsigned short sa_family;

char sa_data[14];

-I
};

This is a generic socket address structure, which will be passed in most of the socket function calls. The following table provides a

IS
description of the member fields:

Attribute Values Description

sa_family
AF_INET
AF_UNIX
AF_NS
AF_IMPLINK ET
It represents an address family. In most of the Internet-based applications, we use AF_INET.

The content of the 14 bytes of protocol specific address are interpreted according to the type of
G
sa_data Protocol-specific Address address. For the Internet family, we will use port number IP address, which is represented by
sockaddr_in structure defined below.
IN

sockaddr in
The second structure that helps you to reference to the socket's elements is as follows:
struct sockaddr_in {

Algorithmics Elearning 9 / 13
Ch ROGET

short int sin_family;

S
unsigned short int sin_port;

struct in_addr sin_addr;

TI
unsigned char sin_zero[8];

};

Here is the description of the member fields:

-I
Attribute Values Description

IS
AF_INET
AF_UNIX
sa_family It represents an address family. In most of the Internet-based applications, we use AF_INET.
AF_NS
AF_IMPLINK
sin_port
sin_addr
sin_zero
IP Address
Not Used
ET
Service Port A 16-bit port number in Network Byte Order.
A 32-bit IP address in Network Byte Order.
You just set this value to NULL as this is not being used.
G
in addr
IN

This structure is used only in the above structure as a structure field and holds 32 but netid/hostid.
struct in_addr {

unsigned long s_addr;

Algorithmics Elearning 10 / 13
Ch ROGET

};

S
Here is the description of the member fields:

Attribute Values Description

TI
s_addr service port A 32-bit IP address in Network Byte Order.

hostent

-I
This structure is used to keep information related to host.

IS
struct hostent

char *h_name;

char **h_aliases;

int h_addrtype;

int h_length;
ET
G
char **h_addr_list

#define h_addr h_addr_list[0]


IN

};

Here is the description of the member fields:

Algorithmics Elearning 11 / 13
Ch ROGET

Attribute Values Description

S
h_name ti.com etc. It is the official name of the host. For example, tutorialspoint.com, google.com, etc.
h_aliases TI It holds a list of host name aliases.

TI
h_addrtype AF_INET It contains the address family and in case of Internet based application, it will always be AF_INET.
h_length 4 It holds the length of the IP address, which is 4 for Internet Address.

-I
For Internet addresses, the array of pointers h_addr_list[0], h_addr_list[1], and so on, are points to structure
h_addr_list in_addr
in_addr.

IS
NOTE : h_addr is defined as h_addr_list[0] to keep backward compatibility.

servent

struct servent

{
ET
This particular structure is used to keep information related to service and associated ports.
G
char *s_name;

char **s_aliases;
IN

int s_port;

char *s_proto;

};

Algorithmics Elearning 12 / 13
Ch ROGET

Here is the description of the member fields:

S
Attribute Values Description
s_name http This is the official name of the service. For example, SMTP, FTP POP3, etc.

TI
s_aliases ALIAS It holds the list of service aliases. Most of the time this will be set to NULL.
s_port 80 It will have associated port number. For example, for HTTP, this will be 80.

-I
TCP
s_proto It is set to the protocol used. Internet services are provided using either TCP or UDP.
UDP

IS
Tips on Socket Structures
Socket address structures are an integral part of every network program. We allocate them, fill them in, and pass pointers to them to

ET
various socket functions. Sometimes we pass a pointer to one of these structures to a socket function and it fills in the contents.

We always pass these structures by reference (i.e., we pass a pointer to the structure, not the structure itself), and we always pass the
size of the structure as another argument.

When a socket function fills in a structure, the length is also passed by reference, so that its value can be updated by the function. We call
G
these value-result arguments.

Always, set the structure variables to NULL (i.e., '') by using memset() for bzero() functions, otherwise it may get unexpected junk values
IN

in your structure.

Algorithmics Elearning 13 / 13