IP-Adresse mit getaddrinfo()



  • Hi,

    ich möchte meinem client "localhost" übergeben, und mit getaddrinfo die ip ermitteln.

    leider schlägt das connect mit dem server fehl (dh bis zum erstellen des sockets kommt keine fehlermeldung). hab nicht besonders viel erfahrung mit c - bitte um tipps.

    vielen dank im voraus!

    die main aus meinem client:

    int main(int argc, char *argv[]){
    
    	struct addrinfo* ai,* ai_sel = NULL,* ai_head;
    	struct addrinfo hints;
    	char *hostname;
    	unsigned short port; 
    	int playing = 1; 
    	int s; 
    	pgm_name = argv[0];
    	char *pEnd;
    	int clientSocket;
    	unsigned int messageLen; 
    	char message[SENDBUFFSIZE];
    
    	/*resolve hostname to IPv4 address */
    	memset(&hints, 0, sizeof(hints));
    	hints.ai_flags = 0;
    	hints.ai_family = AF_INET; /* IPv4 only, IPv6: AF_INET6  */
    	hints.ai_socktype = SOCK_STREAM;
    	hints.ai_protocol = IPPROTO_TCP;
    	hints.ai_addrlen = 0;
    	hints.ai_addr = NULL;
    	hints.ai_canonname = NULL;
    	hints.ai_next = NULL;
    
    	/*Getting hostname and port*/
    	hostname = argv[1];
    	port = strtol(argv[2], &pEnd, 10); 
    	printf("%s %s \n","Host: ",hostname);
    	printf("%s %i \n","Port: ",port);
    
    	/*initial color sequence*/
    	colorSequence[0] = B;
     	colorSequence[1] = D;
    	colorSequence[2] = G;
    	colorSequence[3] = O;
    	colorSequence[4] = R;
    
    	/*Resolving the hostname*/
    	if((getaddrinfo(hostname,NULL, &hints, &ai)) != 0){
    		usage(); 
    	}
    	ai_head = ai;
    
    	if(ai == NULL){
    		command = "Could not resolve the hostname.";
    		usage(); 
    	}
    	ai_sel = ai;
    
    	/*TCP connection to the server*/
    	if ((clientSocket = socket(ai_sel->ai_family, ai_sel->ai_socktype, ai->ai_protocol)) < 0){
    		command = "Could not create socket.";
    		usage(); 
    	}
    
    [b]	/*connect */
    	if(connect(clientSocket, ai_sel->ai_addr, ai_sel->ai_addrlen) < 0){
    		freeaddrinfo(ai);
    		(void) close(clientSocket);
    		command = "Could not connect to the socket."; 
    		usage(); 
    	}[/b]
    
    	freeaddrinfo(ai); /* the dynamically created linked list of getaddrinfo mustbe freed */
    
    	while(playing == 1){
    		/*Sending the color Sequence to the Server*/
    
    		/*Creating the message.*/
    		char *messageSend;
    		getMessage(colorSequence, message); 
    		messageSend[0] = message[0];
    		messageSend[1] = message[1];
    		messageLen = strlen(messageSend); 
    
    		/*Sending the message to the server*/	
    		if (write(clientSocket, messageSend, messageLen) != messageLen){
    			command = "Could not send the message.";
    			usage(); 
    		}
    				printf("%s","The Message for the Server: "); 
    		print16BitMessage(messageSend[0]); 
    		printf("%s","The Message for the Server: "); 
    		print16BitMessage(messageSend[1]);
    		HandleTCPServer(clientSocket); 
    
    		/*chooseColorSequence(colorSequence);*/ 
    	}
    	close(clientSocket); 
    	return EXIT_SUCCESS;
    }
    

    die main aus meinem server:

    int main(int argc, char *argv[]){
    
    	unsigned short port; 
    	char *pEnd;
    	char *serverColorSequenceUserInput; 
    	struct sockaddr_in echoServAddr; /* Local address */
    	struct sockaddr_in echoClntAddr; /* Client address */
    	unsigned int clntLen; 
    	round = 0; 
    	clientFoundSequence = 0;
    
    	pgm_name = argv[0];
    
    	/*Reading in the Port*/
    	port = strtol(argv[optind], &pEnd, 10); 
    	printf("%s %i \n","Port: ",port);
    
    	optind++;
    	/*Reading in the color sequence*/
    	serverColorSequenceUserInput = argv[optind];
    	int i; 
    	for(i = 0; i < MAXINT; i++){
    		int color; 
    		color = checkUserInput(serverColorSequenceUserInput[i]);
    		/*printf("%s %i \n", "Color server:",color);*/
    		serverColorSequence[i]=color; 
    	}
    
    	/* Create socket for incoming connections */
    	if ((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
    		command = "Could not create socket.";
    		usage(); 
    	}
    
    	/* Construct local address structure */
    	memset(&echoServAddr, 0, sizeof(echoServAddr));   /* Zero out structure */
    	echoServAddr.sin_family = AF_INET;                /* Internet address family */
    	echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
    	echoServAddr.sin_port = htons(port);      /* Local port */
    
    	/* Bind to the local address */
    	if (bind(servSock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0){
    		command = "Could not bind socket.";
    		(void) close(servSock);
    		usage(); 
    	}
    
    	/* Mark the socket so it will listen for incoming connections */
    	if (listen(servSock, MAXPENDING) < 0){
    		command = "Could not listen to the socket.";
    		(void) close(servSock);
    		usage(); 
    	}
    
    	/* Set the size of the in-out parameter */
    	clntLen = sizeof(echoClntAddr);
    
    	/* Wait for a client to connect */
    	if ((clntSocket = accept(servSock, (struct sockaddr *) &echoClntAddr, &clntLen)) < 0){
    		command = "Could not accept the client's connection.";
    		usage(); 
    	}
    	/* clntSock is connected to a client! */
    
    	(void) signal(SIGINT, cleanup); 
    	(void) signal(SIGQUIT, cleanup); 
    	(void) signal (SIGTERM, cleanup); 
    
    	while(round < 5 && clientFoundSequence!=1){
    		round++;
    
    		printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr));
    		HandleTCPClient(clntSocket);
    
    		/*Getting the message for the Client*/
    		char sendMessage = messageForClient();
    		char *response;
    		response = &sendMessage;
    		int recvMsgSize = strlen(response); 
    
    		/* Echo message back to client */
    		if (write(clntSocket, response, recvMsgSize) != recvMsgSize){
    			command = "Could not send message to client.";
    			usage(); 
    		}
    		printf("%s", "Sent response to client."); 
    		print16BitMessage(*response); 
    	}
    
    	close(clntSocket); 
    	close(servSock); 
    	if(round == 5 && clientFoundSequence == 0){
    		printf("%s \n","Spiel verloren"); 
    		return 3; 
    	}else{
    		printf("%s %d \n","Anzahl gespielter Runden: ",round); 
    		return 0; 
    	}
    	return EXIT_SUCCESS;
    }
    


  • Da fehlt irgendwo das gethostbyname()

    Schau mal bei http://www.zotteljedi.de/socket-tipps/index.html nach.
    Da findest du Hinweise.



  • DirkB schrieb:

    Da fehlt irgendwo das gethostbyname()

    Jain. gethostbyname ist zumindest von Microsoft deprecated, und getaddrinfo soll genutzt werden.
    Zugegeben, ich nutze oft auch noch gehostbyname - weil einfach einfach einfach ist. :p

    @TE
    Im zweiten Link steht ganz unten ein Anwendungsbeispiel für getaddrinfo, hilft dir das?



  • mit hilfe von dem beispiel konnte ich die sache jetzt endlich lösen:

    http://www.logix.cz/michal/devel/various/getaddrinfo.c.xp


Anmelden zum Antworten