Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux: socket programming, accept(sd, null, null)

Tags:

linux

sockets

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?

like image 291
S.P Avatar asked Sep 07 '25 23:09

S.P


1 Answers

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.

like image 181
jxh Avatar answered Sep 09 '25 15:09

jxh