分类: LINUX
2012-09-01 13:29:22
今天写程序时候,服务器端启动了,然后客户端总是连接不上,connect返回错误号是10061,服务器积极拒绝请求。
用telnet连接一下端口,发现服务端服务没有开启,但是我程序是启动的,用netstat -a 命令看服务器是监听状态。
把流程走一遍,发现bind用的参数是INADDR_LOOPBACK,改成INADDR_ANY就OK了。
只是有些困惑,因为之前一个程序用的是INADDR_LOOPBACK运行没有问题,为何这里运行不了?
sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK); // 1
sa.sin_addr.s_addr = htonl(INADDR_ANY ); //2
两者的区别
vfivept2015-12-10 14:50:27
More details from http://developerweb.net/viewtopic.php?id=5588
INADDR_ANY is 0.0.0.0... When a listening socket is bound to this address, it will
be able to accept incoming connections on all local network interfaces (including
loopback, as well as any real Ethernet or other interfaces)...
If you instead bound a listening socket to I