LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-24-2004, 09:08 PM   #1
skywalker27182
Member
 
Registered: Nov 2003
Posts: 35

Rep: Reputation: 15
why need struct sockaddr_xx?


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?
 
Old 05-24-2004, 10:06 PM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
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.
 
Old 05-25-2004, 12:51 PM   #3
skywalker27182
Member
 
Registered: Nov 2003
Posts: 35

Original Poster
Rep: Reputation: 15
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?
 
Old 05-25-2004, 02:54 PM   #4
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
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.
 
Old 05-25-2004, 10:30 PM   #5
skywalker27182
Member
 
Registered: Nov 2003
Posts: 35

Original Poster
Rep: Reputation: 15
okkk then, thanks a bunch!
 
Old 05-25-2004, 10:58 PM   #6
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
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.)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
operator== for struct lordofring Programming 10 11-21-2005 12:09 PM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM
using struct type X as pointer in struct X. worldmagic Programming 1 10-28-2003 02:06 PM
Accessing a struct inside struct cxel91a Programming 1 09-17-2003 04:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration