AsyncStreams Problem

comptrmedic

Member
Licensed User
Longtime User
Hi All, new to B4A, have one program under my belt but having problems on the second one using asyncstreams. I'm creating a program to write a number 0 to 255 to a device server, no response from server is required. I've got it working except for one problem, it only transmits the last character, i.e. if I transmit 128 the device server reacts as if it got only the 8. I'm pretty hard headed and have searched the forum over and over but I'm bleeding now and need a bit of help. :BangHead: Thanks!
By the way, who ever came up with B4A is a frigging Genius!!
My Code:

Sub Process_Globals
Dim AStreams As AsyncStreams
End Sub

Sub Globals
Dim Socket1 As Socket
Dim EditText1 As EditText
Dim Button1 As Button
Dim Button2 As Button
Dim Timer1 As Timer
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("jetdirectcontrol")

End Sub

Sub Button1_Click
Socket1.Initialize("Socket1")
Socket1.Connect("192.168.1.146" , 9100, 0)
Log ("Socket Connect")
End Sub

Sub Socket1_Connected (Successful As Boolean)
If Successful = False Then
Msgbox(LastException.Message, "Error connecting")
Return
Else
AStreams.Initialize(Socket1.InputStream, Socket1.OutputStream, "AStreams")
End If
Log ("Connected No Error")
End Sub

Sub AStreams_NewData (Buffer() As Byte)
Dim msg As String
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF-8")
ToastMessageShow(msg, False)
Log(msg)
End Sub

Sub AStreams_Error
ToastMessageShow(LastException.Message, True)
End Sub

Sub EditText1_EnterPressed
If AStreams.IsInitialized = False Then Return
If EditText1.Text.Length > 0 Then
Dim buffer() As Byte
buffer = EditText1.Text.GetBytes("UTF8")
AStreams.Write(buffer)
EditText1.SelectAll
Log("Sending: " & EditText1.Text)
End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button2_Click
Socket1.Close
AStreams.Close
Log ("Socket Disconnected")
Activity.Finish
Log ("Program Closed")
End Sub
 

mc73

Well-Known Member
Licensed User
Longtime User
Somehow I think that your coding at the server's side expects a byte (since you mentioned sending a 0->255 byte). If that's the case, please note that when you're sending 128, in fact you're sending three bytes (1,2,8), thus probably your server neglects the other two. I guess you have to show a but of code at the server's side, as Erel already mentioned.
 
Upvote 0

comptrmedic

Member
Licensed User
Longtime User
Please use [ code ] [ /code ] tags (without spaces) when posting code.

Probably not related, however timers should be declared as process globals.

Your code seems to be correct. Where is the server code?

I forgot to strip the Timer out before I posted my code, I had tried to use it in case there was a speed issue transmitting the data, didn't cure the problem.

Regarding the server portion of the code, since I don't need a response from the device server I didn't think I needed it in there.
 
Upvote 0
Top