what is servaddr.sin_addr.s_addr=INADDR_ANY [duplicate]

試著忘記壹切 提交于 2021-02-18 08:42:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!