重新SOCKET API时对bind的发现

以前对SOCKET API的学习都是基于中文的资料,看MSDN也只是针对几个难点函数而也,而没有细看过其它的函数。这几天要好好重新温习一下整个SOCKET API了。

今在看到bind的时候,才发现了bind的一个功能,就是先绑定后再取得绑定的端口号。这样,在那些通信过程中要临时打一个端口进行通信,就可以方便的告知对方这边已经绑定好的端口号。做法就是在创建sockaddr_in这个结构的时候,把0赋给nport这个成员。而在以前,我以为是要一个一个端试的。原文如下:

For TCP/IP, if the port is specified as zero, the service provider assigns a unique port to the application with a value between 1024 and 5000. The application can use getsockname after calling bind to learn the address and the port that has been assigned to it. If the Internet address is equal to INADDR_ANY, getsockname cannot necessarily supply the address until the socket is connected, since several addresses can be valid if the host is multihomed. Binding to a specific port number other than port 0 is discouraged for client applications, since there is a danger of conflicting with another socket already using that port number.

 

这也解释了为什么在FTP通过程中,用到的端口号除了指定的20,21外,都是在1024到5000间的。

不过现在对于FTP是怎么用指定的端口去连接(connect)客户端还是不太清楚, 在connectex中好似说了,只是还看不太明白,有待再解决。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值