Changes between Version 4 and Version 5 of IOC_T06
- Timestamp:
- Apr 19, 2019, 2:33:21 PM (6 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
IOC_T06
v4 v5 52 52 Illustration dans un programme où le client envoie un message à un serveur (qui ne lui répond pas). 53 53 54 ** client.c :54 ** server.c : 55 55 {{{ 56 56 #!c 57 /* A simple server in the internet domain using TCP The port number is passed as an argument */ 57 58 #include <stdio.h> 58 59 #include <stdlib.h> 60 #include <string.h> 59 61 #include <unistd.h> 60 #include <string.h>61 62 #include <sys/types.h> 62 63 #include <sys/socket.h> 63 64 #include <netinet/in.h> 65 64 66 #include <netdb.h> 67 #include <arpa/inet.h> 65 68 66 69 void error(const char *msg) 67 70 { 68 71 perror(msg); 69 exit( 0);72 exit(1); 70 73 } 71 74 72 75 int main(int argc, char *argv[]) 73 76 { 74 int sockfd, portno, n; 75 struct sockaddr_in serv_addr; 76 struct hostent *server; 77 77 int sockfd, newsockfd, portno; 78 socklen_t clilen; 78 79 char buffer[256]; 79 80 // Le client doit connaitre l'adresse IP du serveur, et son numero de port81 if (argc < 3) { 82 fprintf(stderr,"usage %s hostname port\n", argv[0]);83 exit(0);84 }85 portno = atoi(argv[2]);86 87 // 1) Création de la socket, INTERNET etTCP80 struct sockaddr_in serv_addr, cli_addr; 81 int n; 82 83 if (argc < 2) { 84 fprintf(stderr, "ERROR, no port provided\n"); 85 exit(1); 86 } 87 88 // 1) on crée la socket, SOCK_STREAM signifie TCP 88 89 89 90 sockfd = socket(AF_INET, SOCK_STREAM, 0); … … 91 92 error("ERROR opening socket"); 92 93 93 server = gethostbyname(argv[1]); 94 if (server == NULL) { 95 fprintf(stderr,"ERROR, no such host\n"); 96 exit(0); 97 } 98 99 // On donne toutes les infos sur le serveur 94 // 2) on réclame au noyau l'utilisation du port passé en paramètre 95 // INADDR_ANY dit que la socket va être affectée à toutes les interfaces locales 100 96 101 97 bzero((char *) &serv_addr, sizeof(serv_addr)); 98 portno = atoi(argv[1]); 102 99 serv_addr.sin_family = AF_INET; 103 bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);100 serv_addr.sin_addr.s_addr = INADDR_ANY; 104 101 serv_addr.sin_port = htons(portno); 105 106 // On se connecte. L'OS local nous trouve un numéro de port, grâce auquel le serveur 107 // peut nous renvoyer des réponses, le \n permet de garantir que le message ne reste 108 // pas en instance dans un buffer d'emission chez l'emetteur (ici c'est le clent). 109 110 if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 111 error("ERROR connecting"); 112 113 strcpy(buffer,"Coucou Peri\n"); 114 n = write(sockfd,buffer,strlen(buffer)); 115 116 // On ferme la socket 102 if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) 103 error("ERROR on binding"); 104 105 106 // On commence à écouter sur la socket. Le 5 est le nombre max 107 // de connexions pendantes 108 109 listen(sockfd, 5); 110 while (1) { 111 newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); 112 if (newsockfd < 0) 113 error("ERROR on accept"); 114 115 bzero(buffer, 256); 116 n = read(newsockfd, buffer, 255); 117 if (n < 0) 118 error("ERROR reading from socket"); 119 120 printf("Received packet from %s:%d\nData: [%s]\n\n", 121 inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port), 122 buffer); 123 124 close(newsockfd); 125 } 117 126 118 127 close(sockfd); … … 121 130 }}} 122 131 123 ** server.c132 ** client.c 124 133 {{{ 125 134 #!c