Compare commits
No commits in common. "fbed97383d753e33369fc10452ab7ead46c376de" and "1e73766abefa6cfec8f975b19bc42d9d33e31230" have entirely different histories.
fbed97383d
...
1e73766abe
2 changed files with 3 additions and 315 deletions
|
|
@ -26,7 +26,7 @@ TODO help output
|
||||||
|
|
||||||
## Roadmap
|
## Roadmap
|
||||||
|
|
||||||
- [X] tsock-v1
|
- [ ] tsock-v1
|
||||||
- [X] tsock-v2
|
- [ ] tsock-v2
|
||||||
- [WIP] tsock-v3
|
- [ ] tsock-v3
|
||||||
- [ ] tsock-v4
|
- [ ] tsock-v4
|
||||||
|
|
|
||||||
312
tsock_v3.c
312
tsock_v3.c
|
|
@ -1,312 +0,0 @@
|
||||||
/* 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
|
|
||||||
données du réseau */
|
|
||||||
#include <netdb.h>
|
|
||||||
/* pour les entrées/sorties */
|
|
||||||
#include <stdio.h>
|
|
||||||
/* pour la gestion des erreurs */
|
|
||||||
#include <errno.h>
|
|
||||||
#define BASE_SIZE 10
|
|
||||||
|
|
||||||
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;
|
|
||||||
printf("message reçu : ");
|
|
||||||
for (i=0;i<lg;i++){
|
|
||||||
printf("%c", message[i]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int main (int argc, char **argv)
|
|
||||||
{
|
|
||||||
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 */
|
|
||||||
int lg=-1;
|
|
||||||
|
|
||||||
while ((c = getopt(argc, argv, "pn:sul:")) != -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;
|
|
||||||
|
|
||||||
case 'n':
|
|
||||||
nb_message = atoi(optarg);
|
|
||||||
//Indice packet limite to 5 characters
|
|
||||||
if(n >= 100000) {
|
|
||||||
printf("Too many packets");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'u':
|
|
||||||
udp=1;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'l':
|
|
||||||
lg=atoi(optarg);
|
|
||||||
// A TCP packet is roughly 1500 bytes. We allow for some overhead.
|
|
||||||
// We provide an MTU of 1400 bytes.
|
|
||||||
if(lg >= 1400) {
|
|
||||||
printf("Messages too long");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
printf("usage: cmd [-p|-s][-n ##]\n");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
char* nom_machine_distante;
|
|
||||||
|
|
||||||
// Recuperation du port
|
|
||||||
int port=atoi(argv[argc-1]);
|
|
||||||
port = htons(port);
|
|
||||||
|
|
||||||
|
|
||||||
if (lg == -1) {
|
|
||||||
lg=30;
|
|
||||||
}
|
|
||||||
|
|
||||||
char* message = malloc(lg * sizeof(char));
|
|
||||||
|
|
||||||
|
|
||||||
if (source == -1) {
|
|
||||||
printf("usage: cmd [-p|-s][-n ##]\n");
|
|
||||||
exit(1) ;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (source == 1) {
|
|
||||||
//Recuperation du nom logique
|
|
||||||
nom_machine_distante=argv[argc-2];
|
|
||||||
printf("SOURCE:lg_mesg_emis=%d,port=%d,nb_envois=%d,TP=%s,dest=%s\n",
|
|
||||||
);
|
|
||||||
} else printf("PUITS:");
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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 = lg;
|
|
||||||
|
|
||||||
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), lg);
|
|
||||||
|
|
||||||
// Envoi du message
|
|
||||||
|
|
||||||
sendto(sock,message,longueur_message,0,(struct sockaddr*)&adr_dest,longueur_adr_dest);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
|
||||||
|
|
||||||
|
|
||||||
// Creation de l'adresse du socket distant
|
|
||||||
struct sockaddr_in adr_locale;
|
|
||||||
int longueur_adr_locale = sizeof(adr_locale);
|
|
||||||
|
|
||||||
int longueur_message = lg;
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
else{
|
|
||||||
|
|
||||||
// Creation du socket local
|
|
||||||
int sock= socket(AF_INET,SOCK_STREAM,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 = lg;
|
|
||||||
|
|
||||||
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 ) ;
|
|
||||||
|
|
||||||
// Demande de connexion
|
|
||||||
int succ;
|
|
||||||
if ((succ=connect(sock,(struct sockaddr*) &adr_dest,longueur_adr_dest))!=0){
|
|
||||||
printf("Echec connect");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Envoie des messages
|
|
||||||
for (int i = 0; i < nb_message; i++) {
|
|
||||||
// Construction du message
|
|
||||||
construire_message(message, 'a' + (i % 26), lg);
|
|
||||||
|
|
||||||
// Envoi du message
|
|
||||||
write(sock,message,longueur_message);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close socket to avoid dangling connections
|
|
||||||
close(sock);
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
|
||||||
|
|
||||||
|
|
||||||
// Creation de l'adresse du socket local
|
|
||||||
struct sockaddr_in adr_locale;
|
|
||||||
socklen_t longueur_adr_locale = sizeof(adr_locale);
|
|
||||||
|
|
||||||
int longueur_message = lg;
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
// set linstening queue size
|
|
||||||
if (listen(sock, 5) == -1) {
|
|
||||||
printf("échec et mat listen\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
int sock_bis;
|
|
||||||
// Accept a single connection on the main thread
|
|
||||||
if ((sock_bis = accept( sock, (struct sockaddr *)&adr_locale, &longueur_adr_locale)) == -1){
|
|
||||||
printf("échec du accept\n") ;
|
|
||||||
exit(1) ;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (read(sock_bis, message, longueur_message) > 0) {
|
|
||||||
afficher_message(message, longueur_message);
|
|
||||||
}
|
|
||||||
close(sock_bis);
|
|
||||||
close(sock);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
Loading…
Reference in a new issue