1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
   |  
        int tcp_delay = 1;//disable nagle algorithm
	int rt;
	struct addrinfo * addrinfos;
	struct addrinfo * addrinfo;
	struct addrinfo hints;
 
	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_INET;//IP V4
	hints.ai_socktype = SOCK_STREAM;//TCP Connection
	hints.ai_flags = 0;
	hints.ai_protocol = 0;
 
	rt = getaddrinfo(host, port, &hints, &addrinfos);//DNS resolution
 
	if (rt != 0)
	{
		if (rt == EAI_SYSTEM)
		{
			perror("getaddrinfo");
		}
		else
		{
			fprintf(stderr, "error in getaddrinfo: %s\n", gai_strerror(rt));
		}
		return(PLD_NOK);
	}
 
	if ((*sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
	{
		perror("socket in request_connexion");
		return(PLD_NOK);
	}
	if (setsockopt(*sock, IPPROTO_TCP, TCP_NODELAY, (void *)&tcp_delay, sizeof(int)))
	{
		perror("setsockopt in request_connexion");
		close(*sock);
		return(PLD_NOK);
	}
 
	for(addrinfo = addrinfos; addrinfo != NULL; addrinfo = addrinfo->ai_next)
	{
		rt = connect(*sock, addrinfo->ai_addr, sizeof (struct sockaddr_in));
		if (rt == -1) perror("connect");
		else break;
	}
 
	freeaddrinfo(addrinfos);
 
	if (addrinfo == NULL)
	{
		fprintf(stderr, "Unable to connect to %s:%s\n",host,port);
		return(PLD_NOK);
	}
	return(PLD_OK); | 
Partager