Thread: help printing sockaddr_in info

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    17

    help printing sockaddr_in info

    I've got the following populated struct and I'm trying to print it into the normal human readable IP and port address (e.g. 192.168.0.1, and port ... 25).

    struct sockaddr_in *saddr;

    saddr->sin_port;
    saddr->sin_addr.s_addr;

    I've been trying these, but none work.

    printf("Numeric: %u\n", inet_ntohl(saddr->sin_addr.s_addr));
    printf("sin_port: %d\n", saddr->sin_port);
    printf("saddr = %d, %s: %d\n", saddr->sin_family, inet_ntoa(saddr->sin_addr), saddr->sin_port);
    printf("s_addr: %d\n", ntohl(saddr->sin_addr.s_addr));

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What doesn't work about them? I.e., are they printing complete garbage? Are they printing 0.0.0.0 or some other invalid address? Are they printing nothing at all?

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    None of them print in the format I'm looking for. A string format for ip xx.xx.xx.xx and an int for port.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    So saddr->sin_port will be stored in network byte order (big endian). I'm guessing you're using an x86 based (little endian) architecture. The port is a 2 byte number, thus, if your port number is 25, the network byte order will be 0x0019. Printing that in %d format would give you 6400 on a little endian machine. You may need to call inet_ntohs on it before printing. As for the IP address, I don't see anything wrong with your call and print statement. Try printing the values in hex with all leading zeros (%04x for port, %08x for IP). It will be easier to see if you have byte order issues in hex instead of in decimal. Also, it might help us if we had a little more code to actually see how your structures get populated and if anything may be corrupting them (e.g. buffer overflow).

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The only one of those that's even supposed to do that (for the IP address anyway) is inet_ntoa. Fortunately, that's exactly what it does. If it's not giving you the numbers you want, that's something else. But what you've got prints just fine.

    Code:
    int main(void) {
        struct sockaddr_in *saddr = malloc(sizeof(*saddr));
        inet_aton("192.168.10.45", &saddr->sin_addr);
        printf("%s\n", inet_ntoa(saddr->sin_addr));
        return 0;
    }
    prints exactly what you want.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    Quote Originally Posted by tabstop View Post
    Code:
    int main(void) {
        struct sockaddr_in *saddr = malloc(sizeof(*saddr));
        inet_aton("192.168.10.45", &saddr->sin_addr);
        printf("%s\n", inet_ntoa(saddr->sin_addr));
        return 0;
    }
    I'm having some issues troubleshooting a bug. That very code segfaults on my linux system.
    Last edited by rrlangly; 02-05-2011 at 06:00 PM.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rrlangly View Post
    I'm having some issues troubleshooting a bug. That very code segfaults on my linux system.
    Code:
    int main(void) {
        struct sockaddr_in *saddr = malloc(sizeof(*saddr));
        inet_aton("192.168.10.45", &saddr->sin_addr);
        printf("%s\n", inet_ntoa(saddr->sin_addr));
        return 0;
    }
    Lose the &

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    Quote Originally Posted by CommonTater View Post
    Lose the &
    So I dropped the & and same results. Even before it segfaulted on the printf line.

    Code:
    int main(void) {
        struct sockaddr_in *saddr = malloc(sizeof(*saddr));
    
        inet_aton("192.168.10.31", saddr->sin_addr);
    
        printf(": %s\n", inet_ntoa(&saddr->sin_addr)); // <-- segfauts on this line
    
        return 0;
    }

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Lose the & ... again.

    saddr already is a pointer.

    Try it like this....
    Code:
        struct sockaddr_in *saddr = malloc(sizeof(*saddr));
    
        saddr->sin_addr = inet_aton("192.168.10.31");
    
        printf(": %s\n", inet_ntoa(saddr->sin_addr)); // <-- segfauts on this line
    Last edited by CommonTater; 02-05-2011 at 08:44 PM.

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    That gives me the following compile error.

    Code:
    int main(void) {
        struct sockaddr_in *saddr = malloc(sizeof(*saddr));
    
        saddr->sin_addr = inet_aton("192.168.10.31");
    
        printf(": %s\n", inet_ntoa(saddr->sin_addr));
    
        return 0;
    }
    
    $ gcc -o tst tst.c
    tst.c: In function 'main':
    tst.c:12: error: incompatible types when assigning to type 'struct in_addr' from type 'int'
    tst.c:18: warning: format '%s' expects type 'char *', but argument 2 has type 'int'
    Last edited by rrlangly; 02-05-2011 at 08:57 PM.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rrlangly View Post
    That gives me the following compile error.
    Dang... now I've got a headache. (sockets can be a real pain!)

    I actually tested this one in winsock...
    Code:
    int main(void)
      {
    
        struct sockaddr_in *saddr = malloc(sizeof(*saddr));
    
        saddr->sin_addr.S_un.S_addr = inet_addr("192.168.10.31");
    
        printf("Stored address is: %s\n", inet_ntoa(saddr->sin_addr));
    
    
        return 0; }

  12. #12
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    Well, I do know that I don't have S_un on my linux box. So I can't get that working either.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rrlangly View Post
    Well, I do know that I don't have S_un on my linux box. So I can't get that working either.
    Did you even try it?

  14. #14
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    And yes, I tried it. It's unrecognized.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rrlangly View Post
    And yes, I tried it. It's unrecognized.
    Then I guess you can't do that on Linux

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. Question about getting an info class from another Form
    By Joelito in forum C# Programming
    Replies: 0
    Last Post: 10-16-2006, 01:02 PM
  3. Help doing an e-mail program in c...
    By Tyler_Durden in forum C Programming
    Replies: 88
    Last Post: 01-02-2005, 03:12 PM
  4. Printing Node Info in Linked List
    By bob2509 in forum C++ Programming
    Replies: 4
    Last Post: 11-11-2002, 02:29 PM