mirror of
https://github.com/yoboujon/tsock.git
synced 2025-06-08 14:00:50 +02:00
ajout des commandes -b,-e et -r pour la BAL. Gestion de -b dans puit, ajout de la fonction initSocket.
This commit is contained in:
parent
968cc17e43
commit
e24e42a74d
5 changed files with 59 additions and 17 deletions
|
@ -3,6 +3,8 @@
|
|||
|
||||
#include "tsock.h"
|
||||
|
||||
int bal(void);
|
||||
|
||||
typedef struct{
|
||||
char *data;
|
||||
int tailleData;
|
||||
|
|
|
@ -3,7 +3,8 @@
|
|||
|
||||
#include "tsock.h"
|
||||
|
||||
int launchPuit(int nombreMessage,int tailleMessage,int isTCP,int port,char * ipAddress);
|
||||
int launchPuit(int nombreMessage,int tailleMessage,int isTCP,int port,char * ipAddress,int isBAL);
|
||||
int initSocket(int socketType, struct sockaddr_in * socketStruct, int port, char * ipAddress);
|
||||
int receiveMultipleData(int nombreMessages, int tailleMessage, int sock, struct sockaddr_in socketStruct, int isTCP);
|
||||
|
||||
#endif
|
|
@ -1,6 +1,6 @@
|
|||
#include "../header/bal.h"
|
||||
|
||||
int main(void){
|
||||
int bal(void){
|
||||
struct listeBAL boiteAuxLettres = initListeBAL();
|
||||
nouveauMessage(&boiteAuxLettres,2,1,"Renvoi moi mon pdf asap. -Simon");
|
||||
nouveauMessage(&boiteAuxLettres,2,1,"Mon anniversaire c'est demain !");
|
||||
|
|
44
src/main.c
44
src/main.c
|
@ -4,24 +4,24 @@
|
|||
|
||||
int main (int argc, char **argv)
|
||||
{
|
||||
char usageChar[30]="usage: cmd [-p|-s][-n ##]\n";
|
||||
char usageChar[100]="usage: cmd [-p|-s]|[-u][-u|-r ##|-e ##|-b][-n ##][-l ##] port ipAdress\n";
|
||||
char *ipAddress;
|
||||
extern char *optarg;
|
||||
extern int optind;
|
||||
int source = -1, nb_message = -1, c, tcp=1, port=-1, tailleMessage=30; /* Nb de messages à envoyer ou à recevoir, par défaut : 10 en émission, infini en réception */
|
||||
while ((c = getopt(argc, argv, "pn:sul:")) != -1) {
|
||||
int source = -1, nb_message = -1, c, tcp=1, port=-1, tailleMessage=30, emetteur=-1, recepteur=-1, isBAL=0;
|
||||
while ((c = getopt(argc, argv, "pn:sul:e:r:b")) != -1) {
|
||||
switch (c) {
|
||||
case 'p':
|
||||
if (source != -1) {
|
||||
printf("%s",usageChar);
|
||||
exit(1);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
source = 0;
|
||||
break;
|
||||
case 's':
|
||||
if (source != -1) {
|
||||
printf("%s",usageChar);
|
||||
exit(1) ;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
source = 1;
|
||||
break;
|
||||
|
@ -35,21 +35,49 @@ int main (int argc, char **argv)
|
|||
case 'u':
|
||||
tcp=0;
|
||||
break;
|
||||
case 'e':
|
||||
if(recepteur !=-1 || tcp !=1 || isBAL!=0 || source != -1)
|
||||
{
|
||||
printf("%s",usageChar);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
source = 1;
|
||||
emetteur = atoi(optarg);
|
||||
break;
|
||||
case 'r':
|
||||
if(emetteur!=-1 || tcp !=1 || isBAL!=0 || source != -1)
|
||||
{
|
||||
printf("%s",usageChar);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
source = 1;
|
||||
recepteur = atoi(optarg);
|
||||
break;
|
||||
case 'b':
|
||||
if(emetteur!=-1 || recepteur !=-1 || tcp !=1)
|
||||
{
|
||||
printf("%s",usageChar);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
source = 0;
|
||||
isBAL=1;
|
||||
break;
|
||||
default:
|
||||
printf("%s",usageChar);
|
||||
exit(EXIT_FAILURE);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (source == -1) {
|
||||
printf("-p|-s non present !\n");
|
||||
printf("%s",usageChar);
|
||||
exit(1);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if(argc != optind+2)
|
||||
{
|
||||
printf("ip ou port non present !\n");
|
||||
printf("%s",usageChar);
|
||||
exit(1);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if(tailleMessage == -1)
|
||||
|
@ -68,7 +96,7 @@ int main (int argc, char **argv)
|
|||
else
|
||||
{
|
||||
//printf("Puit : %d\n",nb_message);
|
||||
launchPuit(nb_message,tailleMessage,tcp,port,ipAddress);
|
||||
launchPuit(nb_message,tailleMessage,tcp,port,ipAddress,isBAL);
|
||||
}
|
||||
return(EXIT_SUCCESS);
|
||||
}
|
25
src/puit.c
25
src/puit.c
|
@ -1,25 +1,36 @@
|
|||
#include "../header/puit.h"
|
||||
|
||||
int launchPuit(int nombreMessage,int tailleMessage,int isTCP,int port,char * ipAddress)
|
||||
int launchPuit(int nombreMessage,int tailleMessage,int isTCP,int port,char * ipAddress,int isBAL)
|
||||
{
|
||||
int sock,socketType;
|
||||
struct sockaddr_in socketPuit;
|
||||
socketType = (isTCP) ? SOCK_STREAM : SOCK_DGRAM;
|
||||
sock = initSocket(socketType,&socketPuit,port,ipAddress);
|
||||
if(isBAL)
|
||||
{
|
||||
printf("Mode Boîte aux Lettres\n");
|
||||
return 0;
|
||||
}
|
||||
receiveMultipleData(nombreMessage,tailleMessage,sock,socketPuit,isTCP);
|
||||
close(sock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if((sock=socket(AF_INET,socketType,0)) == -1)
|
||||
int initSocket(int socketType, struct sockaddr_in * socketStruct, int port, char * ipAddress)
|
||||
{
|
||||
int sockReturn;
|
||||
if((sockReturn=socket(AF_INET,socketType,0)) == -1)
|
||||
{
|
||||
perror("[tsock] : fonction socket() : echec creation du socket\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
initStructSocket(&socketPuit,0,port,ipAddress);
|
||||
if (bind(sock, (struct sockaddr *)&socketPuit, sizeof(socketPuit)) < 0 )
|
||||
initStructSocket(socketStruct,0,port,ipAddress);
|
||||
if (bind(sockReturn, (struct sockaddr *)socketStruct, sizeof(*socketStruct)) < 0 )
|
||||
{
|
||||
perror("[tsock] : fonction bind() : echec du lien avec socket serveur.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
receiveMultipleData(nombreMessage,tailleMessage,sock,socketPuit,isTCP);
|
||||
close(sock);
|
||||
return 0;
|
||||
return sockReturn;
|
||||
}
|
||||
|
||||
int receiveMultipleData(int nombreMessages, int tailleMessage, int sock, struct sockaddr_in socketStruct, int isTCP)
|
||||
|
|
Loading…
Add table
Reference in a new issue