début v4

This commit is contained in:
nbillard 2023-02-13 08:44:51 +01:00
부모 06977b5e33
커밋 41fd577b34
3개의 변경된 파일442개의 추가작업 그리고 56개의 파일을 삭제

파일 보기

@ -1,9 +1,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
//#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
//#include <sys/un.h> #include <sys/un.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netdb.h> #include <netdb.h>
#include <unistd.h> #include <unistd.h>
@ -98,38 +98,38 @@ int main (int argc, char **argv) {
exit(1) ; exit(1) ;
} }
if(argc==optind){ if(argc==optind){
exit(1); exit(1);
} }
if(argc == optind + 2){ if(argc == optind + 2){
if (source == 0){ if (source == 0){
printf("On est dans le puits : nom de machine ignoré\n"); printf("On est dans le puits : nom de machine ignoré\n");
} }
port = atoi(argv[optind+1]); port = atoi(argv[optind+1]);
} }
else { else {
if (source == 1){ if (source == 1){
printf("On est dans la source : il faut un nom de machine et un numéro de port.\n"); printf("On est dans la source : il faut un nom de machine et un numéro de port.\n");
exit(1); exit(1);
} }
port = atoi(argv[optind]); port = atoi(argv[optind]);
} }
int port_affichage = port ; int port_affichage = port ;
port = htons(port); port = htons(port);
pmesg = malloc(sizeof(char)*lg_mesg); pmesg = malloc(sizeof(char)*lg_mesg);
if (port_affichage<5000){ if (port_affichage<5000){
printf("Le numéro de port doit être supérieur à 5000.\n"); printf("Le numéro de port doit être supérieur à 5000.\n");
exit(1); exit(1);
} }
if (transport==0) { //protocole UDP if (transport==0) { //protocole UDP
//---------SOURCE---------// //---------SOURCE---------//
if (source == 1) { if (source == 1) {
@ -143,7 +143,7 @@ int main (int argc, char **argv) {
printf("echec de la creation du socket\n"); printf("echec de la creation du socket\n");
exit(1); exit(1);
} }
printf("SOURCE: socket\n"); printf("SOURCE: socket\n");
//construction de l'adresse du socket distant //construction de l'adresse du socket distant
//affectation domaine et num port //affectation domaine et num port
@ -160,10 +160,10 @@ int main (int argc, char **argv) {
if (lg_mesg<6){ if (lg_mesg<6){
printf("Erreur: la taille du buffer doit être >=6.\n"); printf("Erreur: la taille du buffer doit être >=6.\n");
exit(1); exit(1);
} }
//envoi //envoi
for(i=0; i<nb_message; i++) { for(i=0; i<nb_message; i++) {
@ -180,7 +180,7 @@ int main (int argc, char **argv) {
printf("SOURCE: fin\n"); printf("SOURCE: fin\n");
} }
//---------PUITS---------// //---------PUITS---------//
else { else {
if (nb_message == -1) { if (nb_message == -1) {
@ -194,7 +194,7 @@ int main (int argc, char **argv) {
printf("echec de la creation du socket\n"); printf("echec de la creation du socket\n");
exit(1); exit(1);
} }
printf("PUITS: socket\n"); printf("PUITS: socket\n");
//construction de l'@ du socket //construction de l'@ du socket
memset((char*)&adr_local, 0, sizeof(adr_local)); memset((char*)&adr_local, 0, sizeof(adr_local));
@ -220,7 +220,7 @@ int main (int argc, char **argv) {
} }
} }
else { else {
for (int k=0; k<nb_message; k++) { 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) { if((lg_mesg=recvfrom(sock, pmesg, lg_max, 0, (struct sockaddr*)&adr_em, &lg_adr_em)) != -1) {
printf("PUITS: Reception "); printf("PUITS: Reception ");
@ -236,9 +236,9 @@ int main (int argc, char **argv) {
} }
} }
} }
else { //connexion TCP else { //connexion TCP
//---------SOURCE---------// //---------SOURCE---------//
if (source == 1) { if (source == 1) {
if (nb_message ==-1) { if (nb_message ==-1) {
@ -278,12 +278,12 @@ int main (int argc, char **argv) {
printf("SOURCE: connect\n"); printf("SOURCE: connect\n");
if (lg_mesg<6){ if (lg_mesg<6){
printf("Erreur: la taille du buffer doit être >=6.\n"); printf("Erreur: la taille du buffer doit être >=6.\n");
exit(1); exit(1);
} }
//envoi //envoi
for(i=0; i<nb_message; i++) { for(i=0; i<nb_message; i++) {
printf("SOURCE: Envoi "); printf("SOURCE: Envoi ");
construire_message(pmesg, (i+1), (char)((i%26)+97), lg_mesg); construire_message(pmesg, (i+1), (char)((i%26)+97), lg_mesg);
@ -299,7 +299,7 @@ int main (int argc, char **argv) {
} }
//---------PUITS---------// //---------PUITS---------//
else { else {
if (nb_message == -1) { if (nb_message == -1) {
@ -362,7 +362,7 @@ int main (int argc, char **argv) {
} }
if (close(sock)==-1) { if (close(sock)==-1) {
printf("échec de destruction du socket\n"); printf("Échec de destruction du socket\n");
exit(1); exit(1);
} }

파일 보기

@ -42,7 +42,7 @@ void construire_message(char* message, char motif, int lg) {
void recvudp(int sock, char* buffer, int buf_len, void* addr, unsigned int addr_len) { void recvudp(int sock, char* buffer, int buf_len, void* addr, unsigned int addr_len) {
int lg_recv = recvfrom(sock, buffer, buf_len, 0, (struct sockaddr*) addr, &addr_len); int lg_recv = recvfrom(sock, buffer, buf_len, 0, (struct sockaddr*) addr, &addr_len);
if (lg_recv == -1) { if (lg_recv == -1) {
perror("Message could not be recieved, Error: "); perror("Message could not be recieved, Error");
exit(1); exit(1);
} else if (lg_recv < buf_len) { } else if (lg_recv < buf_len) {
fprintf(stderr, "Only %i from %i characters have been reviewed.", lg_recv, buf_len); fprintf(stderr, "Only %i from %i characters have been reviewed.", lg_recv, buf_len);
@ -57,7 +57,7 @@ void recvudp(int sock, char* buffer, int buf_len, void* addr, unsigned int addr_
void sendudp(int sock, char* message, int msg_len, void* addr, unsigned int addr_len) { void sendudp(int sock, char* message, int msg_len, void* addr, unsigned int addr_len) {
int lg_sent = sendto(sock, message, msg_len, 0, (struct sockaddr*) addr, addr_len); int lg_sent = sendto(sock, message, msg_len, 0, (struct sockaddr*) addr, addr_len);
if (lg_sent == -1) { if (lg_sent == -1) {
perror("Message could not be sent, Error: "); perror("Message could not be sent, Error");
exit(1); exit(1);
} else if (lg_sent < MSG_LENGTH) { } else if (lg_sent < MSG_LENGTH) {
fprintf(stderr, "Only %i from %i characters have been sent.", lg_sent, MSG_LENGTH); fprintf(stderr, "Only %i from %i characters have been sent.", lg_sent, MSG_LENGTH);
@ -181,7 +181,7 @@ int main (int argc, char **argv)
sock = socket(AF_INET, SOCK_DGRAM, 0); sock = socket(AF_INET, SOCK_DGRAM, 0);
} }
if (sock < 0) { if (sock < 0) {
perror("Error creating socket:"); perror("Error creating socket");
exit(1); exit(1);
} }
@ -201,7 +201,7 @@ int main (int argc, char **argv)
// liaison entre l'addresse locale et le buffer(sock) // liaison entre l'addresse locale et le buffer(sock)
if (bind(sock, (struct sockaddr*) &addr_local, (sizeof(addr_local))) < 0 ) { if (bind(sock, (struct sockaddr*) &addr_local, (sizeof(addr_local))) < 0 ) {
perror("Error while binding socket: "); perror("Error while binding socket");
exit(1); exit(1);
} }
@ -235,14 +235,14 @@ int main (int argc, char **argv)
} else { } else {
// ouverture pour l'écoute de connexions // ouverture pour l'écoute de connexions
if (listen(sock, NB_CLIENTS) < 0) { if (listen(sock, NB_CLIENTS) < 0) {
perror("Server could not listen: "); perror("Server could not listen");
exit(1); exit(1);
} else { } else {
printf("listening\n"); printf("listening\n");
} }
// accepte la première demande de connexion // accepte la première demande de connexion
if (accept(sock, (struct sockaddr*) &addr_distant, &addr_distant_len) < 0) { if (accept(sock, (struct sockaddr*) &addr_distant, &addr_distant_len) < 0) {
perror("Server could not accept connection: "); perror("Server could not accept connection");
exit(1); exit(1);
} else { } else {
printf("accepting request\n"); printf("accepting request\n");
@ -312,7 +312,7 @@ int main (int argc, char **argv)
} }
if (shutdown(sock, SHUT_RDWR)==-1) { if (shutdown(sock, SHUT_RDWR)==-1) {
perror("echec de destruction du socket: \n"); perror("echec de destruction du socket");
} }
} else /*puit */ { } else /*puit */ {
char buffer[TCP_BUFFER_LEN]; char buffer[TCP_BUFFER_LEN];
@ -352,7 +352,7 @@ int main (int argc, char **argv)
if (close(sock) < 0) { if (close(sock) < 0) {
perror("Error closing socket:"); perror("Error closing socket");
exit(1); exit(1);
} }
return 0; return 0;

386
tsock_v4.c Normal file
파일 보기

@ -0,0 +1,386 @@
#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, char motif, int lg) {
int i;
for (i=0; i<(lg); i++) {
message[i] = motif;
}
}
void afficher_message(char *message, int lg, int num) {
int i;
printf("n°%d (%d) [----%d", num, lg, num);
for (i=0; i<lg; i++) {
printf("%c", message[i]);
}
printf("]\n");
}
void usage() {
printf("usage: cmd [-p|-s][-u|-t] [-n ##] [-l ##]\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, "pstun:l:")) != -1) {
switch (c) {
case 'p': /* On choisit le puit. */
if (source == 1) {
usage();
exit(1);
}
source = 0;
break;
case 's': /* On choisit la source. */
if (source == 0) {
usage();
exit(1) ;
}
source = 1;
break;
case 'u': /* On choisit le mode UDP. */
if (transport == 1) {
usage();
exit(1);
}
transport = 0;
break;
case 't': /* On choisit le mode TCP */
if (transport == 0) {
usage();
exit(1);
}
transport = 1;
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:
usage();
break;
}
}
if (transport == -1) {
transport = 1;
}
if (source == -1) {
usage();
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, (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("échec 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, (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");
}
}
}