Thread: How do I convert this char string into an IPv6 address?

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    28

    How do I convert this char string into an IPv6 address?

    How do I convert this char string into an IPv6 address for this structure? When I try to connect() it works if myAddress = "::1",but does not work for any other IPv6 address.

    Code:
    // MAIN
    char myAddress[256] = "fd00::3";
    struct sockaddr_in6 Cserv_addr;
    
    myReturn = getMyAddr(myAddress, &Cserv_addr);
    
    // FUNCTION
    int getMyAddr(char ChostAddress[], struct sockaddr_in6 *Cserv_addr) {
        char*phostAddress = ChostAddress;
    
        server = gethostbyname2(phostAddress, AF_INET6); // This is probably the wrong function?
    
        memmove((char *) &Cserv_add->sin6_addr.s6_addr, (char *)   server->h_addr, server->h_length);
        
         return(0);
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    call something like this right you fail to connect and report back:
    Code:
    #include <error.h>
    perror("connect attempt");
    Reason is connect() has a bunch of errno codes, and calling perror() might help explain what is wrong, especially if you're sure the address should work.
    connect(2): initiate connection on socket - Linux man page

  3. #3
    Registered User
    Join Date
    Jul 2017
    Posts
    28
    Quote Originally Posted by whiteflags View Post
    call something like this right you fail to connect and report back:
    Code:
    #include <error.h>
    perror("connect attempt");
    Reason is connect() has a bunch of errno codes, and calling perror() might help explain what is wrong, especially if you're sure the address should work.
    connect(2): initiate connection on socket - Linux man page
    Right after the connect() call I used perror() and got the following message and errno:
    "connect attempt: Network is unreachable"
    errno = 101

    This is strange because I have another computer running the server script with an open socket with the correct address. That's why I am wondering, if my client script is converting the string into an IPv6 address wrong.

  4. #4
    Registered User
    Join Date
    Jul 2017
    Posts
    28
    I managed to connect. I had to set an IPv6 address for the client computer even though I was only using the IPv6 address for the server computer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expanding IPv6 address using inet_pton
    By roxynatal in forum Linux Programming
    Replies: 3
    Last Post: 12-07-2011, 04:54 AM
  2. IPv6 address increment
    By JNeha in forum C Programming
    Replies: 2
    Last Post: 11-30-2011, 11:42 AM
  3. Replies: 11
    Last Post: 06-16-2011, 11:59 AM
  4. Replies: 2
    Last Post: 09-12-2010, 09:15 AM
  5. IPv6 Address
    By sarathius in forum Networking/Device Communication
    Replies: 8
    Last Post: 03-02-2008, 05:52 AM

Tags for this Thread