I have a question about socket programming.
The prototype of the accept () function is as follows:
int accept(int s, struct sockaddr *addr, socklen_t *addrlen);
What I want to know is what happens when I put null in the second and third arguments?
accept(sd, NULL, NULL);
Can it run normally without information about sockaddr
? If so, why?
When accept
returns, the addr
and addrlen
parameters are used to fill in information about the source (ie, peer). By passing in NULL
for addr
, addrlen
is ignored, and no information is returned. You don't actually need to know who the peer is to begin a network transaction, but having is helpful for logging or access control. If you don't get the peer information on accept
, but you need it later, you would have to call getpeername
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With