Ajouter 'tsock_V3.c'

This commit is contained in:
Abderrahman El-Ouali 2023-02-13 08:17:08 +01:00
parent 17cc084b86
commit 06977b5e33

372
tsock_V3.c Normal file
View file

@ -0,0 +1,372 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//#include <sys/socket.h>
#include <sys/types.h>
//#include <sys/un.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
void construire_message(char *message, int num, char motif, int lg) {
int i;
int lg_deb;
char num_ch[20];
strcpy(message,"----");
sprintf(num_ch, "%d", num);
strcat(message, num_ch);
lg_deb = strlen(message);
for (i=lg_deb; i<(lg); i++) {
message[i] = motif;
}
}
void afficher_message(char *message, int lg, int num) {
int i;
printf("n°%d (%d) [", num, lg);
for (i=0; i<lg; i++) {
printf("%c", message[i]);
}
printf("]\n");
}
int main (int argc, char **argv) {
int i;
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 transport = 1 ; /* 0=UDP, 1=TCP <- par défaut*/
struct hostent *hp;
struct sockaddr_in adr_em;
struct sockaddr_in adr_local;
struct sockaddr_in adr_distant;
int sock;
int sock_bis;
int port;
unsigned int lg_adr_em=sizeof(adr_em);
int lg_adr_local=sizeof(adr_local);
int lg_adr_dest=sizeof(adr_distant);
int lg_mesg = 30;
int lg_max = 30;
int nb_max_connexion = 5;
char* pmesg;
while ((c = getopt(argc, argv, "psun:l:")) != -1) {
switch (c) {
case 'p': /* On choisit le puit. */
if (source == 1) {
printf("usage: cmd [-p|-s][-u| -n ## |-l ##]\n");
exit(1);
}
source = 0;
break;
case 's': /* On choisit la source. */
if (source == 0) {
printf("usage: cmd [-p|-s][-u| -n ## | -l ##]\n");
exit(1) ;
}
source = 1;
break;
case 'u': /* On choisit le mode UDP. */
transport = 0;
break;
case 'n': /* On choisit le nombre de messages à envoyer ou à lire. */
nb_message = atoi(optarg);
break;
case 'l': /* On choisit la longueur des messages à envoyer ou la longueur maximale des messages à lire. */
lg_mesg = atoi(optarg);
break;
default:
printf("usage: cmd [-p|-s][-u| -n ## | -l ##]\n");
break;
}
}
if (source == -1) {
printf("usage: cmd [-p|-s][-u| -n ## | -l ##]\n");
exit(1) ;
}
if(argc==optind){
exit(1);
}
if(argc == optind + 2){
if (source == 0){
printf("On est dans le puits : nom de machine ignoré\n");
}
port = atoi(argv[optind+1]);
}
else {
if (source == 1){
printf("On est dans la source : il faut un nom de machine et un numéro de port.\n");
exit(1);
}
port = atoi(argv[optind]);
}
int port_affichage = port ;
port = htons(port);
pmesg = malloc(sizeof(char)*lg_mesg);
if (port_affichage<5000){
printf("Le numéro de port doit être supérieur à 5000.\n");
exit(1);
}
if (transport==0) { //protocole UDP
//---------SOURCE---------//
if (source == 1) {
if (nb_message == -1) {
nb_message = 10; // par défaut
}
printf("SOURCE : lg_mesg_emis=%d, port=%d, nb_envois=%d, TP=udp, dest=%s\n", lg_mesg, port_affichage, nb_message, argv[argc-2]);
//création du socket local
if((sock=socket(AF_INET, SOCK_DGRAM,0)) == -1) {
printf("echec de la creation du socket\n");
exit(1);
}
printf("SOURCE: socket\n");
//construction de l'adresse du socket distant
//affectation domaine et num port
memset((char *)&adr_distant, 0, sizeof(adr_distant));
adr_distant.sin_family=AF_INET;
adr_distant.sin_port=port;
//affectation @IP
if((hp=gethostbyname(argv[argc-2])) == NULL) {
printf("erreur gethostbyname\n");
exit(1);
}
memcpy((char*)&(adr_distant.sin_addr.s_addr), hp->h_addr, hp->h_length);
if (lg_mesg<6){
printf("Erreur: la taille du buffer doit être >=6.\n");
exit(1);
}
//envoi
for(i=0; i<nb_message; i++) {
construire_message(pmesg,(i+1), (char)((i%26)+97), lg_mesg);
printf("SOURCE: Envoi ");
afficher_message(pmesg, lg_mesg, (i+1));
sendto(sock, pmesg, lg_mesg, 0,(struct sockaddr*)&adr_distant, lg_adr_dest);
}
if (close(sock)==-1) {
printf("échec de destruction du socket\n");
exit(1);
}
printf("SOURCE: fin\n");
}
//---------PUITS---------//
else {
if (nb_message == -1) {
printf("PUITS : lg_mesg_lu=%d, port=%d, nb_receptions=infini, TP=udp\n", lg_mesg, port_affichage);
} else {
printf("PUITS : lg_mesg_lu=%d, port=%d, nb_receptions=%d, TP=udp\n", lg_mesg, port_affichage, nb_message);
}
//création du socket
if((sock=socket(AF_INET, SOCK_DGRAM,0)) == -1) {
printf("echec de la creation du socket\n");
exit(1);
}
printf("PUITS: socket\n");
//construction de l'@ du socket
memset((char*)&adr_local, 0, sizeof(adr_local));
adr_local.sin_family=AF_INET;
adr_local.sin_port=port;
adr_local.sin_addr.s_addr=INADDR_ANY;
//bind
if (bind(sock,(struct sockaddr*)&adr_local, lg_adr_local) == -1) {
printf("echec du bind\n");
exit(1);
}
int count=0;
lg_max=lg_mesg;
//reception
if (nb_message == -1) {
while ((lg_mesg=recvfrom(sock, pmesg, lg_max, 0, (struct sockaddr*)&adr_em, &lg_adr_em)) != -1) {
printf("PUITS: Reception ");
count++;
afficher_message(pmesg, lg_mesg, count);
}
}
else {
for (int k=0; k<nb_message; k++) {
if((lg_mesg=recvfrom(sock, pmesg, lg_max, 0, (struct sockaddr*)&adr_em, &lg_adr_em)) != -1) {
printf("PUITS: Reception ");
afficher_message(pmesg, lg_mesg, (k+1));
}
}
}
if (close(sock)==-1) {
printf("échec de destruction du socket\n");
exit(1);
}
}
}
else { //connexion TCP
//---------SOURCE---------//
if (source == 1) {
if (nb_message ==-1) {
nb_message = 10;
}
printf("SOURCE : lg_mesg_emis=%d, port=%d, nb_envois=%d, TP=tcp, dest=%s\n", lg_mesg, port_affichage, nb_message, argv[argc-2]);
//création du socket local
if((sock=socket(AF_INET, SOCK_STREAM,0)) == -1) {
printf("echec de la creation du socket\n");
exit(1);
}
//construction de l'adresse du socket distant
//affectation domaine et num port
memset((char *)&adr_distant, 0, sizeof(adr_distant));
adr_distant.sin_family=AF_INET;
adr_distant.sin_port=port;
//affectation @IP
if((hp=gethostbyname(argv[argc-2])) == NULL) {
printf("erreur gethostbyname\n");
exit(1);
}
memcpy((char*)&(adr_distant.sin_addr.s_addr), hp->h_addr, hp->h_length);
printf("SOURCE: socket\n");
//connexion
if (connect(sock,(struct sockaddr*)&adr_distant, lg_adr_dest) == -1) {
printf("echec de la connexion\n");
exit(1);
}
printf("SOURCE: connect\n");
if (lg_mesg<6){
printf("Erreur: la taille du buffer doit être >=6.\n");
exit(1);
}
//envoi
for(i=0; i<nb_message; i++) {
printf("SOURCE: Envoi ");
construire_message(pmesg, (i+1), (char)((i%26)+97), lg_mesg);
afficher_message(pmesg, lg_mesg, (i+1));
send(sock, pmesg, lg_mesg, 0);
}
if (close(sock)==-1) {
printf("échec de destruction du socket\n");
}
printf("SOURCE: fin\n");
}
//---------PUITS---------//
else {
if (nb_message == -1) {
printf("PUITS : lg_mesg_lu=%d, port=%d, nb_receptions=infini, TP=tcp\n", lg_mesg, port_affichage);
} else {
printf("PUITS : lg_mesg_lu=%d, port=%d, nb_receptions=%d, TP=tcp\n", lg_mesg, port_affichage, nb_message);
}
//création du socket
if((sock=socket(AF_INET, SOCK_STREAM,0)) == -1) {
printf("echec de la creation du socket\n");
exit(1);
}
//construction de l'@ du socket
memset((char*)&adr_local, 0, sizeof(adr_local));
adr_local.sin_family=AF_INET;
adr_local.sin_port=port;
adr_local.sin_addr.s_addr=INADDR_ANY;
//bind
if (bind(sock,(struct sockaddr*)&adr_local, lg_adr_local) == -1) {
printf("echec du bind\n");
exit(1);
}
printf("PUITS : socket\n");
//dimensionnement de la file de demandes de connexion
if (listen(sock, nb_max_connexion) == -1) {
printf("echec du listen\n");
exit(1);
}
//acceptation d'une demande de connexion
if((sock_bis=accept(sock,(struct sockaddr *)&adr_em, &lg_adr_em)) == -1) {
printf("echec du accept\n");
exit(1);
}
printf("PUITS: connexion acceptée\n");
//reception
lg_max=lg_mesg;
if (nb_message == -1) {
int count = 0;
while ((lg_mesg=recv(sock_bis, pmesg, lg_max, 0))>0) {
count++;
printf("PUITS: Reception ");
afficher_message(pmesg, lg_mesg, count);
}
} else {
for (int k=0; k<nb_message; k++) {
if ((lg_mesg=recv(sock_bis, pmesg, lg_max, 0))>0) {
printf("PUITS: Reception ");
afficher_message(pmesg, lg_mesg, (k+1));
}
}
}
if (close(sock)==-1) {
printf("échec de destruction du socket\n");
exit(1);
}
printf("PUITS: fin\n");
}
}
}