You Are Here:

Community: Developer Discussion Boards

#1 Old Sockets and setblocking attribute - 2006-03-06, 20:34

Join Date: Mar 2006
Posts: 4
vgies
Offline
Registered User
Hi,

I would like some help on python sockets.
I use bluetooth sockets for communicating in an asynchronous mode. So i need to use them in non-blocking mode. I have seen this mode is supported by Python1.2, but i can't manage to use it in my project.

Does someone has some examples using socket.setblocking(0) ?
Is it possible to change the blocking status of a socket during execution of a program ?

Thanks a lot, please help me !

Valentin
Reply With Quote

#2 Old Re: Sockets and setblocking attribute - 2006-03-07, 09:23

Join Date: Nov 2005
Posts: 12
lhuovine
Offline
Registered User
Hello,

Here's a piece of code (messy) where I'm using non-blocking BT sockets. Seems to work fine. I've PyS60 1.3.1.

Code:
import socket
import thread
import e32
import appuifw
import pyS60uiutil

def service_discovery():
    try:
        addr, serv = socket.bt_discover()
        if addr == None or serv == None:
            appuifw.note(u'BlueTooth Discovery Error', 'error')
            return None
        print 'got address...'
        choises = serv.keys()
        choises.sort()
        lst = []
        for x in choises:
            lst.append(unicode(str(x)+u' #'+str(serv[x])))
            choice = appuifw.popup_menu(lst)
            if choice == None: return None
            print 'Selected service: ' + str(addr) + ' ' + \
                  str(serv[choises[choice]])
    except socket.error, detail:
        err_msg = u'BlueTooth socket error: ' + unicode(detail)
        appuifw.note(err_msg, 'error')
        return None
    return (addr, serv[choises[choice]])

def create_connection(address):
    appuifw.note(u'Creating connection', 'info')
    sock=socket.socket(socket.AF_BT,socket.SOCK_STREAM)
    try:
        sock.connect(address)
    except socket.error, err:
        if err[0]==54: # "connection refused"
            appuifw.note(u'Connection refused.','error')
            return None
        raise
    appuifw.note(u'Cool, you are connected!', 'info')
    return sock

class main:
    def __init__(self, sock):
        self.timer   = e32.Ao_timer()
        self.lock    = e32.Ao_lock()
        self.sock    = sock
        self.do_exit = False
        appuifw.app.exit_key_handler = self.myexit
        self.sock.setblocking(False)
        
    def myexit(self):
        self.do_exit = True
        self.timer.cancel()
        self.lock.signal()
        appuifw.app.exit_key_handler = None
        
    def timeout_h(self):
        rxmsg = self.sock.recv(1024)
        if rxmsg == None or rxmsg == '':
            print 'no msg'
            #pass
        else:
            appuifw.note(unicode(rxmsg), 'info')
        self.lock.signal()
    
    def run(self):
        while 1:
            self.timer.after(0.5, self.timeout_h)
            self.lock.wait()
            if self.do_exit: break
        
if __name__ == '__main__':
    ai = pyS60uiutil.save_current_app_info()
    
    addr = service_discovery()
    if not addr == None:
        sock = create_connection(addr)
        if not sock == None:
            m = main(sock)
            m.run()
            sock.close()

    pyS60uiutil.restore_app_info(ai)
Reply With Quote

#3 Old Re: Sockets and setblocking attribute - 2006-03-07, 10:03

Join Date: Mar 2006
Posts: 4
vgies
Offline
Registered User
Thanks a lot,

it works well !
Really thanks for your help !

Valentin
Reply With Quote

#4 Old Re: Sockets and setblocking attribute - 2006-03-09, 22:05

Join Date: Dec 2004
Posts: 646
jplauril's Avatar
jplauril
Offline
Forum Nokia Expert
Just one note here: restoring the menu, screen size etc. is no longer necessary, so the save_current_app_info and restore_app_info calls are basically redundant.
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia