Académique Documents
Professionnel Documents
Culture Documents
S
TI
Créé par :@Algorithmics-c.roget
-I
IS
Programmation Réseau
Programmation Réseau TD Réalisation D'un Serveur en C/Linux
- en affichant son descripteur (n° d'ouveture, ici sur l'interface 4/5 avec TCP)
IN
#include <sys/socket.h>
Algorithmics Elearning 1 / 13
Ch ROGET
S
TI
Informations:
Domaine de Communication:
-I
AF_INET: IP, TCP, UDP, ICMP.
IS
AF_UNIX / AF_LOCAL: communication limitée aux processus d'un même Système
(hors programme)
ET
AF_IPX Novell IPX
Algorithmics Elearning 2 / 13
Ch ROGET
Type de socket:
S
SOCK_STREAM: dialogue/protocole en mode connecté, flux et circuit virtuel.
TI
SOCK_RAW : dialogue/protocole brut avec le protocole.
Domaine de Communication:
-I
Domaine Type Socket Protocole
IS
AF_INET SOCK_DGRAM UDP/IP IPPROTO_UDP
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:
-I
#include <netinet/in.h>
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
S
sa_family_t sa_family; /* famille d'adresse */
TI
};
struct sockaddr_in {
-I
uint8_t sin_len; /* longueur totale */
IS
sa_family_t sin_family; /* famille : AF_INET */
};
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 */
-I
hostinfo = gethostbyname(hostname); /* on récupère les informations de l'hôte auquel on veut se connecter */
IS
{
}
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_family = AF_INET;
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;
Algorithmics Elearning 7 / 13
Ch ROGET
S
perror("gethostbyname");
return -1;
TI
}
-I
perror("getprotobyname");
IS
return -1;
}
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{
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:
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
S
unsigned short int sin_port;
TI
unsigned char sin_zero[8];
};
-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 {
Algorithmics Elearning 10 / 13
Ch ROGET
};
S
Here is the description of the member fields:
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
};
Algorithmics Elearning 11 / 13
Ch ROGET
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
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