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情况:
INADDR_ANY情况:
telnet退出时 ‘ctrl+]’再输入 quit
转载请注明原文地址: https://ju.6miu.com/read-1000133.html