working send maybe
This commit is contained in:
parent
952727251a
commit
f0b339953d
1 changed files with 18 additions and 16 deletions
|
@ -5,7 +5,7 @@
|
|||
#include <unistd.h>
|
||||
/* déclaration des types de base */
|
||||
#include <sys/types.h>
|
||||
/* constantes relatives aux domaines, types et protocoles */
|
||||
/* constantes relatives aux domaines, types et tcps */
|
||||
#include <sys/socket.h>
|
||||
/* constantes et structures propres au domaine UNIX */
|
||||
#include <sys/un.h>
|
||||
|
@ -77,14 +77,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 source = -1 ; /* 0=puits, 1=source */
|
||||
char* hostname;
|
||||
int protocole = -1; /* 0=tcp, 1=udp */
|
||||
int tcp = -1; /* 0=tcp, 1=udp */
|
||||
int sock;
|
||||
char* message;
|
||||
struct hostent *hp;
|
||||
struct sockaddr_in addr_local;
|
||||
struct sockaddr_in addr_distant;
|
||||
unsigned int addr_distant_len = sizeof(addr_distant);
|
||||
while ((c = getopt(argc, argv, "ht:u:pn:s")) != -1) {
|
||||
while ((c = getopt(argc, argv, "hpst:u:n:")) != -1) {
|
||||
switch (c) {
|
||||
case 'h':
|
||||
usage(argv[0]);
|
||||
|
@ -112,32 +112,35 @@ int main (int argc, char **argv)
|
|||
break;
|
||||
|
||||
case 'u':
|
||||
if (!protocole) {
|
||||
if (tcp == 1) {
|
||||
usage(argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
protocole = 1;
|
||||
tcp = 0;
|
||||
hostname = optarg;
|
||||
break;
|
||||
case 't':
|
||||
if (protocole) {
|
||||
if (tcp == 0) {
|
||||
usage(argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
protocole = 0;
|
||||
tcp = 1;
|
||||
hostname = optarg;
|
||||
break;
|
||||
default:
|
||||
usage(argv[0]);
|
||||
exit(1);
|
||||
/* printf("usage: cmd [-p|-s][-n ##]\n"); */
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (protocole == -1) {
|
||||
protocole = 1;
|
||||
if (tcp == -1) {
|
||||
tcp = 1;
|
||||
}
|
||||
|
||||
if (source == -1) {
|
||||
printf("outside\n");
|
||||
usage(argv[0]);
|
||||
/* printf("usage: cmd [-p|-s][-n ##]\n"); */
|
||||
exit(1) ;
|
||||
|
@ -163,7 +166,7 @@ int main (int argc, char **argv)
|
|||
}
|
||||
|
||||
}
|
||||
if (protocole) /* udp */ {
|
||||
if (tcp) /* udp */ {
|
||||
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
} else {
|
||||
sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
|
@ -188,7 +191,8 @@ int main (int argc, char **argv)
|
|||
|
||||
printf("socket bound\n");
|
||||
|
||||
if (!protocole) /* if udp activated */ {
|
||||
if (!tcp) /* if udp activated */ {
|
||||
printf("pas tcp\n");
|
||||
memset(&addr_distant, 0, sizeof(addr_local));
|
||||
addr_distant.sin_family = AF_INET;
|
||||
addr_distant.sin_port = DEFAULTPORT;
|
||||
|
@ -239,18 +243,16 @@ int main (int argc, char **argv)
|
|||
|
||||
|
||||
message = malloc((MSG_LENGTH + 1) * sizeof(char));
|
||||
if (protocole) /* tcp activated */ {
|
||||
if (tcp) /* tcp activated */ {
|
||||
if (source) {
|
||||
printf("nb de tampons a envoyer : %d\n", nb_message);
|
||||
|
||||
for(int i=0;i<nb_message;i++) {
|
||||
construire_message(message, (char)((i%26)+97), MSG_LENGTH);
|
||||
printf("sending message: \n");
|
||||
afficher_message(message, MSG_LENGTH);
|
||||
printf("\t\tsending message: \n");
|
||||
send(sock, message, MSG_LENGTH, 0);
|
||||
}
|
||||
|
||||
if (close(sock)==-1) {
|
||||
if (shutdown(sock, SHUT_RDWR)==-1) {
|
||||
perror("echec de destruction du socket: \n");
|
||||
}
|
||||
} else /*puit */ {
|
||||
|
|
Loading…
Reference in a new issue