This commit is contained in:
Abderrahman El-Ouali 2023-02-07 13:31:09 +02:00
commit 40a6fe5e27
2 changed files with 25 additions and 15 deletions

BIN
a.out

Binary file not shown.

View file

@ -41,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);
}
}
@ -73,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':
@ -161,9 +165,21 @@ int main (int argc, char **argv)
printf("socket created\n");
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = DEFAULTPORT;
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_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) {
@ -173,13 +189,7 @@ int main (int argc, char **argv)
}
printf("hostname found\n");
memcpy(&addr.sin_addr.s_addr, hp->h_addr, hp->h_length);
printf("address built\n");
if (bind(sock, (struct sockaddr*) &addr, (sizeof(addr))) < 0 ) {
perror("Error while binding socket: ");
exit(1);
}
memcpy(&addr_distant.sin_addr.s_addr, hp->h_addr, hp->h_length);
printf("socket bound\n");
@ -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");