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 ...
- 05-08-2009 #1
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
- 05-09-2009 #2Linux Guru
- 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!
- 05-09-2009 #3Linux Guru
- 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!
- 05-09-2009 #4
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
- 05-09-2009 #5
- 05-09-2009 #6Linux Guru
- 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
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!
- 05-09-2009 #7
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


Reply With Quote
