2024-02-29 18:44:49 +01:00
|
|
|
/* librairie standard ... */
|
|
|
|
#include <stdlib.h>
|
|
|
|
/* pour getopt */
|
|
|
|
#include <unistd.h>
|
|
|
|
/* déclaration des types de base */
|
|
|
|
#include <sys/types.h>
|
|
|
|
/* constantes relatives aux domaines, types et protocoles */
|
|
|
|
#include <sys/socket.h>
|
|
|
|
/* constantes et structures propres au domaine UNIX */
|
|
|
|
#include <sys/un.h>
|
|
|
|
/* constantes et structures propres au domaine INTERNET */
|
|
|
|
#include <netinet/in.h>
|
|
|
|
/* structures retournées par les fonctions de gestion de la base de
|
2024-03-07 09:53:44 +01:00
|
|
|
données du réseau */
|
2024-02-29 18:44:49 +01:00
|
|
|
#include <netdb.h>
|
|
|
|
/* pour les entrées/sorties */
|
|
|
|
#include <stdio.h>
|
|
|
|
/* pour la gestion des erreurs */
|
|
|
|
#include <errno.h>
|
2024-03-01 16:11:19 +01:00
|
|
|
#define BASE_SIZE 10
|
2024-02-29 18:44:49 +01:00
|
|
|
|
2024-03-01 16:11:19 +01:00
|
|
|
void construire_message(char *message, char motif, int lg) {
|
|
|
|
int i;
|
|
|
|
for (i=0;i<lg;i++) message[i] = motif;
|
|
|
|
}
|
|
|
|
|
|
|
|
void afficher_message(char *message, int lg) {
|
|
|
|
int i;
|
2024-03-01 16:30:54 +01:00
|
|
|
printf("message reçu : ");
|
2024-03-01 16:11:19 +01:00
|
|
|
for (i=0;i<lg;i++){
|
2024-03-07 09:53:44 +01:00
|
|
|
printf("%c", message[i]);
|
2024-03-01 16:11:19 +01:00
|
|
|
}
|
2024-03-07 09:53:44 +01:00
|
|
|
printf("\n");
|
2024-03-01 16:11:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main (int argc, char **argv)
|
2024-02-29 18:44:49 +01:00
|
|
|
{
|
2024-03-07 09:53:44 +01:00
|
|
|
int c;
|
|
|
|
extern char *optarg;
|
|
|
|
extern int optind;
|
|
|
|
int nb_message = -1; /* Nb de messages à envoyer ou à recevoir, par défaut : 10 en émission, infini en réception */
|
|
|
|
int source = -1 ; /* 0=puits, 1=source */
|
|
|
|
int udp=0; /* 0=TCP, 1=UDP */
|
|
|
|
|
|
|
|
while ((c = getopt(argc, argv, "pn:su")) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'p':
|
|
|
|
if (source == 1) {
|
|
|
|
printf("usage: cmd [-p|-s][-n ##]\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
source = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
if (source == 0) {
|
|
|
|
printf("usage: cmd [-p|-s][-n ##]\n");
|
|
|
|
exit(1) ;
|
|
|
|
}
|
|
|
|
source = 1;
|
|
|
|
break;
|
2024-03-01 16:29:29 +01:00
|
|
|
|
2024-03-07 09:53:44 +01:00
|
|
|
case 'n':
|
|
|
|
nb_message = atoi(optarg);
|
|
|
|
break;
|
2024-03-01 16:29:29 +01:00
|
|
|
|
2024-03-07 09:53:44 +01:00
|
|
|
case 'u':
|
|
|
|
udp=1;
|
|
|
|
break;
|
2024-03-01 16:29:29 +01:00
|
|
|
|
2024-03-07 09:53:44 +01:00
|
|
|
default:
|
|
|
|
printf("usage: cmd [-p|-s][-n ##]\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-03-01 16:29:29 +01:00
|
|
|
|
2024-03-07 09:53:44 +01:00
|
|
|
char* nom_machine_distante;
|
2024-03-01 16:29:29 +01:00
|
|
|
|
2024-03-07 09:53:44 +01:00
|
|
|
// Recuperation du port
|
|
|
|
int port=atoi(argv[argc-1]);
|
|
|
|
port = htons(port);
|
|
|
|
|
|
|
|
char* message = malloc(BASE_SIZE * sizeof(char));
|
|
|
|
|
|
|
|
if (source == -1) {
|
|
|
|
printf("usage: cmd [-p|-s][-n ##]\n");
|
|
|
|
exit(1) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (source == 1) {
|
|
|
|
printf("on est dans le source\n");
|
|
|
|
|
|
|
|
//Recuperation du nom logique
|
|
|
|
nom_machine_distante=argv[argc-2];
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
printf("on est dans le puits\n");
|
|
|
|
|
|
|
|
if (nb_message != -1) {
|
|
|
|
if (source == 1)
|
|
|
|
printf("nb de tampons à envoyer : %d\n", nb_message);
|
|
|
|
else
|
|
|
|
printf("nb de tampons à recevoir : %d\n", nb_message);
|
|
|
|
} else {
|
|
|
|
if (source == 1) {
|
|
|
|
nb_message = 10 ;
|
|
|
|
printf("nb de tampons à envoyer = 10 par défaut\n");
|
|
|
|
} else
|
|
|
|
printf("nb de tampons à envoyer = infini\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (udp==1){
|
|
|
|
// Creation du socket local
|
|
|
|
int sock= socket(AF_INET,SOCK_DGRAM,0);
|
|
|
|
|
|
|
|
if (source==1) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Creation de l'adresse du socket distant
|
|
|
|
struct hostent *hp ;
|
|
|
|
struct sockaddr_in adr_dest;
|
|
|
|
int longueur_adr_dest = sizeof(adr_dest);
|
|
|
|
|
|
|
|
int longueur_message = BASE_SIZE;
|
|
|
|
|
|
|
|
memset((char *)& adr_dest, 0, sizeof(adr_dest)) ;
|
|
|
|
|
|
|
|
adr_dest.sin_family=AF_INET;
|
|
|
|
adr_dest.sin_port=port;
|
|
|
|
|
|
|
|
|
|
|
|
if ((hp = gethostbyname(nom_machine_distante)) == NULL) {
|
|
|
|
printf("erreur gethostbyname\n") ;
|
|
|
|
exit(1) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy( (char*)&(adr_dest.sin_addr.s_addr),
|
|
|
|
hp->h_addr,
|
|
|
|
hp->h_length ) ;
|
|
|
|
|
|
|
|
for (int i = 0; i < nb_message; i++) {
|
|
|
|
// Construction du message
|
|
|
|
construire_message(message, 'a' + (i % 26), BASE_SIZE);
|
|
|
|
|
|
|
|
// Envoi du message
|
|
|
|
|
|
|
|
sendto(sock,message,longueur_message,0,(struct sockaddr*)&adr_dest,longueur_adr_dest);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
|
|
|
|
// Creation de l'adresse du socket distant
|
|
|
|
struct sockaddr_in adr_locale;
|
|
|
|
int longueur_adr_locale = sizeof(adr_locale);
|
|
|
|
|
|
|
|
int longueur_message = BASE_SIZE;
|
|
|
|
|
|
|
|
memset((char *)& adr_locale, 0, sizeof(adr_locale)) ;
|
|
|
|
|
|
|
|
adr_locale.sin_family=AF_INET;
|
|
|
|
adr_locale.sin_port=port;
|
|
|
|
adr_locale.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
|
|
|
// Bind the local socket to any local address (ie any available interface)
|
|
|
|
if (bind(sock, (struct sockaddr *) &adr_locale, longueur_adr_locale) == -1) {
|
|
|
|
printf("failed to bind\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
// Receive a single message because we are lazy
|
|
|
|
recvfrom(sock, message, longueur_message, 0, NULL, NULL);
|
|
|
|
|
|
|
|
// Afficher notre seule et unique triste message
|
|
|
|
afficher_message(message, longueur_message);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2024-02-29 18:44:49 +01:00
|
|
|
}
|
|
|
|
|