重新SOCKET API时对bind的发现

来源:互联网 发布:淘宝十元包邮是真货么 编辑:程序博客网 时间:2024/05/30 05:14

以前对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中好似说了,只是还看不太明白,有待再解决。

原创粉丝点击