Socket problem with service module

Dirk A

Member
Licensed User
Longtime User
Hello,

in my service module i've declared a socket ' Socket1' . I've got a sub called connectToSocket where I initialize and connect the socket.

The Socket1_Connected sub is also located in my service module.

When I run my program i get the runtime error : "sub socket1_connected' was not found.

Is it necessary to place this sub in your main module ? Because I would like to use my main module solely for the user interface.

thank you in advance
 

Penko

Active Member
Licensed User
Longtime User
With Activities, Events are normally handled within the current Activity active. Not sure how it looks like for Services but I guess it's similar.

Try putting the _Connected inside your Activity Module.

If you want to keep the Activity for User Interface purposes only, then add a Code Module and isolate the Socket logic there.

Within the Activity Event, just call your function with the respective arguments(if any) and you will achieve some level of code isolation.
 
Upvote 0

Dirk A

Member
Licensed User
Longtime User
Thank you for your replies.

I made a mistake in my previous post , the socket and all the subs are located in a static code module (not in a service) .

my subs are really basic for the moment (just for testing) :

B4X:
Dim Socket1 As Socket
Dim SOCKETHOST As String 
Dim SOCKETPORT As Int 
SOCKETHOST  = "myserver.com" 
SOCKETPORT  = 1234
   
Sub connectToSocket ()     '   
   Socket1.Initialize("Socket1")       
   Socket1.Connect(SOCKETHOST, SOCKETPORT, 200)        
End Sub

Sub Socket1_Connected (Successful As Boolean)  
   Try
      If  Successful = False  Then     
             Msgbox("Error Connecting to server","OK")        
      Else
         Msgbox("Connected to server","OK")     
      End If
    Catch
   End Try
End Sub

the connectToSocket method is called from the main activity_create

I've tried to put the previous code in a service module but the problem is that i can't call the connectToSocket method from the service module.
serviceModule.connectToSocket doesn't work
 
Upvote 0

tango

Active Member
Licensed User
Longtime User
in my service module i've declared a socket ' Socket1' . I've got a sub called connectToSocket where I initialize and connect the socket.

The Socket1_Connected sub is also located in my service module.

When I run my program i get the runtime error : "sub socket1_connected' was not found.

Is it necessary to place this sub in your main module ? Because I would like to use my main module solely for the user interface.

thank you in advance
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
I've just replied in your other thread. Please do not duplicate questions.
But to be clear, service module's can handle events whereas code modules can't. Code modules are really only for performing business logic, crunching numbers and providing a result etc. No view's and no events.
 
Upvote 0
Top