La communication réseau et TCP-IP
Transcription
La communication réseau et TCP-IP
Ministère de l’Enseignement Supérieur et de la Recherche Scientifique Université Virtuelle de Tunis Programmation Système et Réseau Leçon6 : La communication réseau et TCP-IP Ezzedine Zagrouba Attention ! Ce produit pédagogique numérisé est la propriété exclusive de l'UVT. Il est strictement interdit de la reproduire à des fins commerciales. Seul le téléchargement ou impression pour un usage personnel (1 copie par utilisateur) est permis. ) ! * )+$) 6.1 Sockets, addresses #include <sys/types.h> #include <sys/socket.h> int socket(int domain, int type, int protocol); socket() read write send sendto recv recvfrom ! " #$ $ $ % • • • struct sockaddr $ & struct sockaddr_in sin_family sin_addr sin_port ) ! ! ( AF_INET " $)# " ' $ # * ! + * , $ • • • struct sockaddr_in6 sin6_family sin6_addr " sin6_port AF_INET6 $) # 6.2 Remplissage d'une adresse . socket() & / & bind()( ! #2 3 4 ( 0 1 " +%+ / " # 2 & "fst"( & (# #include <netdb.h> struct hostent *gethostbyname(const char *name); hostent gethostbyname() h_addr , ! 5 ) ! * sin_addr# " sin_port * )+$) 7 htons()# ) • ( • INADDR_ANY# * " $) + 3 $) 3 / " # # getsockname() * 127.0.0.1# * % " 4 • & # INADDR_LOOPBACK " / " % / # #include <sys/socket.h> int getsockname(int s, struct sockaddr *name, socklen_t *namelen); sin_port * :; ntohs() &8 (# *" $) "193.95.32.xxx"( 3 5 inet_ntoa() & 9 &! / (# #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> char *inet_ntoa(struct in_addr in); : " +%+ gethostbyaddr hostent # 6 % " h_name 9 / ) ! * "4 = )+$) $) % # #include <netdb.h> extern int h_errno; struct hostent *gethostbyaddr(const char *addr, int len, int type); struct hostent { char *h_name; /* official name of host */ char **h_aliases; /* alias list */ int h_addrtype; /* host address type */ int h_length; /* length of address */ char **h_addr_list; /* list of addresses */ } #define h_addr h_addr_list[0] /* for backward compatibility */ 6.3 Fermeture d'un socket / close() 3 shutdown()# int shutdown(int fd, int how); / " how 4 2 !& &( &( >0 1 % close()(# 6.4 Communication par datagrammes (UDP) #include <sys/types.h> #include <sys/socket.h> int socket(int domain, int type, int protocol); $ % . -1 AF_INET connect * SOCK_DGRAM 1 #include <sys/types.h> #include <sys/socket.h> / & ( / # int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen); < / 0 ! 0 ) ! * / n( / & + ( ! )+$) t & write(sockfd,t,n)# send() int send(int s, const void *msg, size_t len, int flags); ! 0 MSG_DONTWAIT 1 sendto() int sendto(int s, const void *msg, size_t len, int flags, const struct sockaddr *to, socklen_t tolen); read() $ recvfrom from " " recv() & ( # int recv(int s, void *buf, size_t len, int flags); int recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen); 6.5 Communication par flots de données (TCP) / ! " int fd; .. fd=socket(AF_INET,SOCK_STREAM,0); * / " read() ) 3 & + write() connect()( % fprintf() fscanf() fdopen()# ) @ ! # / listen() ? " " ! accept()# # ) ! * #include <sys/types.h> #include <sys/socket.h> int listen(int s, int backlog); int accept(int s, struct sockaddr / *addr, socklen_t )+$) *addrlen); & backlog 2 345 SOMAXCONN / ! 7 & addr /! addrlen " 1accept() & ! / 16 1 accept() 0 & / fork() 1 6.6 Exercices 2 A ! & 2 )( & )(# '( ,( / / %3 # # #* 5 = # ! : " A & > 4 (# 2 4 #* 5 4 > # - ) * + ! )+$) > int Recherche(char *Abonne, char * telephone) /* cette fonction recherche le numéro de téléphone correspondant à un abonné et elle retourne 1 si l’abonné est trouvé, 0 dans le cas contraire */ '( 2 C & 2 )(# ,( 2 C " " & )(# B