#include #include #include #include #include #include #include #include #include #define MAX_BUFF 100 int connexionServeur(char* host, char* service) { int sock_fd; struct addrinfo precisions, *resultat=NULL, *origine; // On stocke dans prĂ©cisions nos besoins pour le socket, dans resultat l'adresse qui respectera les requĂȘtes memset(&precisions, 0, sizeof precisions); precisions.ai_family = AF_UNSPEC; precisions.ai_socktype = SOCK_STREAM; if(getaddrinfo(host, service, &precisions, &origine)<0) { fprintf(stderr, "Erreur getaddrinfo\n"); return(-1); } int n=0; for(struct addrinfo* i=origine; i!=NULL && resultat==NULL; i=i->ai_next) { printf("%d - ", n); if(i->ai_family==AF_INET) { resultat=i; printf("Test : %d", origine->ai_addrlen); } printf("\n"); n++; } struct sockaddr_in* test=(struct sockaddr_in*)(resultat->ai_addr); printf("Addr : %x\n", test->sin_addr.s_addr); if((sock_fd=socket(resultat->ai_family, resultat->ai_socktype, resultat->ai_protocol))<0) { fprintf(stderr, "Erreur socket\n"); return(-1); } if(connect(sock_fd, resultat->ai_addr, resultat->ai_addrlen) < 0) return(-1); freeaddrinfo(origine); return sock_fd; } int main(void) { int sock_fd = connexionServeur("localhost", "2020"); if(sock_fd<0) { fprintf(stderr, "Erreur connexionServeur\n"); return(-1); } FILE* sockfd_stream = fdopen(sock_fd, "a+"); while(1) { fprintf(sockfd_stream, "YES !!! "); } shutdown(sock_fd, SHUT_RDWR); return 0; }