Android Question AsyncStreamsText problem

Devv

Active Member
Licensed User
Longtime User
I tried the following code after adding the "AsyncStreamsText" class

B4X:
Sub Process_Globals
   Private ast As AsyncStreamsText
   Dim Socket1 As Socket
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Socket1.Initialize("Socket1")
    Socket1.Connect("192.168.1.111", 403,0)
    'ast.Initialize(Me, "ast", Socket1.InputStream, Socket1.OutputStream)
End Sub


Sub Socket1_Connected (Successful As Boolean)
    If Successful = False Then
        Msgbox(LastException.Message, "Error connecting")
        Return
    End If
    ToastMessageShow("Connected successfully", False)
    ast.Initialize(Me, "ast", Socket1.InputStream, Socket1.OutputStream)
    ast.Write("test")
End Sub

when i run the code i cant receive any data on my server ...
but i see the Connected successfully toast
any ideas ?
 

Devv

Active Member
Licensed User
Longtime User
B4X:
TCPStartup()
$listen = TCPListen(@IPAddress1,403)

while 1
$accept = TCPAccept($listen)
$recive = TCPRecv($accept,10000000)
ConsoleWrite($recive)
WEnd

im pretty much sure that the problem is not with the server code
i tried to send messages using a tcp tool from play store it was correctly displayed on my server
 
Last edited:
Upvote 0

Devv

Active Member
Licensed User
Longtime User
mr erel please make alook at my project
 

Attachments

  • as.zip
    6.8 KB · Views: 119
Upvote 0

Devv

Active Member
Licensed User
Longtime User
the first code is working ...

B4X:
Sub Process_Globals
   Private ast As AsyncStreamsText
   Dim Socket1 As Socket
   Dim Ti As Long
End Sub

Sub Globals
    Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")
    Socket1.Initialize("Socket1")
    Socket1.Connect("192.168.1.111", 403,0)
    'ast.Initialize(Me, "ast", Socket1.InputStream, Socket1.OutputStream)
End Sub


Sub Socket1_Connected (Successful As Boolean)
    If Successful = False Then
        Msgbox(LastException.Message, "Error connecting")
        Return
    End If
    ToastMessageShow("Connected successfully", False)
    ast.Initialize(Me, "ast", Socket1.InputStream, Socket1.OutputStream)
    ast.Write("test")
    ToastMessageShow("hi",True)
   Ti = DateTime.Now + 5000
   Do While DateTime.Now < Ti
      DoEvents
   Loop
    ast.Close
End Sub

im reciving "test" on my server


this code is NOT working (i'm not receiving any data when i press the button)
B4X:
Sub Process_Globals
   Private ast As AsyncStreamsText
   Dim Socket1 As Socket
   Dim Ti As Long
End Sub

Sub Globals
    Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")
    Socket1.Initialize("Socket1")
    Socket1.Connect("192.168.1.111", 403,0)
    'ast.Initialize(Me, "ast", Socket1.InputStream, Socket1.OutputStream)
End Sub


Sub Socket1_Connected (Successful As Boolean)
    If Successful = False Then
        Msgbox(LastException.Message, "Error connecting")
        Return
    End If
    ToastMessageShow("Connected successfully", False)
    ast.Initialize(Me, "ast", Socket1.InputStream, Socket1.OutputStream)
End Sub

Sub Button1_Click
    ToastMessageShow("btn ckd",False)
        ast.Write("test")
   Ti = DateTime.Now + 5000
   Do While DateTime.Now < Ti
      DoEvents
   Loop
   ast.Close
End Sub

any ideas ?
 
Upvote 0

Devv

Active Member
Licensed User
Longtime User
the problem is that the string will not be sent if i ddnt closed the connection

B4X:
Sub Process_Globals
   Private ast As AsyncStreamsText
   Dim Socket1 As Socket
   Dim Ti As Long
End Sub

Sub Globals
    Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")
    Socket1.Initialize("Socket1")
    Socket1.Connect("192.168.1.111", 403,0)
    'ast.Initialize(Me, "ast", Socket1.InputStream, Socket1.OutputStream)
End Sub


Sub Socket1_Connected (Successful As Boolean)
    If Successful = False Then
        Msgbox(LastException.Message, "Error connecting")
        Return
    End If
    ToastMessageShow("Connected successfully", False)
    ast.Initialize(Me, "ast", Socket1.InputStream, Socket1.OutputStream)
End Sub

Sub Button1_Click
    ToastMessageShow("btn ckd",False)
        ast.Write("test")
End Sub


the upper code is not working


MR erel please edit my code and make it send when i press the button
thanks in advanced ....
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Maybe try this:

B4X:
Sub Process_Globals
   Private ast As AsyncStreamsText
   Dim Socket1 As Socket
   Dim Ti As Long
End Sub

Sub Globals
    Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")
    Socket1.Initialize("Socket1")
    Socket1.Connect("192.168.1.111", 403,0)
    'ast.Initialize(Me, "ast", Socket1.InputStream, Socket1.OutputStream)
End Sub


Sub Socket1_Connected (Successful As Boolean)
    If Successful = False Then
        Msgbox(LastException.Message, "Error connecting")
        Return
    End If
    ToastMessageShow("Connected successfully", False)
    ast.Initialize(Me, "ast", Socket1.InputStream, Socket1.OutputStream)
End Sub

Sub Button1_Click
    ToastMessageShow("btn ckd",False)
       SendData("Message you want to send here")
       SendData("Message you want to send here" & CRLF) ' You many need to send CRLF with the message ?
End Sub

Sub SendData (msg as string)
       Dim buffer() as Byte
              Buffer = msg.GetBytes("UTF8")
       ast.Write(Buffer)
       ast.Write(Array as Byte(254)) ' << Not sure if this line is required, (may want to try without it first)
End Sub
 
Upvote 0

Devv

Active Member
Licensed User
Longtime User
Hello Mr.aaronk

i am getting object converted to string error on this line " ast.Write(buffer)"
when i use your code...
 
Upvote 0

Devv

Active Member
Licensed User
Longtime User
You don't need to close the connection for the data to be sent.

As I wrote before you should try this desktop + device example: http://www.basic4ppc.com/android/fo...ceive-objects-over-the-network.34612/#content

bro honestly i cant understand your example , the demo app you make it is so complicated for me , im still a beginner
all what i need is the code of sending a simple tcp message

on most programming and scripting languages sending a tcp message is 2 or 3 lines long
why using your b4 a it needs 10+ lines of code !
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
why using your b4 a it needs 10+ lines of code !
There is a large difference between sending the data over the main thread which causes the whole app to hang and to be prone to crashes due to communication errors and sending data reliably with AsyncStreams.

BTW, your B4A code looks correct (without closing the connection). This is why I suggest that you test the B4J application as an alternative to the desktop app you are now running.
 
Last edited:
Upvote 0

Devv

Active Member
Licensed User
Longtime User
Erel thaaaanxxxx alot bro, the problem was in my server code i fixed it and every thing is now ok
i got
AsyncStreamsText working and text writer working
now every thing is ok , again thank you for your support
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
and every thing is now ok

You can make a tutorial about this including B4A and B4J Code do demonstrate ;-)
 
Upvote 0
Top