ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi,
I am trying to do some network programming using sockets. But I am not able to understand 2 things:
1. There structures for each set of Protocols, like 'struct sockaddr_in' and 'struct sockaddr_ns', but they are cast to 'struct sockaddr'. In that case, why is one structure not enough?
2. And the length of the address is also passed as an argument, why?
the reason is so that each protocol family can work with a generic type, simplifying the kernel networking code. basically, u have the generic sockaddr type, and then each different family (AF_INET, AF_OSI, AF_UNIX,etc) defines a specialized version of the generic sockaddr type, giving u sockaddr_in and sockaddr_ns etc.. the difference between these specialized versions is in their size, and that is the reason for the size argument. the basic point is generalizing the address structure used to define an address so that different protocol families can easily be implemented.
ok, u mean to say that the kernel executes a single code no matter what protocol we specify, but it differs its way of execution depending on the arguments we give, like the family and size?
yep u got the idea. the actual code executed will be different depending on the address family, but it allows all address families to work with teh same generic structure.
Do you do any c++ or java? Assume that sockaddr_in inherits from sockaddr
The length needs to be passed because sockaddr_in may be larger that sockaddr, and without the length the socket function wouldn't know how big the structure was. (The function you call sees the struct as a sockaddr, since that's what you casted it to. It can cast it back once it gets to a point where the code for different socket types diverges.)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.