This commit is contained in:
Billard Nathan 2023-02-07 13:34:54 +01:00
commit b604c7a65d
3 changed files with 38 additions and 21 deletions

7
.gitignore vendored Normal file
View file

@ -0,0 +1,7 @@
.DS_Store
.idea
*.log
tmp/
a.out

View file

@ -19,7 +19,7 @@ git pull
```
upload updates
git
``` sh
git add .
git commit -m "meaningfull message"

View file

@ -1,3 +1,4 @@
/*test */
/* librairie standard ... */
#include <stdlib.h>
/* pour getopt */
@ -40,6 +41,9 @@ void recvudp(int sock, char* buffer, int buf_len, void* addr, unsigned int addr_
} else if (lg_recv < buf_len) {
fprintf(stderr, "Only %i from %i characters have been reviewed.", lg_recv, buf_len);
exit(1);
} else {
buffer[lg_recv] = '\0';
printf("message revieved: %s\n", buffer);
}
}
@ -72,7 +76,8 @@ int main (int argc, char **argv)
int sock;
char* message;
struct hostent *hp;
struct sockaddr_in addr;
struct sockaddr_in addr_local;
struct sockaddr_in addr_distant;
while ((c = getopt(argc, argv, "hu:pn:s")) != -1) {
switch (c) {
case 'h':
@ -160,27 +165,32 @@ int main (int argc, char **argv)
printf("socket created\n");
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = DEFAULTPORT;
/* printf("%s\n", hostname); */
/* hp = gethostbyname(hostname); */
/* if (hp == NULL) { */
/* printf("hosname not found\n"); */
/* /\* herror("Error: "); *\/ */
/* exit(1); */
/* } */
/* printf("hostname found\n"); */
/* memcpy(&addr.sin_addr.s_addr, hp->h_addr, hp->h_length); */
addr.sin_addr.s_addr = INADDR_ANY;
memset(&addr_local, 0, sizeof(addr_local));
addr_local.sin_family = AF_INET;
addr_local.sin_port = DEFAULTPORT;
addr_local.sin_addr.s_addr = INADDR_ANY;
printf("address built\n");
if (bind(sock, (struct sockaddr*) &addr, (sizeof(addr))) < 0 ) {
if (bind(sock, (struct sockaddr*) &addr_local, (sizeof(addr_local))) < 0 ) {
perror("Error while binding socket: ");
exit(1);
}
memset(&addr_distant, 0, sizeof(addr_local));
addr_distant.sin_family = AF_INET;
addr_distant.sin_port = DEFAULTPORT;
printf("%s\n", hostname);
hp = gethostbyname(hostname);
if (hp == NULL) {
printf("hosname not found\n");
/* herror("Error: "); */
exit(1);
}
printf("hostname found\n");
memcpy(&addr_distant.sin_addr.s_addr, hp->h_addr, hp->h_length);
printf("socket bound\n");
message = malloc((MSG_LENGTH + 1) * sizeof(char));
@ -188,22 +198,22 @@ int main (int argc, char **argv)
construire_message(message, 'a', MSG_LENGTH);
if (nb_message < 0) {
while (1) {
sendudp(sock, message, MSG_LENGTH, &addr, sizeof(addr));
sendudp(sock, message, MSG_LENGTH, &addr_distant, sizeof(addr_distant));
}
} else {
for (int i = 0; i < nb_message; ++i) {
sendudp(sock, message, MSG_LENGTH, &addr, sizeof(addr));
sendudp(sock, message, MSG_LENGTH, &addr_distant, sizeof(addr_distant));
}
}
} else {
if (nb_message < 0) {
while (1) {
recvudp(sock, message, MSG_LENGTH, &addr, sizeof(addr));
recvudp(sock, message, MSG_LENGTH, &addr_distant, sizeof(addr_distant));
}
} else {
for (int i = 0; i < nb_message; ++i) {
recvudp(sock, message, MSG_LENGTH, &addr, sizeof(addr));
recvudp(sock, message, MSG_LENGTH, &addr_distant, sizeof(addr_distant));
}
}
printf("puit not implemented\n");