socket in a service - problem with connect-function

kkolle

Member
Licensed User
Longtime User
Hi everybody!

I hope that my english is good enough to explain my problem.

I have an application which sends data to a socket-server within a service.
I´m using an AsyncStream-object to send the data.
Right now I´m using only the mobile network. No WiFi.

Now my problem:
Normaly the connect-function works fine. But when there is a bad mobile network coverage (the ConnectivityChanged-event in my PhoneEvents-object gives still CONNECTED) the connect-function freezes the whole app.
After a few seconds an os-message pops up: A service takes time. Try to close. Wait.
I can say "wait" and everything is fine. The app comes up again.

I hope the someone can help me out! :sign0163:

Here are some code-snippets:

B4X:
'Service module
Sub Process_Globals
   Dim AStreams As AsyncStreams   
   Dim Socket1 As Socket
   Dim OpeningSocket As Boolean
   Dim SocketHost As String
   SocketHost = "www.XXXXXXX.XX"
   Dim SocketPort As Int
   SocketPort = XXXX
End Sub

Sub connectToSocket   
   If PhoneService.myPhoneIsReady = False Then
      Return
   End If   
   OpeningSocket = True   
   Try
      Socket1.Initialize("Socket1")
      Socket1.Connect(SocketHost, SocketPort, 0)
   Catch
   
   End Try
End Sub

Sub Socket1_Connected (Successful As Boolean)
   Try      
      If Successful Then      
         AStreams.Initialize(Socket1.InputStream, Socket1.OutputStream, "AStreams")      
      End If
      
      OpeningSocket = False
   Catch
      OpeningSocket = False
   End Try      
End Sub

Sub AStreams_NewData (Buffer() As Byte)
   ....
End Sub

Sub AStreams_Error
   ...
End Sub

By the way: I tried several values as Timeout (5000, 2000 and 0).
 

kkolle

Member
Licensed User
Longtime User
Hi Erel!

Thanks for your reply! How can I dump the programm status to a txt file?

In the filtered log nothing happens at this time.
 
Upvote 0

kkolle

Member
Licensed User
Longtime User
OK. I understand!

I will test it tomorrow morning in an area where I have bad mobile network coverage.
Hopefully you can see in the log what happens after calling "Connect".

Thanks again!!
 
Upvote 0

kkolle

Member
Licensed User
Longtime User
Hi Erel!

Attached you can find a complete dump of my unfiltered log.

The interesting lines are:
line 2054 - ** Service (socketservice) Start **
line 2060 - 071645 - connectToSocket - Initialize
....
line 4090 - ** Service (socketservice) Start **
line 4091 - 071730 - Connected: false ...

The service should run every 5 seconds. As you can see my app was not accessible for 45 seconds (after choose "wait" in the popup).

Here my complete function to connect to the socket-server:

B4X:
Sub connectToSocket
   
   Log(DateTime.Time(DateTime.Now) & " - connectToSocket - Counter: " & SocketReconnectCounter)
   
   If PhoneService.myPhoneIsReady = False Then
      Log(DateTime.Time(DateTime.Now) & " - connectToSocket - " & PhoneService.myPhoneIsReady)
      Return
   End If
   
   If SocketReconnectCounter <> 0 Then
      SocketReconnectCounter = SocketReconnectCounter - 1
      Log(DateTime.Time(DateTime.Now) & " - connectToSocket - Return: " & SocketReconnectCounter)
      Return
   End If
   
   ToastMessageShow(DateTime.Time(DateTime.Now) & " - connectToSocket", False)
   
   OpeningSocket = True
   
   Try
      Socket1.Initialize("Socket1")
      Log(DateTime.Time(DateTime.Now) & " - connectToSocket - Initialize")
      Socket1.Connect(SocketHost, SocketPort, 0)
      Log(DateTime.Time(DateTime.Now) & " - connectToSocket - Connect")
   Catch
   
   End Try
End Sub

Thanks!!
 

Attachments

  • 070645_dump.zip
    7.8 KB · Views: 215
Upvote 0

kkolle

Member
Licensed User
Longtime User
Hi Erel!

Attached you can find the "traces.txt" from my device.

Hopefully you can find something out with this information!

Thanks again!
 

Attachments

  • traces.zip
    26.7 KB · Views: 195
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
From this section of the file:
B4X:
at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
  at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55)
  at anywheresoftware.b4a.sql.SQL.ExecNonQuery2(SQL.java:77)
  at com.mO.mOFleetDroid.logger._loggps(logger.java:250)
  at com.mO.mOFleetDroid.gpsservice._handletimerevent(gpsservice.java:288)
  at com.mO.mOFleetDroid.gpsservice._service_start(gpsservice.java:373)
It appears that the main thread is busy executing an SQL query.
Check Logger.LogGps.
 
Upvote 0

kkolle

Member
Licensed User
Longtime User
Hi Erel, it´s me again!

I think I uploaded a wrong traces.txt! I made a new one!

Hopefully you can take a look at the new attached traces.txt.

B4X:
  at java.net.InetAddress.getaddrinfo(Native Method)
  at java.net.InetAddress.lookupHostByName(InetAddress.java:508)
  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:280)
  at java.net.InetAddress.getByName(InetAddress.java:310)
  at anywheresoftware.b4a.objects.SocketWrapper.Connect(SocketWrapper.java:84)
  at com.mO.mOFleetDroid.socketservice._connecttosocket(socketservice.java:529)

Thanks!
 

Attachments

  • traces.zip
    21.6 KB · Views: 182
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
That makes sense.
It hangs when trying to resolve the host name.

Please try the attached updated library (v1.11). The name resolution is now done in the background and should not block your program.
You will still not get any error in such case. Instead you will need to decide to close the socket, initialize it again and connect.
 

Attachments

  • Network.zip
    12.2 KB · Views: 243
Upvote 0

kkolle

Member
Licensed User
Longtime User
Hi Erel!

Many thanks! :sign0188:

My first test was successful! I will try it again tomorrow morning!
 
Upvote 0
Top