python蓝牙-如何在Windows上多次使用套接字地址 [英] python Bluetooth - How to use Socket address more than once on windows

查看:42
本文介绍了python蓝牙-如何在Windows上多次使用套接字地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题已经问了好几次了,大多数答案都和 TCP/IP 有关.但是我正在寻找与蓝牙相关的 Bluetooth .

this same question has been asked several times, and most answers where related to TCP/IP. But im looking for Bluetooth related.

我正在尝试通过蓝牙在两台计算机之间发送信息.我在Linux和Windows上都安装了 pybluez ,在发现两个OS上的其他附近设备时,它都可以正常工作.后来我以此代码为例来发送信息.当客户端是linux机器而服务器是linux机器时,它工作正常.当我在Windows7上运行服务器端代码时,出现错误

Im trying to send information between 2 machines through bluetooth. I installed pybluez on both linux and windows, It worked fine on discovering other nearby devices on both Os. Later i used this code as an example to send information. It worked fine when, client was linux machine and server was linux machine. When i run the server side code on windows7 i got the error

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中使用该端口后,仅关闭地址/端口是不够的,而且还必须设置为可重复使用(

which i realized that on windows once the port is used, just closing the address/port is not sufficient but also has to be set to reuse(from SO). But there is no similar library inside bluetooth.BluetoothSocket to reuse the address/port.

我如何不止一次使用插座?还是有另一种方法,..?

How can i use a socket more than once? or is there an alternative way,..?

代码:

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(instea为1),并且不再遇到此问题.希望能帮助到你!由于某些原因,我还不太确定,在该端口上首次成功连接后,无法重新使用端口1.

I experienced the same problem as you but resolved it yesterday and thought maybe you could try the same approach as me. Try using a different port number instead of port 1. I personally used port 5 (instea of 1) and stopped having this issue. Hope it helps! For some reason I am not quite sure about yet, port 1 cannot be re-used after a first successful connection in that port.

这篇关于python蓝牙-如何在Windows上多次使用套接字地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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