Note that there are some explanatory texts on larger screens.

plurals
  1. PONon blocking python sockets
    primarykey
    data
    text
    <p>I'd like to write a small Bluetooth server application to my Nokia phone in PyS60. It needs to be able to send response to the client's request and be able to push data to the client as well.</p> <p><strong>option 1</strong>: if I use <code>socket.recv(1024)</code>, the program waits until something is received, therefore the server can't push data to the client. The Python for S60 implementation is missing the <code>socket.settimeout()</code> method, so I couldn't write a proper non-blocking code.</p> <p><strong>oprion 2</strong>: The <code>socket.makefile()</code> approach was looking good, but couldn't make it work. When I replaced the <code>conn.recv(1024)</code> to <code>fd = socket.makefile() fd.readline()</code>, it didn't read a thing.</p> <p><strong>option 3</strong>: Looked into the <code>select()</code> function, but had no luck with it. When I changed the <code>conn.recv()</code> to the <code>r,w,e = select.select([conn],[],[])</code> like it's been suggested the client doesn't even connect. It hangs at "Waiting for the client...". Strange... </p> <p>I know that there are pretty nice server implementations and asynchronous API-s as well, but I only need a really basic stuff here. Thanks in advance!</p> <p>here's what I have:</p> <pre><code>sock = btsocket.socket(btsocket.AF_BT, btsocket.SOCK_STREAM) channel = btsocket.bt_rfcomm_get_available_server_channel(sock) sock.bind(("", channel)) sock.listen(1) btsocket.bt_advertise_service(u"name", sock, True, btsocket.RFCOMM) print "Waiting for the client..." conn, client_mac = sock.accept() print "connected: " + client_mac while True: try: data = conn.recv(1024) if len(data) != 0: print "received [%s]" % data if data.startswith("something"): conn.send("something\r\n") else: conn.send("some other data \r\n") except: pass </code></pre> <p>It's obviously blocking, so the "some other data" is never sent, but it's the best I've got so far. At least I can send something in reply to the client.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload