Merge pull request #2 from yoboujon/yohan

Yohan
This commit is contained in:
yoboujon 2023-01-11 18:04:18 +01:00 committed by GitHub
commit 4fc41b4d9d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 98 additions and 9 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);
}
}

5
puit.c
View file

@ -24,7 +24,6 @@ int main(void)
creationSocket(&sock,&socketClient); creationSocket(&sock,&socketClient);
initSocketAddr(&socketClient,0); initSocketAddr(&socketClient,0);
if (bind(sock, (const struct sockaddr *)&socketClient, sizeof(socketClient)) < 0 ) if (bind(sock, (const struct sockaddr *)&socketClient, sizeof(socketClient)) < 0 )
{ {
perror("bind failed"); perror("bind failed");
@ -32,9 +31,9 @@ int main(void)
} }
int n; int n;
unsigned int lg_socketClient = sizeof(socketClient); unsigned int lg_socketServer = sizeof(socketServer);
n = recvfrom(sock, (char *)buffer, 1024, 0, (struct sockaddr*) &socketClient,&lg_socketClient); n = recvfrom(sock, (char *)buffer, 1024, 0, (struct sockaddr*) &socketServer,&lg_socketServer);
buffer[n] = '\0'; buffer[n] = '\0';
printf("Server : %s\n", buffer); printf("Server : %s\n", buffer);
close(sock); close(sock);

BIN
source Executable file

Binary file not shown.

View file

@ -15,7 +15,7 @@ void initSocket();
int sock; int sock;
int lg_emis; int lg_emis;
struct hostent *hp; struct hostent *hp;
struct sockaddr_in socketServer; struct sockaddr_in socketClient;
int main(void) int main(void)
{ {
@ -24,9 +24,9 @@ int main(void)
printf("échec création du socket\n"); printf("échec création du socket\n");
exit(1); exit(1);
} }
memset(&socketServer, 0, sizeof(socketServer)); memset(&socketClient, 0, sizeof(socketClient));
initSocket(); 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); printf("lgemis = %d\n",lg_emis);
close(sock); close(sock);
return 0; return 0;
@ -34,12 +34,12 @@ int main(void)
void initSocket() void initSocket()
{ {
socketServer.sin_family=AF_INET; socketClient.sin_family=AF_INET;
socketServer.sin_port=htons(PORT_NUM); socketClient.sin_port=htons(PORT_NUM);
if((hp = gethostbyname("localhost")) == NULL) if((hp = gethostbyname("localhost")) == NULL)
{ {
printf("erreur gethostbyname\n"); printf("erreur gethostbyname\n");
exit(1); 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