Android Question [Solved] Socket communications in Starter module

Didier9

Well-Known Member
Licensed User
Longtime User
I have an app that uses socket communications and Async Streams. It was written without a Starter module and it has several modes of operations with different screen layouts using panels in a single activity, all using the socket.
Very ugly (my first B4A app) but it works.
To clean things up, I have rewritten it using several activities (instead of multiple panels in a single activity) and trying to run the socket within the Starter module so that it does not close when I turn the tablet. The main idea is to keep the socket running while I switch activities. Not sure it is necessary or even helpful, but it seems cleaner to have a single socket shared by the activities and the Starter module seems like the right place to have it.
I have been unable to get anything in and out of the socket that way. The socket does connect to my target, but the data I send through it does not get to the other end.
There is a possibility that I missed something but I have spent several hours troubleshooting it without success so I have to ask if it is possible to run socket communications and Async Streams from the Starter module.

Here is a rough example of the code (showing all the socket and Async Stream related stuff) in the Starter module:

B4X:
Sub Process_Globals 
  Public Socket1 As Socket ' requires Network library
  Public ServerSocket1 As ServerSocket  ' requires Network library
  Public AStreams As AsyncStreams ' requires File library
  Public MyIP, TargetIP as string
End Sub

Sub Service_Create
  If ServerSocket1.IsInitialized Then ServerSocket1.close
  ServerSocket1.Initialize( 0, "" )
  MyIP = ServerSocket1.GetMyIP
  TargetIP = "1.2.3.4"
end Sub

Sub Service_Start( StartingIntent As Intent )
End Sub

Public Sub Connect
  If Socket1.IsInitialized = False Then
    Socket1.Initialize( "Socket1" )
  End If
  Socket1.Connect( TargetIP, 8899, 5000 )
End Sub

Sub Socket1_Connected( success As Boolean )
  If success  = True Then
    AStreams.Initialize( Socket1.InputStream, Socket1.OutputStream, "Astreams" )
  Else
    CallSub( myActivity, "ErrorSub" )
  End If
End Sub

Sub AStreams_NewData( Buffer() As Byte )
  Public data as String
  data = BytesToString( Buffer, 0, Buffer.Length, "UTF8" )
  CallSub2( myActivity, "GoodDataSub", data )
End Sub

Public Sub SendData( data as string )
  Public Buffer() As Byte
  Buffer = data.GetBytes( "UTF8" )
  AStreams.Write( Buffer )
End Sub

The activities communicate with the socket through the Connect, SendData() subs in Starter and GoodDataSub() and ErrorSub in each of the Activity modules. This short example does not show the Select statements used to call the subs in the proper activities.

Is it a correct way to handle it?

Thanks in advance.
 

Didier9

Well-Known Member
Licensed User
Longtime User
I answered my own question...

Since I had written a skeleton Starter module for the purpose of this post, I wrote the matching Main activity module and it works, so there must be something else wrong in my code.

I should have probably started that way...

Sorry for the bandwidth...
 
Upvote 0
Top