From 10a15c0c430d2e9bdc72ad90332c6fb8203b5b58 Mon Sep 17 00:00:00 2001 From: Lacau Clement Date: Fri, 1 Mar 2024 16:29:29 +0100 Subject: [PATCH] feat(puit): v1 sans boucle --- tsock_v1.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/tsock_v1.c b/tsock_v1.c index bbf948b..fa5cd5a 100644 --- a/tsock_v1.c +++ b/tsock_v1.c @@ -113,11 +113,11 @@ int main (int argc, char **argv) } if (udp==1){ + // Creation du socket local + int sock= socket(AF_INET,SOCK_DGRAM,0); if (source==1) { - // Creation du socket local - int sock= socket(AF_INET,SOCK_DGRAM,0); // Creation de l'adresse du socket distant @@ -154,6 +154,34 @@ int main (int argc, char **argv) else { + + // Creation de l'adresse du socket distant + struct sockaddr_in adr_locale; + int longueur_adr_locale = sizeof(adr_locale); + + int longueur_message = BASE_SIZE; + + 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); + } + + // 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); + + exit(0); + + } }