简体   繁体   English

使用0作为端口时的C#,Socket.Bind

[英]C#, Socket.Bind when using the 0 as port

In MSDN, there is a sentence, 在MSDN中,有一句话,

"If you do not care which local port is used, you can create an IPEndPoint using 0 for the port number. In this case, the service provider will assign an available port number between 1024 and 5000" “如果您不关心使用哪个本地端口,则可以使用0创建IPEndPoint作为端口号。在这种情况下,服务提供商将分配1024到5000之间的可用端口号”

My confusion is: When the Client want to connect to server, 我的困惑是:当客户端想要连接到服务器时,

Does the client still need to know which port the server is listening? 客户端是否仍需要知道服务器正在侦听哪个端口?

Or the Client just connect to the 0 port. 或者客户端只是连接到0端口。

Thanks. 谢谢。

The client still needs to know which port the server was actually bound to, and connect to that. 客户端仍然需要知道服务器实际绑定到哪个端口,并连接到该端口。

An example of this in action is in the FTP protocol. 这个实例的一个例子是FTP协议。 Normally when using active mode, the server uses one (known) port for commands, and binds an extra dynamic port for data transfer. 通常,当使用活动模式时,服务器使用一个(已知)端口作为命令,并绑定一个额外的动态端口进行数据传输。 When a data transfer starts, the server sends the actual port in a message to the client over the command channel , and the client then connects to the (psudo-random) data port it just got informed about to get the actual file data. 当数据传输开始时,服务器通过命令通道将消息中的实际端口发送到客户端 ,然后客户端连接到刚刚获知实际文件数据的(psudo-random)数据端口。

when you connect to server ,you must know ip+port decide your application to link which process on the remoting server. 当你连接到服务器时,你必须知道ip + port决定你的应用程序链接远程服务器上的哪个进程。

socket connect like that socket连接就是这样

your :IP server:Ip < =============================================================> your : port server:port 你的:IP服务器:Ip <============================================= ==================>你的:端口服务器:端口

so in client. 所以在客户端。 you do not care which local port. 你不关心哪个本地端口。 when you connect to the server ,server had know the port . 当您连接到服务器时,服务器已知道该端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM