.NET Components for Mobility

Obex connection lost

Last post 09-24-2008 3:55 PM by Andy Hume. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 09-24-2008 5:04 AM

    Obex connection lost

    Hi,

    I use the myObexListener.GetContext() fonction which it's work very fine for my need. Except for one need : when the connection is lost during a transfert nothing happen and the getcontext() never return. Is there a way to put a timeout or a connection lost behaviour or anything else to force getcontext to return if there is a loose of connection ?

    (see server and client code below)

    Thanks

    Marc Alzieu

    sender code ( on device)  :

    if (server != null)
                    {
                        try
                        {
                            file = null;
                            // define an EndPoint for the server and connect to it

                            // define the stream
                            NetworkStream service;
                            Log("Create new End Point");
                            serverEP = new BluetoothEndPoint(server.DeviceAddress, BluetoothService.ObexObjectPush);
                            Log("Create new Client");
                            blue_client = new BluetoothClient();
                            Log("Connect Client");
                            blue_client.Connect(serverEP);
                            Log("Blue Tooth Connected : " + blue_client.Connected);
                            //serverEP.

     

                            // if I am connected ...


                            if (blue_client.Connected)
                            {
                                // receive the stream
                                service = blue_client.GetStream();
                                // connect to OBEX server
                                sessionFileSending = new ObexClientSession(service, 65535);

                                sessionFileSending.Connect();
                                Log("session Connected : ");
                                // load the file to send
                                try
                                {
                                    file = new FileStream(localPath + fileName, FileMode.Open, FileAccess.Read);

                                }
                                catch (FileNotFoundException e)
                                {
                                    Log("Fichier Absent" + e.ToString());
                                    return;
                                }
                                // send file to server
                                StartMonitoring();
                                Log("Sending File...");
                                sessionFileSending.PutFrom(file, file.Name, null);


                                Log("File Sent");
                                StopMonitoring();

                                fileSent = true;
                                // close resource
                                file.Close();
                                //sessionFileSending.Disconnect();
                                //sessionFileSending.Disconnect
                                //sessionFileSending.Dispose();
                                //blue_client.Dispose();

                            }
                            else
                            { // if I am not connected ...
                                fileSent = false;
                            }
                        }
                        catch (ObexResponseException exc)
                        {
                            // exception caused from the lacked availability of Bluetooth server
                            Log("Connection lost :" + exc.ToString());
                        }
                        catch (ObjectDisposedException exc)
                        {
                            // exception caused from the closing of the client
                            Log("Client closed: " + exc.ToString());
                        }
                        catch (System.IO.EndOfStreamException exc)
                        {
                            Log("Communication Interrupted" + exc.ToString());
                        }
                        catch (System.Net.Sockets.SocketException exc)
                        {
                          
                            Log("Unable to connect to server" + exc.ToString());
                        }
                        catch (Exception exc)
                        {
                            // generic exception caused from the other operation
                            Log("Something go wrong :" + exc.ToString());
                        }
                        finally
                        {                     
                            //blue_client.();
                            StopMonitoring();
                            Log("Disconnected");
                            if (file!=null)
                                file.Close();
                            Log("File Closed");

                            if (sessionFileSending != null)
                            {
                                try
                                {
                                    sessionFileSending.Disconnect();
                                } catch(Exception){}
                                sessionFileSending.Dispose();
                            }
                            if (blue_client !=null)
                                blue_client.Dispose();
                           
                        }
                    }

    Obex listener code (on PC) :

     ol = new ObexListener(ObexTransport.Bluetooth);

    ol.Start();

     while (ol.IsListening)
                {
                    try
                    {

                        //Session = new ObexSessionConnection();
                        Log("Attente reception");
                        //Session.Connect();
                       

                        ObexListenerContext olc = ol.GetContext();
                        Log("Recus");
                        ObexListenerRequest olr = olc.Request;
                        string filename = Uri.UnescapeDataString(olr.RawUrl.TrimStart(new char[ { '/' }));

                        WebHeaderCollection HeaderValues;
                        Log("Taille Recus : " + olr.InputStream.Length.ToString());

                        olr.WriteFile(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "\\" + DateTime.Now.ToString("yyMMddHHmmss") + " " + System.IO.Path.GetFileName(filename));

                        HeaderValues = olr.Headers;

                        Log(filename);
                        Log("Header :");
                        for (int i = 0; i <= HeaderValues.Count - 1; i++)
                        {
                            String header = HeaderValues.GetKey(i);
                            String[ values = HeaderValues.GetValues(header);
                            if (values.Length > 0)
                            {
                                Log("The values of " + header + " header are : ");
                                int j;

                                for (j = 0; j <= values.Length - 1; j++)
                                {
                                    //byte[ test=Encoding.Unicode.GetBytes(values[j]);
                                    Log(values[j].ToString());
                                }
                            }
                            else
                                Log("There is no value associated with the header");
                        }
                    }
                    catch (Exception ex)
                    {
                        break;
                    }
                }

     

  • 09-24-2008 3:55 PM In reply to

    Re: Obex connection lost

    ObexListener is a 32feet.NET class so I don't know much about it.  So does GetContext only return when the OBEX PUT operation has completed?  Hmm presumably its catching the error internally but not exposing that to you.  You could look at the sources and see if a fix is possible, probably to throw an error out of GetContext (is that how .NET's HttpListener behaves?)

    Andy 

Page 1 of 1 (2 items)
Copyright © 2001-2008 In The Hand Ltd. All rights reserved. Terms of Use and Privacy Policy.