I just started to get into socket programming. I don't understand how by just declaring sockaddr_storage and then make a socket listen(), all the incoming connections just automatically get stored in sockaddr_storage. What if you declare 5 sockaddr_storage?
-
See [this answer](http://stackoverflow.com/a/8842041/841108) to a similar question – Basile Starynkevitch Aug 06 '16 at 06:48
-
2"I don't understand how by just declaring sockaddr_storage and then make a socket listen(), all the incoming connections just automatically get stored in sockaddr_storage." - ***that's because they don't***. – user253751 Aug 06 '16 at 08:34
-
1You might want to have a closer look at the arguments to [`accept()`](http://pubs.opengroup.org/onlinepubs/7908799/xns/accept.html). – dhke Aug 06 '16 at 08:54
-
Yes. Thank you. That's exactly where I should have looked into at first. Now I understand it. – Xiwen Li Aug 07 '16 at 15:36
2 Answers
How exactly does sockaddr_storage work?
sockaddr_storage
is a type that's big enough to hold a socket address for any supported protocol (sockaddr_in
for IPv4, sockaddr_in6
for IPv6, sockaddr_un
for UNIX domains, sockaddr_bth
for Bluetooth, etc).
I don't understand how by just declaring sockaddr_storage and then make a socket listen(), all the incoming connections just automatically get stored in sockaddr_storage.
That's because they don't.
You have to pass a sockaddr_storage
to a function that fills it with a socket address, like accept()
, getsockname()
, getpeername()
etc.
What if you declare 5 sockaddr_storage?
Then you have 5 variables of type sockaddr_storage
. Nothing special happens. It's the same as if you asked "what if you declare 5 ints?" You still have to populate them somehow.

- 555,201
- 31
- 458
- 770

- 57,427
- 7
- 48
- 90
-
Be careful with the limitations: Because `sockaddr_storage` needs to be [*large enough to accommodate all supported protocol-specific address structures*](http://pubs.opengroup.org/onlinepubs/009696699/basedefs/sys/socket.h.html). Which is e.g. why `sockaddr_un` limits maximum path length. – dhke Aug 06 '16 at 10:27
I'd recommend you to reed Beej's guide to network programming and Beej's Guide to Unix Interprocess Communication it's really worth it.

- 79
- 4