python 蓝牙 - 如何在 windows 上多次使用套接字地址

python Bluetooth - How to use Socket address more than once on windows

同一个问题已被问过多次,大多数答案都与 TCP/IP 相关。但我正在寻找 Bluetooth 相关的内容。

我正在尝试通过蓝牙在两台机器之间发送信息。我在 linux 和 windows 上都安装了 pybluez,在 Os 上发现附近的其他设备时效果很好。后来我以this code为例来发信息。当客户端是 linux 机器而服务器是 linux 机器时,它工作正常。当我 运行 windows7 上的服务器端代码时,我得到了错误

server_sock.bind(("",port))
  File "C:\Python27\lib\site-packages\bluetooth\msbt.py", line 60, in bind
    status = bt.bind (self._sockfd, addr, port)
IOError: Only one usage of each socket address (protocol/network address/port) is normally permitted.

我意识到在 windows 上,一旦端口被使用,仅关闭 address/port 是不够的,还必须设置为重用(from SO)。 但是bluetooth.BluetoothSocket里面没有类似的库来重用address/port.

如何多次使用一个套接字?还是有其他方法,..?

代码:

import bluetooth

server_sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )

port = 1
server_sock.bind(("",port))
server_sock.listen(1)

client_sock,address = server_sock.accept()
print "Accepted connection from ",address

data = client_sock.recv(1024)
print "received [%s]" % data

client_sock.close()
server_sock.close()

我遇到了和你一样的问题,但昨天解决了,我想也许你可以尝试和我一样的方法。尝试使用不同的端口号而不是端口 1。我个人使用端口 5(而不是 1)并不再出现此问题。希望能帮助到你!出于某种原因,我还不太确定,端口 1 在该端口首次成功连接后无法重新使用。

重置 hciconfig,这是因为套接字已在使用中