简体   繁体   English

如何使用python套接字在特定端口中发送消息? 没有随机端口

[英]How to send a message in a specific port with python sockets ? No random ports

I have to test a broadcast with acknowledgement on localhost. 我必须在本地主机上测试带有确认的广播。 So I have some text files that represent the nodes and inside there is a list of neighbors. 因此,我有一些表示节点的文本文件,并且在其中有邻居列表。
I use localhost as the IP and the port is the number of the node. 我使用本地主机作为IP,端口是节点的编号。

The problem is when I receive a message (that I sent) from a node like 7000, python replaces it with a random number for example 65724. So now my father is 65724 instead of 7000, so I cannot remove 7000 from the list of neighbors. 问题是当我从类似7000的节点接收到一条消息(我发送的消息)时,python用一个随机数代替了它,例如65724。所以现在我的父亲是65724,而不是7000,所以我无法从邻居列表中删除7000 。
I cannot complete my algorithm and that is very frustrating. 我无法完成算法,这非常令人沮丧。

I can send a message with the port number that I want, but it's not very elegant. 我可以发送带有所需端口号的消息,但这不是很优雅。

Could someone tell me how to not allow python to randomize the port? 有人可以告诉我如何不允许python随机化端口吗?

rmunn saved me, the answer I was looking far is the bind before connect method. rmunn救了我,我一直在寻找的答案是连接前绑定方法。 Befor sending a message you bind your own port and you connect to the other one. 在发送消息之前,您绑定自己的端口,然后连接到另一个端口。

This is not a Python problem, per se. 本质上,这不是Python问题。 You are confused about how ports work. 您对端口的工作方式感到困惑。

Each TCP communication (sending or receiving) has two IP addresses and two ports: the host IP and host port, and the destination IP and destination port. 每个TCP通信(发送或接收)都有两个IP地址和两个端口:主机IP和主机端口,以及目标IP和目标端口。

If you're communicating with a computer "out there" on the network, your host and destination IPs will be different. 如果您要与网络上“不在那儿”的计算机进行通信,则主机IP和目标IP将有所不同。 In your test case, your host and destination IPs will both be 127.0.0.1 (localhost). 在您的测试用例中,您的主机IP和目标IP均为127.0.0.1(localhost)。 But I'm going to go with the "different IPs" case for my example, because it makes it easier to see. 但是,在我的示例中,我将使用“不同IP”的情况,因为这样可以更容易看到。

So your IP address is, say 10.1.2.3, and you're talking to a computer at 10.1.2.99. 因此,您的IP地址为10.1.2.3,并且您正在与10.1.2.99处的计算机通信。 You tell the system that you want to talk to 10.1.2.99 at port 7000, and it opens up a connection. 您告诉系统您想在端口7000上与10.1.2.99通话,它会打开一个连接。 When that happens, it will randomly pick a source port that's not in use. 发生这种情况时,它将随机选择一个未使用的源端口。 So now there's a two-way communication channel open: 因此,现在有一个双向通讯渠道:

10.1.2.3:65274 <-> 10.1.2.99:7000

Note that you did not pick that host port. 请注意,您没有挑选主机端口。 EDIT: I originally said "In fact, the system will not allow you to pick the host port; it will be assigned to you" here, but that is wrong. 编辑:我最初说“实际上,系统不允许您选择主机端口;它将分配给您”,但这是错误的。 If s is a socket object, you can call s.bind() to set its source port, then call s.connect() to connect to a destination port. 如果s是套接字对象,则可以调用s.bind()设置其源端口,然后调用s.connect()以连接到目标端口。

Now, when you're listening for a message, then you pick the port you're listening on, and the computer that's connecting to you will have a random port. 现在,当您正在收听消息时,您可以选择正在收听的端口,并且与您连接的计算机将具有一个随机端口。 So if you were listening for a message on port 8912, the incoming connection (once established) will look like: 因此,如果您正在侦听端口8912上的消息,则传入连接(一旦建立)将如下所示:

10.1.2.3:8912 <-> 10.1.2.99:38290

Note that 38290 was chosen at random by the operating system of the computer at the 10.1.2.99 IP address. 请注意,计算机的操作系统在10.1.2.99 IP地址上随机选择了38290。

Now for the bit of Python. 现在使用Python。 You mention sockets in your question title, so I'll assume you're using the socket module from Python's standard library. 您在问题标题中提到了套接字,所以我假设您正在使用Python标准库中的socket模块。 Once you've created a socket object s , use s.getpeername() to find out the address (host and port) that you've connected to, and s.getsockname() to find out the address (host and port) that you've connected from . 一旦你创建一个Socket对象s ,用s.getpeername()来找出你连接到地址(主机和端口),并且s.getsockname()找出地址(主机和端口),其您已从连接。

Since you talk about expecting the number 7000 and getting a random number, I think you're using the host socket when you should be using the destination socket. 由于您谈论的是期望数字7000并获得一个随机数,因此,我认为您应该在使用目标套接字时使用主机套接字。

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

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