Find the answer to your Linux question:
Results 1 to 7 of 7
I'm reading a book called 'Unix Network Programming' and I'm having a problem with one of the lines of code(my line of code not the book's). The problem isn't a ...
  1. #1
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714

    internet connect()

    I'm reading a book called 'Unix Network Programming' and I'm having a problem with one of the lines of code(my line of code not the book's). The problem isn't a compile or running issue because the program works as intended and compiles without warning or errors. The problem is the casting of &servaddr to (const struct sockaddr*) which is shown below. Is this correct, casting a struct sockaddr_in to (const struct sockaddr*)?

    Code:
    int sockfd;
    struct sockaddr_in servaddr;
    
    
    connect(sockfd, (const struct sockaddr*)&servaddr, sizeof(servaddr))
    Make mine Arch Linux

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    This is normal usage. All the sockaddr types have a common base with struct sockaddr, so casing any of the sockaddr_xx structures to the simplest of them, struct sockaddr*, is perfectly fine. This is called down-casting, where you cast a pointer or reference from one type, to a base type. In C++ this would be like this:

    class foo {};
    class bar {};
    class foobar : public foo, public bar {};

    foobar fb;
    foo* pfoo = (foo*)&fb;

    This is perfectly legitimate, though in C++ you should REALLY use a static or dynamic cast instead of a C-style cast, but that is a subject for another day.

    So, look at the sockaddr_in as a foobar class object, and sockaddr as the foo class. A sockaddr_in structure is a TYPE of sockaddr structure, so a sockaddr_in can be cast to a sockaddr type, but not the other way around or things go boom!
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    BTW, that was a really good question, and illustrates how easy it is for people to get confused writing this sort of code. Keep it up, and invest in Douglas Comer's 3 volume series, Internetworking with TCP/IP. It covers just about everything you will want to know - the bible of TCP/IP programming for sure. Needless to say, it occupies a prominent place on my book shelf, just under my K&R and Stroustrup & Ellis books on C and C++.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  4. #4
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Thanks for the reply Rubberman, I suspected as much about this casting but couldn't find anything to verify my assumption. All I could find was the structure definitions and concluded they are the same size....Note these are the book's definitions...Gerard4143

    Code:
    struct sockaddr
    {
    	unit8_t		sa_len;
    	sa_family_t	sa_family;
    	char		sa_data[14];
    };
    
    struct sockaddr_in
    {
    	unit8_t		sin_len;
    	sa_family_t	sin_family;
    	in_port_t	sin_port;
    	struct		in_addr	sin_addr;
    	char		sin_data[8];
    };
    Make mine Arch Linux

  5. #5
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Quote Originally Posted by Rubberman View Post
    Keep it up, and invest in Douglas Comer's 3 volume series, Internetworking with TCP/IP.
    Thanks for the book tip. Its difficult to find good titles in this field...Gerard4143
    Make mine Arch Linux

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Quote Originally Posted by gerard4143 View Post
    Thanks for the book tip. Its difficult to find good titles in this field...Gerard4143
    There aren't a lot of them, but there are some really, really good ones. Comer's set is considered a "must have" for network software engineers, as are the DDN Protocol Handbook, volumes 2, 3, and 4 - the infamous White Books. I have both sets, as well a number of others, but these two sets cover everything that most engineers would ever need. FYI, the White Book(s) contain the definitive protocol RFC's for the Darpa Network. Volume 2 are the Internetworking Protocols, Volume 3 is Suppliment for Volume 2, and Volume 4 is the DNS protocol. From the protocol specifications in the White Book(s) one can write a TCP/IP protocol stack from scratch that will inter-operate with any TCP/IP client or server. I know, because I implemented the full Telnet protocol for a real-time system from the specs and not from existing code. In fact, I never looked at any existing implementation when I did that.

    So, the White Book is for the low-level engineer who needs to do a clean implementation of the protocols or is debugging existing TCP protocol handling code, while Comer is for the engineer who is writing TCP/IP programs in C. You probably don't need the White Book until and unless you end up with a job doing such low-level coding.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  7. #7
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Thanks again for the tips...I think I'll stay on the user apps side of things , at least until I finish this book - Unix Network Programming - and then maybe I'll drive into the workings of TCP/IP and networking...Gerard4143
    Make mine Arch Linux

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...