Création de plusieurs fichiers pour permettre un meilleur arrangement

This commit is contained in:
Yohan Boujon 2023-01-11 17:59:18 +01:00
parent 66bbb8e281
commit 3b42dc82e8
6 changed files with 96 additions and 6 deletions

51
main.c Normal file
View file

@ -0,0 +1,51 @@
#include "tsock.h"
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 */
while ((c = getopt(argc, argv, "pn:s")) != -1) {
switch (c) {
case 'p':
if (source != -1) {
printf("usage: cmd [-p|-s][-n ##]\n");
exit(1);
}
source = 0;
break;
case 's':
if (source != -1) {
printf("usage: cmd [-p|-s][-n ##]\n");
exit(1) ;
}
source = 1;
break;
case 'n':
nb_message = atoi(optarg);
break;
default:
printf("usage: cmd [-p|-s][-n ##]\n");
break;
}
}
if (source == -1) {
printf("usage: cmd [-p|-s][-n ##]\n");
exit(1);
}
setNbMessage(&nb_message,source);
printInfo(nb_message,source);
if(source)
{
printf("Source : %d\n",nb_message);
}
else
{
printf("Puit : %d\n",nb_message);
}
}

BIN
puit

Binary file not shown.

BIN
source

Binary file not shown.

View file

@ -15,7 +15,7 @@ void initSocket();
int sock;
int lg_emis;
struct hostent *hp;
struct sockaddr_in socketServer;
struct sockaddr_in socketClient;
int main(void)
{
@ -24,9 +24,9 @@ int main(void)
printf("échec création du socket\n");
exit(1);
}
memset(&socketServer, 0, sizeof(socketServer));
memset(&socketClient, 0, sizeof(socketClient));
initSocket();
lg_emis=sendto(sock, "aaaaa", 6, 0, (struct sockaddr*)&socketServer, sizeof(socketServer));
lg_emis=sendto(sock, "aaaaa", 6, 0, (struct sockaddr*)&socketClient, sizeof(socketClient));
printf("lgemis = %d\n",lg_emis);
close(sock);
return 0;
@ -34,12 +34,12 @@ int main(void)
void initSocket()
{
socketServer.sin_family=AF_INET;
socketServer.sin_port=htons(PORT_NUM);
socketClient.sin_family=AF_INET;
socketClient.sin_port=htons(PORT_NUM);
if((hp = gethostbyname("localhost")) == NULL)
{
printf("erreur gethostbyname\n");
exit(1);
}
memcpy((char*)&(socketServer.sin_addr.s_addr),hp->h_addr_list[0],hp->h_length);
memcpy((char*)&(socketClient.sin_addr.s_addr),hp->h_addr_list[0],hp->h_length);
}

22
tsock.c Normal file
View file

@ -0,0 +1,22 @@
#include "tsock.h"
void setNbMessage(int * nb, int source)
{
if((*nb == -1) && (source))
{
*nb = 10;
}
}
void printInfo(int nb, int source)
{
if(source)
{
printf("tsock lance en mode source, nombre de tampons à envoyer : %d\n", nb);
}
else
{
printf("tsock lance en mode puit, nombre de tampons à recevoir :");
nb != -1 ? printf("%d\n", nb) : printf("infini\n");
}
}

17
tsock.h Normal file
View file

@ -0,0 +1,17 @@
#ifndef HEADER_TSOCK
#define HEADER_TSOCK
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <errno.h>
void setNbMessage(int * nb, int source);
void printInfo(int nb, int source);
#endif