为什么在 TCP 中使用 bind()?为什么它只用在服务器端而不用在客户端? [英] Why is bind() used in TCP? Why is it used only on server side and not in client side?

查看:18
本文介绍了为什么在 TCP 中使用 bind()?为什么它只用在服务器端而不用在客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 TCP 中 bind() 的确切功能.将本地地址绑定"到套接字是什么意思?如果它为套接字分配一个端口号,那么我们为什么不在客户端使用它呢?我知道该端口是由操作系统在客户端自动分配的,但我并没有全面了解这一切是如何工作的.

I wanted to know the exact function of bind() in TCP. What does it mean by 'binding' a local address to the socket? If it's assigning a port number to the socket, then why don't we use it in the client? I know that port is assigned by OS automatically in the client side, but I'm not getting the big picture of how all of this works.

在bind()之后,我们监听().绑定与listen() 有什么关系?listen() 会知道 bind() 已经执行了吗?如果是这样,bind() 会进行哪些更改以使其为人所知?我的意思是,成功执行返回零有什么帮助?

After bind(), we listen(). How is the bind related to listen()? Will the listen() know that bind() has been executed? If so, what changes does bind() make so that it is known? I mean, how does returning zero for successful execution help?

我已经阅读了许多定义,但我无法详细了解所有这些.因此,如果有人可以向我解释一下,我将不胜感激.

I've gone through many definitions, but no where I could get all of this in detail. So if anyone can please explain this to me, I will be grateful.

推荐答案

它分配本地"端的端口号.

It assigns the "local" end's port number.

对于服务器套接字,这是最终的方法 - 这正是我们所需要的:例如,将您的套接字绑定到 Web 服务器的端口 80.

For a server socket, this is the ultimate way to go - it is exactly what is needed: have your socket be bound to port 80 for a web server, for example.

但是,对于客户端套接字,本地地址和端口通常并不重要.所以你不要bind().如果服务器限制其客户端可能具有某个端口号,或者端口号超出给定范围,您也可以在客户端使用 bind().

For a client socket, however, the local address and port is normally not of importance. So you don't bind(). If the server restricts its clients to maybe have a certain port number, or a port number out of a given range, you can use bind() on client side as well.

另一方面,您也可以在没有调用 bind() 的套接字上 listen() (实际上我不是肯定的,但这是有道理的).在这种情况下,您的服务器端口将是随机的,并且服务器进程将通过不同的方式将其端口传递给客户端.想象一个双连接"协议,例如 FTP,其中有一个控制连接和一个数据连接.数据连接侦听的端口是完全任意的,但必须与另一端通信.所以使用自动确定的端口"进行通信.

On the other hand, you might as well be able to listen() on a socket where you haven't called bind() (actually I'm not sure about that, but it would make sense). In this scenario, your server port would be random, and the server process would communicate its port via a different means to the client. Imagine a "double-connection" protocol such as FTP, where you have a control connection and a data connection. The port the data connection listens on is completely arbitrary, but must be communicated to the other side. So the "automatically determined port" is used and communicated.

Python 中的一个例子:

One example in Python:

import socket
s = socket.socket() # create your socket
s.listen(10) # call listen without bind
s.getsockname() Which random port number did we get?
# here results in ('0.0.0.0', 61372)

s2 = socket.socket() # create client socket
s2.connect(('localhost', 61372)) # connect to the one above
s3, x = s.accept() # Python specific use; s3 is our connected socket on the server side
s2.getpeername()
# gives ('127.0.0.1', 61372)
s2.getsockname()
# gives ('127.0.0.1', 54663)
s3.getsockname()
# gives ('127.0.0.1', 61372), the same as s2.getpeername(), for symmetry
s3.getpeername()
#gives ('127.0.0.1', 54663), the same as s2.getsockname(), for symmetry
#test the connection
s2.send('hello')
print s3.recv(10)

这篇关于为什么在 TCP 中使用 bind()?为什么它只用在服务器端而不用在客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆