Thread: Sockets: IPv4 vs IPv6

  1. #1
    Registered User
    Join Date
    May 2008

    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...

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

    The code:
    	printf("Esperando conexiones por el puerto %s...\n",puerto);
    	// Manejo de conexiones, etc
    		sizeClient = sizeof(struct sockaddr_storage);
    		socketClient = accept(socketId, (struct sockaddr *)&infoClient, &sizeClient);
    		char s[INET6_ADDRSTRLEN];
    			  (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);
    And the direccionIn:
    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
    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, 08:39 AM
  5. Starting window sockets
    By _Cl0wn_ in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2003, 11:49 AM

Tags for this Thread