You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.1 KiB
89 lines
2.1 KiB
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h> /* pour read(2)/write(2) */
|
|
|
|
#include <netdb.h> /* pour getaddrinfo*/
|
|
#include <string.h> /* pour memset */
|
|
|
|
#include <arpa/inet.h> /* pour inet_ntop */
|
|
|
|
#define LINE_MAX 1024 /* taille MAX en réception */
|
|
|
|
void usage() {
|
|
fprintf(stderr,"usage : client hostname port\n");
|
|
exit(1);
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
int s, ret;
|
|
|
|
struct addrinfo hints, *result;
|
|
|
|
char msg[LINE_MAX];
|
|
char response[LINE_MAX];
|
|
|
|
|
|
/* Vérification des arguments */
|
|
if(argc!=3) {
|
|
fprintf(stderr,"Erreur : Nb args !\n");
|
|
usage();
|
|
}
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
hints.ai_flags = 0;
|
|
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
|
|
hints.ai_socktype = SOCK_STREAM; /* socket TCP (flux) */
|
|
hints.ai_protocol = 0; /* Any protocol */
|
|
hints.ai_canonname = NULL;
|
|
hints.ai_addr = NULL;
|
|
hints.ai_next = NULL;
|
|
|
|
ret = getaddrinfo(argv[1], argv[2], &hints, &result);
|
|
if (ret != 0) {
|
|
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
/* Création de la socket IPv4/IPv6 */
|
|
if((s=socket(result->ai_family, result->ai_socktype,
|
|
result->ai_protocol))==-1) {
|
|
perror("socket"); exit(1);
|
|
}
|
|
|
|
/* Connexion au serveur */
|
|
if(connect(s, result->ai_addr, result->ai_addrlen)) {
|
|
perror("connect"); exit(1);
|
|
}
|
|
|
|
freeaddrinfo(result); // si result ne sert plus
|
|
|
|
// while(fgets(msg, LINE_MAX, stdin) != NULL) {
|
|
|
|
// msg[strlen(msg)-1]=0; /* retire '\n' final */
|
|
|
|
// /* Émission */
|
|
// if(send(s, "msg", strlen(msg)+1, 0) != strlen(msg)+1) {
|
|
// perror("send"); exit(1);
|
|
// }
|
|
|
|
/* Attente et lecture de la réponse */
|
|
while((ret=recv(s, response, LINE_MAX, 0))!=-1) {
|
|
if(ret==0) {
|
|
fprintf(stderr, "Déconnexion du serveur !\n");
|
|
exit(1);
|
|
}
|
|
|
|
else if(ret == -1) {
|
|
perror("recv"); exit(1);
|
|
}
|
|
puts(response);
|
|
}
|
|
|
|
if(close(s)==-1) {
|
|
perror("close"); exit(1);
|
|
}
|
|
|
|
return 0;
|
|
} |