B4J Question TCP / IP with device

bogdanc

Active Member
Licensed User
Longtime User
Hi all!

I'm using B4j to communicate with hardware device on TCP /IP.
I used an Erel tutorial : https://www.b4x.com/android/forum/threads/android-network-tutorial.7001/

I can send command but when I try to initialize app is crashing:


I'm checking this in Timer.


  1. So first I initialize the connection to device
  2. Then I enabling the Timer
  3. On timer at the beginning I sending a command to my device
  4. Then I trying initialize the
    B4X:
     tr.Initialize(Socket1.InputStream)
    and app is crashing
 

bogdanc

Active Member
Licensed User
Longtime User
I've added a message to this old tutorial. You should use AsyncStreams instead: https://www.b4x.com/android/forum/pages/results/?query=asyncstreams

Hi Erel!

I check this great tutorial and I used code from this topic.
B4X:
Dim AStreams As AsyncStreams
   Dim TCPSocket As Socket
   Dim TCPAddress As String
   Dim TCPPort As Int
   Dim TCPDelay As Int
  
  
    Private Button1 As Button
    Private TextField1 As TextField
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    MainForm.RootPane.LoadLayout("async") 'Load the layout file.
    MainForm.Show
  
    'initialize connection with port
    Initialize
  
End Sub


    Public Sub Initialize
   TCPAddress ="192.168.0.30"
   TCPPort = 2000
   TCPDelay =5000
    TCPSocket.Initialize("TCPSocket")
    TCPSocket.Connect(TCPAddress , TCPPort, TCPDelay)
 
End Sub

Public Sub TCPSocket_Connected (Successful As Boolean)
    If Successful = False Then
        Log("Connection Failed")
'  
    End If
   AStreams.Initialize  (TCPSocket.InputStream,TCPSocket.OutputStream, "AStreams")
   Log("Connected")
   communicate
End Sub

Sub AStreams_Error
    Log(LastException.Message)
    Log("Error of rec")
End Sub


Sub AStreams_NewData (Buffer() As Byte)
    Log("new dataa test")
    Dim msg As String
    msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    Log(msg)
    Log(msg)
End Sub

Public Sub communicate
   Dim s As String
    s="test"
 
    If AStreams.IsInitialized = False Then
      Log("AStreams not Initialized")
         Return
         Else
             Log("Async initalized")
   End If
 
    Dim buffer() As Byte
    buffer = s.GetBytes("UTF8")
    AStreams.Write(buffer)
    Log("Sending: " & s)
  
End Sub

Public Sub Close
   AStreams.Close
   TCPSocket.Close
End Sub


And I can connect .
I'm trying to send some message to my device.
It's showing sending but I don't receiving anything.
I should get even some error.
 
Upvote 0

bogdanc

Active Member
Licensed User
Longtime User
Yes. my mistake I put \r instated of chr(13).

Thank You very much again Erel!

BTW: Your software is really grate
 
Upvote 0

bogdanc

Active Member
Licensed User
Longtime User
Works for me perfect.

But I have some issue: app is crashing when device is not reachable.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
If you are using the code from Post#3 then it is clear why. See my comment in the code after log("Connection....

B4X:
Public Sub TCPSocket_Connected (Successful As Boolean)
    If Successful = False Then
        Log("Connection Failed")
'                                                           ****** <- If connection is not possible/reachable then you need to exit this sub 
                                                                        'otherwise the AStream will try to execute and can't!
    End If
   AStreams.Initialize  (TCPSocket.InputStream,TCPSocket.OutputStream, "AStreams")
   Log("Connected")
   communicate
End Sub
 
Upvote 0

bogdanc

Active Member
Licensed User
Longtime User
If you are using the code from Post#3 then it is clear why. See my comment in the code after log("Connection....

B4X:
Public Sub TCPSocket_Connected (Successful As Boolean)
    If Successful = False Then
        Log("Connection Failed")
'                                                           ****** <- If connection is not possible/reachable then you need to exit this sub
                                                                        'otherwise the AStream will try to execute and can't!
    End If
   AStreams.Initialize  (TCPSocket.InputStream,TCPSocket.OutputStream, "AStreams")
   Log("Connected")
   communicate
End Sub


how to exit the sub by EXIT?
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Normally you would use "Return" and then look to see where the code goes back to, or you could try to re-initialize.
 
Upvote 0

bogdanc

Active Member
Licensed User
Longtime User
I used rerun but it did not work:
B4X:
java.net.SocketTimeoutException: connect timed out

and app crashed
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
I used rerun but it did not work:

Er how did you "rerun"?

Unless there is a very good reason not to, I would try along the lines of:

1. Rename your close sub to CloseSocket. I am not sure but close could be a reserved word. Avoid using words from the programming language as sub names.

2. Try changing to the code below [not tested]

B4X:
Public Sub TCPSocket_Connected (Successful As Boolean)
If Successful = False Then
       Log("Connection Failed")
       CloseSocket                        
       Initialize
else
      AStreams.Initialize (TCPSocket.InputStream,TCPSocket.OutputStream, "AStreams")
      Log("Connected")
       communicate
End If
End Sub


But be careful, this is almost an endless loop. You will need to maybe make X tries for a connection and then stop. Hope this helps.
 
Last edited:
Upvote 0
Top