【发布时间】: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