×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Jun 2007
    Location
    Boulder, Colorado, USA
    Posts
    27

    Reconnecting to a dropped stream

    I thought I read a post about this earlier, but I couldn't find it when I went looking today. Anyway...

    I've got an application that connects to a PDA over bluetooth and allows the phone and PDA to send and receive strings from one another. I have to assume that the PDA and phone will not always be within BlueTooth range of one another. When the two devices are disconnected (whether due to distance or an error of mine) I have to restart the software on both devices. I was curious if there were a way to check if the phone were connected to the PDA. If it is, great, send and receive strings as normal. If not, the phone should close its current socket (just to make sure it's clean) and then reinitialize the service that the phone is attempting to connect to.

    I think I need to do some form of try catch.
    Any ideas? Thanks!

    Oh yea, when there is a disconnect I get this error
    Code:
    error socket.error(32, 'Broken pipe')
    PyS60 1.4
    Symbian 3rd Edition (N73 and N80)

  2. #2
    Super Contributor
    Join Date
    May 2006
    Location
    Oulu, Finland
    Posts
    622

    Re: Reconnecting to a dropped stream

    Hi workmaster2,

    Maybe this thread could help you http://discussion.forum.nokia.com/fo...d.php?t=111603

    You need to socketObj.disconnect() before trying to reconnect.

    LFD
    Devices:
    Nokia E61 3rd Edition - pys60 1.4.0

    Tips and modules:
    http://www.lfdm.net/thesis

  3. #3
    Registered User
    Join Date
    Jun 2007
    Location
    Boulder, Colorado, USA
    Posts
    27

    Re: Reconnecting to a dropped stream

    That post looks like it will be really useful once I start writing the code to reconnect. Thanks for pointing it out!

    The first step I need to take is to see if there is already a connection active or not. Should I just run a try/except block that looks something like
    Code:
    try:
         connect()
    except:
         pass
    And just run this code every time I cycle through my other functions? Or is there a way to Asynchronously check if a connection is still active, perhaps a KError or something like that?

    Thanks for any ideas!

  4. #4
    Super Contributor
    Join Date
    May 2006
    Location
    Oulu, Finland
    Posts
    622

    Re: Reconnecting to a dropped stream

    Looking at the Python socket doc, there's connect_ex which returns an integer error code. Unfortunately it is not implemented.

    No looking at "static int KErrToErrno(TInt aError)", there's a case that looks like what we are searching for:
    Code:
      case (-11):
        i = EEXIST;
        break;
    ...
    return i;
    In the SDK doc EEXIST = 17. So if you get the error 17 in you try / except statement, bingo

    Not tested, but here are some keys for your research.

    LFD
    Devices:
    Nokia E61 3rd Edition - pys60 1.4.0

    Tips and modules:
    http://www.lfdm.net/thesis

  5. #5
    Registered User
    Join Date
    Jun 2007
    Location
    Boulder, Colorado, USA
    Posts
    27

    Re: Reconnecting to a dropped stream

    I couldn't use that socketObj.disconnect() call. It raised an error and when I tried to look up disconnect in the docs I couldn't find it. The following code snippet successfully creates a service that a PDA connects to and then ends the service. It restarts another service but the PDA crashes because the PDA cannot handle the situation where the phone closes the connection. I'll continue to test this code, but I think it is functional.

    Code:
    from socket import *
    import appuifw
    import e32
    
    def Connect():
        
        server_socket = socket (AF_BT, SOCK_STREAM)
        p = bt_rfcomm_get_available_server_channel(server_socket)
        server_socket.bind(("",p))
        print "bind done"
        server_socket.listen(1)
        bt_advertise_service("jurgen", server_socket, True, RFCOMM)
        set_security(server_socket, AUTH)
        print "I am listening"
        (sock, peer_addr) = server_socket.accept()
        print "connection from %s" %peer_addr
        sock.send("what")
        e32.ao_sleep(1)
        return sock
    
    foo = Connect()
    
    print "out of connect()"
    foo.close()
    print "sock closed"
    
    #foo.disconnect()
    print "sock disconnected"
    
    Connect()

  6. #6
    Registered User
    Join Date
    Jun 2007
    Location
    Boulder, Colorado, USA
    Posts
    27

    Re: Reconnecting to a dropped stream

    Looking at the example code from lfd's link I saw that the user had defined disconnect(). Of course there was no documentation. My bad. His method makes sense though. Will post on success of determining if stream is dropped or ended on other end.

    *********************
    Edit
    *********************

    Following the example in lfd's link works perfectly. Awesome job lfd! Thanks for all the help (this and the callStatus problem).
    Last edited by workmaster2; 2007-07-12 at 21:44.

  7. #7
    Registered User
    Join Date
    Jun 2007
    Location
    Boulder, Colorado, USA
    Posts
    27

    Re: Reconnecting to a dropped stream

    Here is a code snippet that successfully checks if a stream is lost and restarts the service if that is the case. Thanks for the help lfd!
    Code:
    class PDAManipulation:
        def __init__(self):
            self.server_socket = socket(AF_BT, SOCK_STREAM)  
            self.p = bt_rfcomm_get_available_server_channel(self.server_socket)   
            self.server_socket.bind(("", self.p)) 
            print "bind done"
            self.server_socket.listen(1)
            bt_advertise_service( u"jurgen", self.server_socket, True, RFCOMM)
            set_security(self.server_socket, AUTH)
            print "I am listening"
    
        def connect(self):
            (self.sock, self.peer_addr) = self.server_socket.accept()
            print "Connection from %s"%self.peer_addr
            
            # Allow time for data to be sent to work around a bug in the socket
            # module.
            e32.ao_sleep(1)
            print "sleep"
    
            self.sock.setblocking(0) #allows Asynchronous calls
            print "unblocked"
    
    activity = PDAManipulation()
    print "class created"
    activity.connect()
    e32.ao_sleep(2)
    print "slept"
    while 1:
        try:
            string = activity.sock.recv(1024)
        except:
            print "nothing received"
            activity.sock.close()
            activity = PDAManipulation()
            activity.connect()
            
    
        e32.ao_sleep(.5)

Similar Threads

  1. Half Duplex Audio Streaming
    By subhrajyotisaha in forum Symbian Media (Closed)
    Replies: 0
    Last Post: 2006-05-25, 04:39
  2. SIP content type
    By hlothman in forum Symbian Networking & Messaging (Closed)
    Replies: 8
    Last Post: 2006-05-17, 09:26
  3. Simple Stream in Store Problem
    By mitay2 in forum Symbian
    Replies: 1
    Last Post: 2004-12-08, 18:03
  4. Video stream to nokia 3650
    By auvinent in forum Streaming and Video
    Replies: 5
    Last Post: 2004-01-30, 08:31
  5. RDesWriteStream / RDesReadStream dropping data?
    By edelsesto in forum Symbian
    Replies: 0
    Last Post: 2003-07-12, 07:59

Posting Permissions

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