【问题标题】:python Bluetooth - How to use Socket address more than once on windowspython蓝牙-如何在Windows上多次使用Socket地址
【发布时间】:2016-05-02 14:00:21
【问题描述】:

这个问题已经被问过好几次了,大多数答案都与TCP/IP 相关。但我正在寻找 Bluetooth 相关的。

我试图通过蓝牙在两台机器之间发送信息。我在 linux 和 windows 上都安装了pybluez,它可以很好地发现两个操作系统上的其他附近设备。后来我以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 上使用端口后,仅关闭地址/端口是不够的,还必须设置为重用(from SO)。 但是bluetooth.BluetoothSocket里面没有类似的库可以重用地址/端口。

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

代码:

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()

【问题讨论】:

  • 你能发布你与这个问题相关的代码吗?

标签: python windows sockets bluetooth


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2016-07-10
      • 1970-01-01
      • 1970-01-01
      • 2014-10-28
      • 1970-01-01
      • 1970-01-01
      • 2022-11-26
      • 2018-06-06
      • 1970-01-01
      相关资源
      最近更新 更多