Android Question sockets, pc as server, b4a as service client

mikewebb

Member
Licensed User
Longtime User
man i've been beating my head over this for 2 days now. heres what i have, a pc sitting at 192.168.2.101 is listening for a connection. when a use the b4a program client2 everything works fine. with the program client5 i'm trying to move the code from client2 into a service. everything seems right to me. but what happens its that the connection is made and the server is ready to accept the incoming message but nothing happens. nether program locks, they just sit there. i've included both the working client2 and the nonworking client5. but heres then code for the heart of client5, some of you gurus might just be able to look at this and see the problem.

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.

Dim Socket1 As Socket
Dim AStreams As AsyncStreams
End Sub
Sub Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)
Socket1.Initialize("Socket1")
Socket1.Connect("192.168.2.101", 8888, 10000)
If AStreams.IsInitialized = False Then Return
Dim Bytebuffer() As Byte
Dim data As String
data = "Diod: Home Router in Range" & CRLF
Bytebuffer = data.GetBytes("UTF8")
AStreams.Write(Bytebuffer)
End Sub

Sub Service_Destroy
AStreams.Close
Socket1.Close
End Sub

Sub Socket1_Connected (Successful As Boolean)
If Successful Then
ToastMessageShow("Connected!", False)
AStreams.Initialize(Socket1.InputStream, Socket1.OutputStream, "AStreams")
Else
ToastMessageShow("Error while connecting!", False)
End If
End Sub
Sub AStreams_NewData (Buffer() As Byte)
Dim msg As String
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
ToastMessageShow(msg, False)
Log("Received: " & msg & " from the server")
End Sub
Sub AStreams_Error
ToastMessageShow(LastException.Message, True)
End Sub

Sub Activity_Pause(UserClosed As Boolean)
If UserClosed Then
' Log("closing")
AStreams.Close
Socket1.Close
End If
End Sub
 

Attachments

  • client2.zip
    7.4 KB · Views: 206
  • client5.zip
    7.4 KB · Views: 187

Erel

B4X founder
Staff member
Licensed User
Longtime User
Please use [ code ] [ /code ] tags (without spaces) when posting code.

How can AStream be initialized in this point:
B4X:
Sub Service_Start (StartingIntent As Intent)
Socket1.Initialize("Socket1")
Socket1.Connect("192.168.2.101", 8888, 10000)
If AStreams.IsInitialized = False Then Return
Dim Bytebuffer() As Byte
Dim data As String
data = "Diod: Home Router in Range" & CRLF
Bytebuffer = data.GetBytes("UTF8")
AStreams.Write(Bytebuffer)
End Sub
 
Upvote 0

mikewebb

Member
Licensed User
Longtime User
left the is streams.IsInitialized = False in by mistake. i've cut away a lot of code so that all thats left is the bare minimal
moved the socket initilized up to Service_Create. still doesn't work. another question, i'm not sure what you mean by [ code ] [ /code ] tags can you further explain ?
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
That means you have to enclose your code within those tags, like this:

[code]

Dim Var as String

etc etc etc

[/code]

That will show the code on the forums like this:
B4X:
Dim Var as String
etc etc etc
 
Upvote 0

mikewebb

Member
Licensed User
Longtime User
let me give it a try, heres what i have now:

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   
   Dim Socket1 As Socket
     Dim AStreams As AsyncStreams

End Sub
Sub Service_Create
   Socket1.Initialize("Socket1")   
End Sub

Sub Service_Start (StartingIntent As Intent)
   Socket1.Connect("192.168.2.101", 8888, 10000)
   Dim Bytebuffer() As Byte
   Dim data As String
   data = "Diod: Home Router in Range" & CRLF
   Bytebuffer = data.GetBytes("UTF8")
   AStreams.Write(Bytebuffer)
   
   

End Sub

Sub Service_Destroy
 AStreams.Close
 Socket1.Close
End Sub

Sub Socket1_Connected (Successful As Boolean)
  If Successful Then
  ToastMessageShow("Connected!", False)
  AStreams.Initialize(Socket1.InputStream, Socket1.OutputStream, "AStreams")
  Else
  ToastMessageShow("Error while connecting!", False)
  End If
End Sub
Sub AStreams_NewData (Buffer() As Byte)
  Dim msg As String
  msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
  ToastMessageShow(msg, False)
  Log("Received: " & msg & " from the server")
End Sub
Sub AStreams_Error
  ToastMessageShow(LastException.Message, True)
End Sub

Sub Activity_Pause(UserClosed As Boolean)
  If UserClosed Then
  ' Log("closing")
  AStreams.Close
  Socket1.Close
  End If
End Sub
Sub Sleep(Ms As Int)
  Dim Ti As Long
  Ti = DateTime.Now + (Ms)
  Do While DateTime.Now < Ti
  DoEvents
  Loop
End Sub
[ /code ]

oh yes, i see in perview thats much better, thanks !
 
Upvote 0

mikewebb

Member
Licensed User
Longtime User
i'm still plugging away here. i tried something different. i took the working code from client2 and created a new b4a program that is not a service. i took all the code from button1_click and added it to Activity_Create. then instead of typing into edittext1 i add the line of code "EditText1.Text = "not working". so what should happen is the program starts, connects to the server and sends a message. the ui is not really necessary. it doesn't work. it reacts the same as client5 does. a connection is made and then both programs just sit there. this leads me to believe that the problem with client5 is not that its a process. in fact the only difference i see between (working) client2 and (non-working) client 2.1 is that the process of connect, initalize,write all happen together very fast (i have tried including some delays). with the ui client 2 there a delay between starting the program, entering text, then pressing the send button. the other difference is that in the none working client2.1 i'm telling it in code what the value of edittext1 is. Heres the code for the non-working client2.1:
B4X:
Sub Process_Globals
  Dim Socket1 As Socket
  Dim AStreams As AsyncStreams
End Sub
Sub Globals
  Dim Button1 As Button
  Dim EditText1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
  Activity.LoadLayout("Layout1")
 Socket1.Initialize("Socket1")
  Socket1.Connect("192.168.2.101", 8888, 10000)
   Log("AStreams Status Pre-send: " & AStreams.IsInitialized)
  If AStreams.IsInitialized = False Then Return
  EditText1.Text = "not working"
   If EditText1.Text.Length > 0 Then
  Dim buffer() As Byte
     Dim data As String
  data = EditText1.text & CRLF
     buffer = data.GetBytes("UTF8")
  AStreams.Write(buffer)
  EditText1.SelectAll
  Log("Sending: " & EditText1.Text)
  Log("AStreams Status Post-send: " & AStreams.IsInitialized)
  End If
End Sub
Sub Socket1_Connected (Successful As Boolean)
     If Successful Then
  ToastMessageShow("Connected!", False)
  AStreams.Initialize(Socket1.InputStream, Socket1.OutputStream, "AStreams")
  Else
  ToastMessageShow("Error while connecting!", False)
  End If
End Sub
Sub AStreams_NewData (Buffer() As Byte)
  Dim msg As String
  msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
  ToastMessageShow(msg, False)
  Log("Received: " & msg & " from the server")
End Sub
Sub AStreams_Error
  ToastMessageShow(LastException.Message, True)
End Sub

Sub Activity_Pause(UserClosed As Boolean)
  If UserClosed Then
  Log("closing")
  AStreams.Close
  Socket1.Close
  End If
End Sub
 
Upvote 0

mikewebb

Member
Licensed User
Longtime User
well i got it to work. i took the buffer variable in the activite_create sub and made it global (renamed it to ByteBuffer seems like everything is called buffer). then in socket1_connected, after astreams.intialize, i added the line AStreams.Write(ByteBuffer). i know when the progam starts that bytebuffer is emtpy but somehow that seems to kick start the system. anyway i guess problem solved. i just don't like it when i dont understand how and why its working this way and not the proper way.
 
Upvote 0
Top