Android Question TCP/IP Connection without Interrupt

Sinan Tuzcu

Well-Known Member
Licensed User
Longtime User
Hi all,
I am looking for an example for TCP/IP Connection without Interrupt.

I have try some exanples what I found here but the connection is not stabil.

I have add also keepAlive but without success.
StartService every 10 sec. And set Foreground set Notification but doesn’t works.

Who can help me?
 

OliverA

Expert
Licensed User
Longtime User
StartService every 10 sec. And set Foreground set Notification but doesn’t works.
Why are you doing both?
I have try some exanples what I found here but the connection is not stabil.
How?
I am looking for an example for TCP/IP Connection without Interrupt.
Meaning?
I have add also keepAlive but without success.
What was the issue?

What is causing the TCP/IP connections to fail? Any logs? Any Wireshark logs to see if one or the other is dropping the connection (keepalives notwithstanding)? Are you sure you have good connection?
 
Upvote 0

Sinan Tuzcu

Well-Known Member
Licensed User
Longtime User
I use this in my Service for hold stabil. But it interrupt after sime minutes.

B4X:
Sub Service_Create
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER
    lock.PartialLock
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StartForeground(1, CreateNotification("..."))
    StartServiceAt(Me, DateTime.Now + 10 * 10000, True)
End Sub

Sub Service_Destroy
    lock.ReleasePartialLock
    Service.StopAutomaticForeground
    CancelScheduledService(Me)
End Sub

Sub CreateNotification (Body As String) As Notification
    Dim notification As Notification
    notification.Initialize2(notification.IMPORTANCE_LOW)
    notification.Icon = "TrainClock36x36"
    notification.Sound = False
    notification.Vibrate = False
    notification.SetInfo("Connected", Body, Main)
    Return notification
End Sub
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I use this in my Service for hold stabil. But it interrupt after sime minutes.
What interrupts? The service (is the service closed even though you need it running for long periods)? The TCP/IP connection? What do you need a continuous TCP/IP connection for?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
StartServiceAt(Me, DateTime.Now + 10 * 10000, True)
Android will not allow you to restart your service every 100 Seconds! At least not in Higher versions. You should restart it with a MINIMUM intervall of 15 MINUTES

Try it with a minimum of
B4X:
StartServiceAt(Me, DateTime.Now + 15*DateTime.TicksPerMinute, True)
 
Last edited:
Upvote 0

Sinan Tuzcu

Well-Known Member
Licensed User
Longtime User
What do you need a continuous TCP/IP connection for?
I need this for the check.
If changed the status of my device, then I get message from server.
Hence, the connection have to available any time.

Unless, I disconnect it myself
 
Upvote 0

Sinan Tuzcu

Well-Known Member
Licensed User
Longtime User
You should expect network connections to break from time to time. Your code should handle it and reconnect.
Is there a mechanism to determine if the connection is disconnected?
Because, the APP don't know this if the connection is disconnected.
Shall I trace in anytime, if the connection is there or not?
 
Upvote 0

Sinan Tuzcu

Well-Known Member
Licensed User
Longtime User
Android will not allow you to restart your service every 100 Seconds! At least not in Higher versions. You should restart it with a MINIMUM intervall of 15 MINUTES
Who gives such a specification that the Android does not allow under 15 minutes?
 
Upvote 0

thetahsk

Active Member
Licensed User
Longtime User
Just kidding. Of course Google. Who else ? or perhaps the man on the moon or the Sandmännchen ?
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
Is there a mechanism to determine if the connection is disconnected?

you can use the phone lib for this...

B4X:
Private pe As PhoneEvents

Sub pe_ConnectivityChanged (NetworkType As String, State As String, Intent As Intent)
   wifiIP=wifi.WifiIpAddress
   If State<>"CONNECTED" Then
       wifiConnected=False
   Else
       wifiConnected=True
   End If   
   UpdateUI
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
- This is the english part of the forum! please write english here.
- Erel is not speaking german
- You should handle the two event subs. See the Examples Erel mentioned. The Events are used there.

You need to use and respect them! But i guess you´ll ignore this like everything you ignore if you get help (from whoever).
 
Upvote 0

thetahsk

Active Member
Licensed User
Longtime User
The B4A documentation is online

https://www.b4x.com/android/help/randomaccessfile.html#asyncstreams

- NewData event is raised when new data is available.
- Error event is raised when an error was encountered. You should check LastException to find the error.
- Terminated event is raised when the other side has terminated the connection.
- NewStream event is only raised in prefix mode when the other side sends a stream with WriteStream.
This event is raised after the complete stream was received successfully.
- The event includes the saved stream folder and name. Note that the file name is an arbitrary string.

https://www.deepl.com/Translator
 
Upvote 0
Top