Sockets: IPv4 vs IPv6

This is a discussion on Sockets: IPv4 vs IPv6 within the Networking/Device Communication forums, part of the General Programming Boards category; Hello everyone in C Boards! I'm here to look again for answer or hints to help me accomplish what I ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    8

    A question about sockets

    Hello everyone in C Boards! I'm here to look again for answer or hints to help me accomplish what I need to do...

    I'm writing a server with sockets (actually, a repository of files), and I have a code (see down). The code shouldn't care about IPv4 and IPv6. When a IPv6 interface connects to the server, everything is printed fine and the IP appears ok. Anyway, when a IPv4 connect to the repository the IP appears in a IPv6 style...

    I know that they are the same address expressed in different ways, but I need it to print the IP as a IPv4 without the ::fffff: prefix...

    Example:
    Aceptada conexión de ::ffff:127.0.0.1
    should print
    Aceptada conexión de 127.0.0.1

    The code:
    Code:
    	printf("Esperando conexiones por el puerto %s...\n",puerto);
    	
    	// Manejo de conexiones, etc
    	while(1){
    		sizeClient = sizeof(struct sockaddr_storage);
    		socketClient = accept(socketId, (struct sockaddr *)&infoClient, &sizeClient);
    		if(socketClient==-1){
    			perror("aceppt");
    			continue;
    		}
    		char s[INET6_ADDRSTRLEN];
    		inet_ntop(infoClient.ss_family,
    			  (void*)direccionIn((struct sockaddr_in *)&infoClient),
    			  s, sizeof(s));
    		printf("Aceptada conexión de %s\n", s);
    		
    		// Todo el código aqui
    		// All the code here
    		
    		
    		printf("Cerrando conexión de %s\n", s);
    		close(socketClient);
    	}
    And the direccionIn:
    Code:
    void *direccionIn(struct sockaddr *sa) {
    	// Si es IPv4
    	if (sa->sa_family == AF_INET) {
    		return &(((struct sockaddr_in*)sa)->sin_addr);
    	}
    	// Si es IPv6
    	return &(((struct sockaddr_in6*)sa)->sin6_addr);
    }
    Last edited by ninboy; 10-15-2008 at 04:59 PM. Reason: More info

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    8
    I've added another question... Anyone can give me some light on this? :S

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-07-2009, 11:31 AM
  2. geting ipv4 and ipv6 interfaces with ioclt
    By wwwnuwan in forum Networking/Device Communication
    Replies: 0
    Last Post: 04-21-2009, 12:38 AM
  3. IPv6 multicast example code
    By Sang-drax in forum Networking/Device Communication
    Replies: 7
    Last Post: 07-25-2005, 09:26 AM
  4. Replies: 1
    Last Post: 04-05-2003, 07:39 AM
  5. Starting window sockets
    By _Cl0wn_ in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2003, 10:49 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21