问题
In socket programming what does
servaddr.sin_addr.s_addr=INADDR_ANY;
in a client program do actually? What does INADDR_ANY mean?
Also in another source found in the internet, I found
servaddr.sin_addr.s_addr=inet_addr(argv[1]);
What is this step doing? I think I have to enter the server ip as argument in that client wants to get connected to. Am I correct?
回答1:
On a server, INADDR_ANY is an argument to bind that tells the socket to listen on all available interfaces.
On a client, it's an argument to connect that tells the client which server to connect to but would appear to be meaningless other than as an alternative way of specifying "this host".
(In both cases it's actually a pointer to a struct sockaddr_in that's actually passed, and the IP adderss is the sin_addr.s_addr field of that structure. The formal function specification actually specifies a struct sockaddr *)
来源:https://stackoverflow.com/questions/21367046/what-is-servaddr-sin-addr-s-addr-inaddr-any