SOCKET bind INADDR_LOOPBACK和INADDR_ANY的区别

int sd;
struct sockaddr_in serv_addr;

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);

serv_addr.sin_addr.s_addr        = htonl(INADDR_LOOPBACK); 
else
serv_addr.sin_addr.s_addr        = htonl(INADDR_ANY  ); 

sd = socket(AF_INET, SOCK_STREAM, 0);
bind(sd, (struct sockaddr *) &serv_addr, sizeof (serv_addr));

两者的区别
INADDR_ANY是ANY,是绑定地址0.0.0.0上的监听, 能收到任意一块网卡的连接;
INADDR_LOOPBACK, 也就是绑定地址LOOPBAC, 往往是127.0.0.1, 只能收到127.0.0.1上面的连接请求

INADDR_LOOPBACK情况:
SOCKET bind INADDR_LOOPBACK和INADDR_ANY的区别_第1张图片

INADDR_ANY情况:
SOCKET bind INADDR_LOOPBACK和INADDR_ANY的区别_第2张图片

telnet退出时 ‘ctrl+]’再输入 quit

你可能感兴趣的:(网络编程)