Merge branch 'master' of https://git.etud.insa-toulouse.fr/nbillard/tsock_Abderrahman_Nathan
This commit is contained in:
commit
b604c7a65d
3 changed files with 38 additions and 21 deletions
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
.DS_Store
|
||||||
|
.idea
|
||||||
|
*.log
|
||||||
|
tmp/
|
||||||
|
|
||||||
|
|
||||||
|
a.out
|
|
@ -19,7 +19,7 @@ git pull
|
||||||
```
|
```
|
||||||
|
|
||||||
upload updates
|
upload updates
|
||||||
|
git
|
||||||
``` sh
|
``` sh
|
||||||
git add .
|
git add .
|
||||||
git commit -m "meaningfull message"
|
git commit -m "meaningfull message"
|
||||||
|
|
50
tsock_v0.c
50
tsock_v0.c
|
@ -1,3 +1,4 @@
|
||||||
|
/*test */
|
||||||
/* librairie standard ... */
|
/* librairie standard ... */
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
/* pour getopt */
|
/* 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) {
|
} else if (lg_recv < buf_len) {
|
||||||
fprintf(stderr, "Only %i from %i characters have been reviewed.", lg_recv, buf_len);
|
fprintf(stderr, "Only %i from %i characters have been reviewed.", lg_recv, buf_len);
|
||||||
exit(1);
|
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;
|
int sock;
|
||||||
char* message;
|
char* message;
|
||||||
struct hostent *hp;
|
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) {
|
while ((c = getopt(argc, argv, "hu:pn:s")) != -1) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'h':
|
case 'h':
|
||||||
|
@ -160,27 +165,32 @@ int main (int argc, char **argv)
|
||||||
|
|
||||||
printf("socket created\n");
|
printf("socket created\n");
|
||||||
|
|
||||||
memset(&addr, 0, sizeof(addr));
|
memset(&addr_local, 0, sizeof(addr_local));
|
||||||
addr.sin_family = AF_INET;
|
addr_local.sin_family = AF_INET;
|
||||||
addr.sin_port = DEFAULTPORT;
|
addr_local.sin_port = DEFAULTPORT;
|
||||||
/* printf("%s\n", hostname); */
|
addr_local.sin_addr.s_addr = INADDR_ANY;
|
||||||
/* 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;
|
|
||||||
|
|
||||||
printf("address built\n");
|
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: ");
|
perror("Error while binding socket: ");
|
||||||
exit(1);
|
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");
|
printf("socket bound\n");
|
||||||
|
|
||||||
message = malloc((MSG_LENGTH + 1) * sizeof(char));
|
message = malloc((MSG_LENGTH + 1) * sizeof(char));
|
||||||
|
@ -188,22 +198,22 @@ int main (int argc, char **argv)
|
||||||
construire_message(message, 'a', MSG_LENGTH);
|
construire_message(message, 'a', MSG_LENGTH);
|
||||||
if (nb_message < 0) {
|
if (nb_message < 0) {
|
||||||
while (1) {
|
while (1) {
|
||||||
sendudp(sock, message, MSG_LENGTH, &addr, sizeof(addr));
|
sendudp(sock, message, MSG_LENGTH, &addr_distant, sizeof(addr_distant));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (int i = 0; i < nb_message; ++i) {
|
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 {
|
} else {
|
||||||
if (nb_message < 0) {
|
if (nb_message < 0) {
|
||||||
while (1) {
|
while (1) {
|
||||||
recvudp(sock, message, MSG_LENGTH, &addr, sizeof(addr));
|
recvudp(sock, message, MSG_LENGTH, &addr_distant, sizeof(addr_distant));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (int i = 0; i < nb_message; ++i) {
|
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");
|
printf("puit not implemented\n");
|
||||||
|
|
Loading…
Reference in a new issue