Merge branch 'master' of https://git.etud.insa-toulouse.fr/nbillard/tsock_Abderrahman_Nathan
This commit is contained in:
commit
af1427a6cd
1 changed files with 88 additions and 43 deletions
|
@ -21,9 +21,11 @@ données du réseau */
|
||||||
|
|
||||||
#define DEFAULTPORT 9000
|
#define DEFAULTPORT 9000
|
||||||
#define MSG_LENGTH 10
|
#define MSG_LENGTH 10
|
||||||
|
#define NB_CLIENTS 1
|
||||||
|
#define TCP_BUFFER_LEN 2000
|
||||||
|
|
||||||
void usage(char* prog) {
|
void usage(char* prog) {
|
||||||
printf("usage: %s [-p|-s][-u][-n ##]\n", prog);
|
printf("usage: %s [-p|-s][-u|-t][-n ##]\n", prog);
|
||||||
}
|
}
|
||||||
|
|
||||||
void construire_message(char* message, char motif, int lg) {
|
void construire_message(char* message, char motif, int lg) {
|
||||||
|
@ -59,6 +61,19 @@ void sendudp(int sock, char* message, int msg_len, void* addr, unsigned int addr
|
||||||
printf("message sent: %s\n", message);
|
printf("message sent: %s\n", message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void recvtcp(int sock) {
|
||||||
|
char buffer[TCP_BUFFER_LEN];
|
||||||
|
size_t nb_octed_read = read(sock, buffer, TCP_BUFFER_LEN - 1);
|
||||||
|
if (nb_octed_read < 0) {
|
||||||
|
perror("Could not read octed-stream: ");
|
||||||
|
exit(1);
|
||||||
|
} else {
|
||||||
|
buffer[nb_octed_read] = '\0';
|
||||||
|
printf("message recieved: %s\n", buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void afficher_message(char* message, int lg) {
|
void afficher_message(char* message, int lg) {
|
||||||
message[lg] = '\0';
|
message[lg] = '\0';
|
||||||
printf("message construit: %s", message);
|
printf("message construit: %s", message);
|
||||||
|
@ -72,13 +87,14 @@ int main (int argc, char **argv)
|
||||||
int nb_message = -1; /* Nb de messages à envoyer ou à recevoir, par défaut : 10 en émission, infini en réception */
|
int nb_message = -1; /* Nb de messages à envoyer ou à recevoir, par défaut : 10 en émission, infini en réception */
|
||||||
int source = -1 ; /* 0=puits, 1=source */
|
int source = -1 ; /* 0=puits, 1=source */
|
||||||
char* hostname;
|
char* hostname;
|
||||||
int protocole = 0; /* 0=tcp, 1=udp */
|
int protocole = -1; /* 0=tcp, 1=udp */
|
||||||
int sock;
|
int sock;
|
||||||
char* message;
|
char* message;
|
||||||
struct hostent *hp;
|
struct hostent *hp;
|
||||||
struct sockaddr_in addr_local;
|
struct sockaddr_in addr_local;
|
||||||
struct sockaddr_in addr_distant;
|
struct sockaddr_in addr_distant;
|
||||||
while ((c = getopt(argc, argv, "hu:pn:s")) != -1) {
|
unsigned int addr_distant_len = sizeof(addr_distant);
|
||||||
|
while ((c = getopt(argc, argv, "ht:u:pn:s")) != -1) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'h':
|
case 'h':
|
||||||
usage(argv[0]);
|
usage(argv[0]);
|
||||||
|
@ -106,13 +122,20 @@ int main (int argc, char **argv)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'u':
|
case 'u':
|
||||||
|
if (!protocole) {
|
||||||
|
usage(argv[0]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
protocole = 1;
|
protocole = 1;
|
||||||
printf("%s\n", optarg);
|
|
||||||
hostname = optarg;
|
hostname = optarg;
|
||||||
/* hostname = malloc((strlen(optarg) + 1) * sizeof(char)); */
|
|
||||||
/* strcpy(hostname, optarg); */
|
|
||||||
break;
|
break;
|
||||||
|
case 't':
|
||||||
|
if (protocole) {
|
||||||
|
usage(argv[0]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
protocole = 0;
|
||||||
|
hostname = optarg;
|
||||||
default:
|
default:
|
||||||
usage(argv[0]);
|
usage(argv[0]);
|
||||||
/* printf("usage: cmd [-p|-s][-n ##]\n"); */
|
/* printf("usage: cmd [-p|-s][-n ##]\n"); */
|
||||||
|
@ -120,6 +143,10 @@ int main (int argc, char **argv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (protocole == -1) {
|
||||||
|
protocole = 1;
|
||||||
|
}
|
||||||
|
|
||||||
if (source == -1) {
|
if (source == -1) {
|
||||||
usage(argv[0]);
|
usage(argv[0]);
|
||||||
/* printf("usage: cmd [-p|-s][-n ##]\n"); */
|
/* printf("usage: cmd [-p|-s][-n ##]\n"); */
|
||||||
|
@ -176,7 +203,9 @@ int main (int argc, char **argv)
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printf("socket bound\n");
|
||||||
|
|
||||||
|
if (!protocole) /* if udp activated */ {
|
||||||
memset(&addr_distant, 0, sizeof(addr_local));
|
memset(&addr_distant, 0, sizeof(addr_local));
|
||||||
addr_distant.sin_family = AF_INET;
|
addr_distant.sin_family = AF_INET;
|
||||||
addr_distant.sin_port = DEFAULTPORT;
|
addr_distant.sin_port = DEFAULTPORT;
|
||||||
|
@ -190,21 +219,36 @@ int main (int argc, char **argv)
|
||||||
printf("hostname found\n");
|
printf("hostname found\n");
|
||||||
|
|
||||||
memcpy(&addr_distant.sin_addr.s_addr, hp->h_addr, hp->h_length);
|
memcpy(&addr_distant.sin_addr.s_addr, hp->h_addr, hp->h_length);
|
||||||
|
} else /* tcp activated */{
|
||||||
|
|
||||||
|
if (listen(sock, NB_CLIENTS) < 0) {
|
||||||
|
perror("Server could not listen: ");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
if (accept(sock, (struct sockaddr*) &addr_distant, &addr_distant_len) < 0) {
|
||||||
|
perror("Server could not accept connection: ");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
printf("socket bound\n");
|
|
||||||
|
|
||||||
message = malloc((MSG_LENGTH + 1) * sizeof(char));
|
message = malloc((MSG_LENGTH + 1) * sizeof(char));
|
||||||
|
if (protocole) /* tcp activated */ {
|
||||||
|
if (source) {
|
||||||
|
;
|
||||||
|
} else /*puit */ {
|
||||||
|
recvtcp(sock);
|
||||||
|
}
|
||||||
|
} else /* udp activated */ {
|
||||||
if (source) {
|
if (source) {
|
||||||
construire_message(message, 'a', MSG_LENGTH);
|
construire_message(message, 'a', MSG_LENGTH);
|
||||||
if (nb_message < 0) {
|
if (nb_message < 0) {
|
||||||
while (1) {
|
nb_message = 10;
|
||||||
sendudp(sock, message, MSG_LENGTH, &addr_distant, sizeof(addr_distant));
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
for (int i = 0; i < nb_message; ++i) {
|
for (int i = 0; i < nb_message; ++i) {
|
||||||
sendudp(sock, message, MSG_LENGTH, &addr_distant, sizeof(addr_distant));
|
sendudp(sock, message, MSG_LENGTH, &addr_distant, sizeof(addr_distant));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if (nb_message < 0) {
|
if (nb_message < 0) {
|
||||||
|
@ -218,6 +262,7 @@ int main (int argc, char **argv)
|
||||||
}
|
}
|
||||||
printf("puit not implemented\n");
|
printf("puit not implemented\n");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if (close(sock) < 0) {
|
if (close(sock) < 0) {
|
Loading…
Reference in a new issue