|
|
|
@ -43,12 +43,31 @@ void con_serveur(int *sfd){
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sendreceive(char* argtemp, size_t len, int j, int sfd){
|
|
|
|
|
char buf[BUF_SIZE];
|
|
|
|
|
ssize_t nread;
|
|
|
|
|
/* Send remaining command-line arguments as separate
|
|
|
|
|
datagrams, and read responses from server */
|
|
|
|
|
if (len + 1 > BUF_SIZE) {
|
|
|
|
|
fprintf(stderr, "Ignoring long message in argument %d\n", j);
|
|
|
|
|
}
|
|
|
|
|
if (write(sfd, argtemp, len) != len) {
|
|
|
|
|
fprintf(stderr, "partial/failed write\n");
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
nread = read(sfd, buf, BUF_SIZE);
|
|
|
|
|
if (nread == -1) {
|
|
|
|
|
perror("read");
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
printf("Received %ld bytes: %s\n", (long) nread, buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
struct addrinfo hints;
|
|
|
|
|
int sfd, s, j;
|
|
|
|
|
size_t len;
|
|
|
|
|
ssize_t nread;
|
|
|
|
|
char buf[BUF_SIZE];
|
|
|
|
|
char* argtemp;
|
|
|
|
|
|
|
|
|
|
if (argc < 3) { // vérification du nombre d'arguments rentrés
|
|
|
|
|
fprintf(stderr, "mettre: %s ip port message\n", argv[0]);
|
|
|
|
@ -61,10 +80,8 @@ int main(int argc, char *argv[]) {
|
|
|
|
|
|
|
|
|
|
/* Obtain address(es) matching host/port */
|
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
|
|
|
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
|
|
|
|
|
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
|
|
|
|
|
hints.ai_flags = 0;
|
|
|
|
|
hints.ai_protocol = 0; /* Any protocol */
|
|
|
|
|
hints.ai_family = AF_UNSPEC; // utilisation du serveur sur IPv4/v6 (AF_INET/AF_INET6)
|
|
|
|
|
hints.ai_socktype = SOCK_DGRAM; // socket en mode datagramme
|
|
|
|
|
|
|
|
|
|
s = cree_socket(ip, port, hints); // création du socket
|
|
|
|
|
if(s == 1) {
|
|
|
|
@ -75,26 +92,14 @@ int main(int argc, char *argv[]) {
|
|
|
|
|
|
|
|
|
|
freeaddrinfo(result); /* No longer needed */
|
|
|
|
|
|
|
|
|
|
/* Send remaining command-line arguments as separate
|
|
|
|
|
datagrams, and read responses from server */
|
|
|
|
|
|
|
|
|
|
for (j = 3; j < argc; j++) {
|
|
|
|
|
for (j = 3; j < argc; j++) { // récupère les mots passés en arguments
|
|
|
|
|
len = strlen(argv[j]) + 1;
|
|
|
|
|
argtemp = argv[j];
|
|
|
|
|
/* +1 for terminating null byte */
|
|
|
|
|
if (len + 1 > BUF_SIZE) {
|
|
|
|
|
fprintf(stderr, "Ignoring long message in argument %d\n", j);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (write(sfd, argv[j], len) != len) {
|
|
|
|
|
fprintf(stderr, "partial/failed write\n");
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
nread = read(sfd, buf, BUF_SIZE);
|
|
|
|
|
if (nread == -1) {
|
|
|
|
|
perror("read");
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
printf("Received %ld bytes: %s\n", (long) nread, buf);
|
|
|
|
|
sendreceive(argv[j], len, j, sfd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
|
}
|
|
|
|
|