puits udp
This commit is contained in:
parent
1cdcedf8c1
commit
968e3c6bd1
1 changed files with 29 additions and 9 deletions
40
tsock_v0.c
40
tsock_v0.c
|
@ -43,6 +43,18 @@ void recvudp(int sock, char* buffer, int buf_len, void* addr, unsigned int addr_
|
|||
}
|
||||
}
|
||||
|
||||
void sendudp(int sock, char* message, int msg_len, void* addr, unsigned int addr_len) {
|
||||
int lg_sent = sendto(sock, message, msg_len, 0, (struct sockaddr*) addr, addr_len);
|
||||
if (lg_sent == -1) {
|
||||
perror("Message could not be sent, Error: ");
|
||||
exit(1);
|
||||
} else if (lg_sent < MSG_LENGTH) {
|
||||
fprintf(stderr, "Only %i from %i characters have been sent.", lg_sent, MSG_LENGTH);
|
||||
exit(1);
|
||||
}
|
||||
printf("message sent: %s\n", message);
|
||||
}
|
||||
|
||||
void afficher_message(char* message, int lg) {
|
||||
message[lg] = '\0';
|
||||
printf("message construit: %s", message);
|
||||
|
@ -173,18 +185,26 @@ int main (int argc, char **argv)
|
|||
message = malloc((MSG_LENGTH + 1) * sizeof(char));
|
||||
if (source) {
|
||||
construire_message(message, 'a', MSG_LENGTH);
|
||||
for (int i = 0; i < nb_message; ++i) {
|
||||
int lg_sent = sendto(sock, message, MSG_LENGTH, 0, (struct sockaddr*) &addr, sizeof(addr));
|
||||
if (lg_sent == -1) {
|
||||
perror("Message could not be sent, Error: ");
|
||||
exit(1);
|
||||
} else if (lg_sent < MSG_LENGTH) {
|
||||
fprintf(stderr, "Only %i from %i characters have been sent.", lg_sent, MSG_LENGTH);
|
||||
exit(1);
|
||||
}
|
||||
printf("message sent: %s\n", message);
|
||||
if (nb_message < 0) {
|
||||
while (1) {
|
||||
sendudp(sock, message, MSG_LENGTH, &addr, sizeof(addr));
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < nb_message; ++i) {
|
||||
sendudp(sock, message, MSG_LENGTH, &addr, sizeof(addr));
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
if (nb_message < 0) {
|
||||
while (1) {
|
||||
recvudp(sock, message, MSG_LENGTH, &addr, sizeof(addr));
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < nb_message; ++i) {
|
||||
recvudp(sock, message, MSG_LENGTH, &addr, sizeof(addr));
|
||||
}
|
||||
}
|
||||
printf("puit not implemented\n");
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue