created distant address

This commit is contained in:
nbillard 2023-02-07 13:23:43 +01:00
parent 7400ebf2bc
commit 0c95cb9e11
2 changed files with 26 additions and 20 deletions

BIN
a.out

Binary file not shown.

View file

@ -75,7 +75,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':
@ -163,27 +164,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));
@ -191,22 +197,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");