274 строки
6,3 КиБ
C
274 строки
6,3 КиБ
C
/*test */
|
|
/* librairie standard ... */
|
|
#include <stdlib.h>
|
|
/* pour getopt */
|
|
#include <unistd.h>
|
|
/* déclaration des types de base */
|
|
#include <sys/types.h>
|
|
/* constantes relatives aux domaines, types et protocoles */
|
|
#include <sys/socket.h>
|
|
/* constantes et structures propres au domaine UNIX */
|
|
#include <sys/un.h>
|
|
/* constantes et structures propres au domaine INTERNET */
|
|
#include <netinet/in.h>
|
|
/* structures retournées par les fonctions de gestion de la base de
|
|
données du réseau */
|
|
#include <netdb.h>
|
|
/* pour les entrées/sorties */
|
|
#include <stdio.h>
|
|
/* pour la gestion des erreurs */
|
|
#include <errno.h>
|
|
|
|
#define DEFAULTPORT 9000
|
|
#define MSG_LENGTH 10
|
|
#define NB_CLIENTS 1
|
|
#define TCP_BUFFER_LEN 2000
|
|
|
|
void usage(char* prog) {
|
|
printf("usage: %s [-p|-s][-u|-t][-n ##]\n", prog);
|
|
}
|
|
|
|
void construire_message(char* message, char motif, int lg) {
|
|
for (int i = 0; i < lg; ++i) {
|
|
message[i] = motif;
|
|
}
|
|
message[lg] = '\0';
|
|
}
|
|
|
|
void recvudp(int sock, char* buffer, int buf_len, void* addr, unsigned int addr_len) {
|
|
int lg_recv = recvfrom(sock, buffer, buf_len, 0, (struct sockaddr*) addr, &addr_len);
|
|
if (lg_recv == -1) {
|
|
perror("Message could not be recieved, Error: ");
|
|
exit(1);
|
|
} else if (lg_recv < buf_len) {
|
|
fprintf(stderr, "Only %i from %i characters have been reviewed.", lg_recv, buf_len);
|
|
exit(1);
|
|
} else {
|
|
buffer[lg_recv] = '\0';
|
|
printf("message revieved: %s\n", buffer);
|
|
}
|
|
}
|
|
|
|
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 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) {
|
|
message[lg] = '\0';
|
|
printf("message construit: %s", message);
|
|
}
|
|
|
|
int main (int argc, char **argv)
|
|
{
|
|
int c;
|
|
extern char *optarg;
|
|
extern int optind;
|
|
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 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) {
|
|
switch (c) {
|
|
case 'h':
|
|
usage(argv[0]);
|
|
return 0;
|
|
case 'p':
|
|
if (source == 1) {
|
|
usage(argv[0]);
|
|
/* printf("usage: cmd [-p|-s][-n ##]\n"); */
|
|
exit(1);
|
|
}
|
|
source = 0;
|
|
break;
|
|
|
|
case 's':
|
|
if (source == 0) {
|
|
usage(argv[0]);
|
|
/* printf("usage: cmd [-p|-s][-n ##]\n"); */
|
|
exit(1) ;
|
|
}
|
|
source = 1;
|
|
break;
|
|
|
|
case 'n':
|
|
nb_message = atoi(optarg);
|
|
break;
|
|
|
|
case 'u':
|
|
if (!protocole) {
|
|
usage(argv[0]);
|
|
exit(1);
|
|
}
|
|
protocole = 1;
|
|
hostname = optarg;
|
|
break;
|
|
case 't':
|
|
if (protocole) {
|
|
usage(argv[0]);
|
|
exit(1);
|
|
}
|
|
protocole = 0;
|
|
hostname = optarg;
|
|
default:
|
|
usage(argv[0]);
|
|
/* printf("usage: cmd [-p|-s][-n ##]\n"); */
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (protocole == -1) {
|
|
protocole = 1;
|
|
}
|
|
|
|
if (source == -1) {
|
|
usage(argv[0]);
|
|
/* printf("usage: cmd [-p|-s][-n ##]\n"); */
|
|
exit(1) ;
|
|
}
|
|
|
|
if (source == 1)
|
|
printf("on est dans le source\n");
|
|
else
|
|
printf("on est dans le puits\n");
|
|
|
|
if (nb_message != -1) {
|
|
if (source == 1)
|
|
printf("nb de tampons à envoyer : %d\n", nb_message);
|
|
else
|
|
printf("nb de tampons à recevoir : %d\n", nb_message);
|
|
} else {
|
|
if (source == 1) {
|
|
nb_message = 10 ;
|
|
printf("nb de tampons à envoyer = 10 par défaut\n");
|
|
} else {
|
|
// changed envoyer to recevoir
|
|
printf("nb de tampons à recevoir = infini\n");
|
|
}
|
|
|
|
}
|
|
/* protocole = 0 (udp) 1(tcp) */
|
|
if (!protocole) /* if (tcp activated) */ {
|
|
printf("TCP not implemented\n");
|
|
return 0;
|
|
} else {
|
|
printf("using udp\n");
|
|
}
|
|
/* if (protocole) /\* udp *\/ { */
|
|
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
|
/* } else { */
|
|
/* sock = socket(AF_INET, SOCK_STREAM, 0); */
|
|
/* } */
|
|
if (sock < 0) {
|
|
perror("Error creating socket:");
|
|
exit(1);
|
|
}
|
|
|
|
printf("socket created\n");
|
|
|
|
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_local, (sizeof(addr_local))) < 0 ) {
|
|
perror("Error while binding socket: ");
|
|
exit(1);
|
|
}
|
|
|
|
printf("socket bound\n");
|
|
|
|
if (!protocole) /* if udp activated */ {
|
|
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);
|
|
} 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);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
message = malloc((MSG_LENGTH + 1) * sizeof(char));
|
|
if (protocole) /* tcp activated */ {
|
|
if (source) {
|
|
;
|
|
} else /*puit */ {
|
|
recvtcp(sock);
|
|
}
|
|
} else /* udp activated */ {
|
|
if (source) {
|
|
construire_message(message, 'a', MSG_LENGTH);
|
|
if (nb_message < 0) {
|
|
nb_message = 10;
|
|
}
|
|
for (int i = 0; i < nb_message; ++i) {
|
|
sendudp(sock, message, MSG_LENGTH, &addr_distant, sizeof(addr_distant));
|
|
}
|
|
|
|
} else {
|
|
if (nb_message < 0) {
|
|
while (1) {
|
|
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_distant, sizeof(addr_distant));
|
|
}
|
|
}
|
|
printf("puit not implemented\n");
|
|
}
|
|
}
|
|
|
|
|
|
if (close(sock) < 0) {
|
|
perror("Error closing socket:");
|
|
exit(1);
|
|
}
|
|
return 0;
|
|
}
|
|
|