哪些客户端情况需要bind()?
我正在学习C套接字编程。什么时候会在客户端使用bind()?哪些类型的程序需要它以及为什么?我在哪里可以找到示例?
I'm learning C socket programming. When would you use bind() on the client-side? What types of program will need it and why? Where can I find an example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在客户端,只有当您想使用特定的客户端端口时,您才会使用bind,但这种情况很少见。通常在客户端上,您指定服务器计算机的 IP 地址和端口,操作系统将选择您要使用的端口。通常您并不关心,但在某些情况下,客户端上可能有防火墙,只允许某些端口上的传出连接。在这种情况下,您需要先绑定到特定端口,然后才能尝试连接。
On the client side, you would only use bind if you want to use a specific client-side port, which is rare. Usually on the client, you specify the IP address and port of the server machine, and the OS will pick which port you will use. Generally you don't care, but in some cases, there may be a firewall on the client that only allows outgoing connections on certain port. In that case, you will need to bind to a specific port before the connection attempt will work.
一个示例是活动 FTP 连接的数据连接。在这种情况下,服务器从其端口 20 连接到 PORT 或 EPRT 命令指定的 IP 和端口。
An example would be the data connection of an active FTP connection. In this case, the server connects from its port 20 to the IP and port specified by a PORT or EPRT command.
使用
bind()
的客户端程序的一个典型示例是(已过时的)rlogin
/rsh
系列网络客户端。这些客户端旨在在具有强信任关系的网络中使用 - 在某些情况下,服务器计算机信任客户端计算机来告诉它正在连接的用户的用户名。这要求客户端程序从低端口(小于 1024 的端口)进行连接,因为此类端口仅限于 root 用户,从而(理论上)证明正在使用的客户端已被授权系统管理员。NFS 协议具有类似的信任关系,同样客户端使用
bind()
从低端口号建立连接。另一个例子是 IRC 客户端,它允许用户指定要连接的特定源 IP 地址。这是为了容纳分配给其计算机的多个 IP 地址的用户,每个 IP 地址都分配有不同的“虚荣”域名。选择要连接的 IP(使用
bind()
)允许用户选择在 IRC 上显示的域名。A classic example of a client program using
bind()
is the (obsolete)rlogin
/rsh
family of network clients. These clients were intended to be used within networks with strong trust relationships - in some cases the server machine trusts the client machine to tell it the username of the user that is connecting. This required that the client program connect from a low port (a port less than 1024), because such ports are restricted to theroot
user and thus (in theory) prove that the client being used is authorised by the system administrator.The NFS protocol has similar trust relationships, and similarly the client makes connections from a low port number, using
bind()
.Another example is IRC clients that allow the user to specify a particular source IP address to connect from. This is to accomodate users with many IP addresses assigned to their machine, each with a different "vanity" domain name assigned to it. Choosing which IP to connect from (with
bind()
) allows the user to choose which domain name to appear as on IRC.一种好的情况是在 p2p 情况下,您正在使用绑定的套接字与 STUN 服务器进行通信,并且 STUN 服务器告诉您他正在从您的套接字接收消息的端口(该端口可能与您指定的端口不同)当您根据您的网络,更具体地说,根据您的 NAT 类型绑定套接字时)。这将使您了解 NAT 正在执行的真实端口转换,并且您将能够将此信息提供给想要连接到您的潜在对等点。
绑定套接字很有用,因为某些 NAT 会动态地为您提供端口(在端口 x 上绑定两次可能不会为您提供相同的“真实”端口)。因此,您将能够直接使用绑定在端口上侦听的套接字。
A good situation would be in a p2p case, you’re communicating with a STUN Server with a bound socket, and the STUN Server tells you the port on which he is receiving messages from your socket (that can be different from the one you specified when you bound your socket depending on your network and more specifically on your NAT type). This will allow you to be aware of the real port translation that your NAT is doing, and you’ll be able to give this information to potential peers that want to connect to you.
Binding the socket is useful as some NATs are dynamically giving you ports (binding on port x twice might not give you the same “real” port). So you’ll be able to directly use the socket you bound to listen on the port.
我想你应该在 UDP 套接字的情况下使用 bind() 。
I suppose you should bind() in the case of UDP sockets.
bind函数是“关键”函数之一。它将您的套接字(服务器或客户端)与地址(ip + 端口)关联起来。对于 Windows,您必须对 WinSockets 使用 bind。有一本关于它的好书“Microsoft Windows 的网络编程”,作者是 Anthony Jones 和 Jim Ohlund。
bind function is one of "key" functions. It associates your socket (server or client) with address (ip + port). As for Windows you must use bind for WinSockets. There is good book about it "Network Programming for Microsoft Windows" by Anthony Jones and Jim Ohlund.
Bind 可用于将名称附加到套接字。因此,假设您创建了一个使用特定 TCP 端口的软件,您需要绑定它,然后您就会知道它正在使用的 TCP 端口。
Bind can be used to attach names to a sockets. Thus, say you create a software that uses a particular TCP port, you need to bind it and then, you will know the TCP port it is using.