Android Question Is there a DataInterval function similar to the Winsock control in VB6.0 in B4A?

bskotu555

Member
Is there a DataInterval function similar to the Winsock control in VB6.0 in B4A, so that I can retrieve the content returned by the device.


Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
Winsock1.GetData strData
If strData <> "" Then
MsgBox strData
End If
End Sub

If possible, can you directly modify it on the following code?

Private Sub ConnectToServer(Host As String)
Log("Trying to connect to: " & Host)
CloseExistingConnection
Dim client As Socket
client.Initialize("client")
client.Connect(Host, 8999, 10000)
Wait For Client_Connected (Successful As Boolean)
If Successful Then
Log("OK")
astream.InitializePrefix(client.InputStream, False, client.OutputStream, "astream")
UpdateState (True)
Else
Log("Failed to connect: " & LastException)
End If
End Sub
 

bskotu555

Member
请在此使用[明码][暗码]...发布代码时的[/code][/plain]标记。


您需要处理NewData事件。参见示例。

注意只有当连接的两端都实现前缀协议时,前缀模式才起作用。
1730269362057.png
 
Upvote 0

bskotu555

Member
Please use [code]code here...[/code] tags when posting code.

You need to handle the NewData event. See the example.

Note the prefix mode will only work if both sides of the connection implement the prefix protocol.
I introduced the Asynchronous StreamsText.bas module and modified the NewDATA event. I'm not sure if that's correct, right?
 

Attachments

  • Network Example.zip
    221.1 KB · Views: 11
Upvote 0

teddybear

Well-Known Member
Licensed User
Please read carefully the post #2 replied by Erel first

1.Please use [code]code here...[/code] tags when posting code. don't upload a code picture
20241030160759.png


2.Note the prefix mode will only work if both sides of the connection implement the prefix protocol.
From your previous post, the server seems to be a a fixed barcode reader, I don't think it uses the prefix protocol.

3.You can join aeric's group for help[/code]
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
Try
B4X:
Private Sub ConnectToServer(Host As String)
    Log("Trying to connect to: " & Host)
    CloseExistingConnection
    Dim client As Socket
    client.Initialize("client")
    client.Connect(Host, PORT, 10000)
    Wait For Client_Connected (Successful As Boolean)
    If Successful Then
        'astream.InitializePrefix(client.InputStream, False, client.OutputStream, "astream")
        astream.Initialize(client.InputStream, Null, "astream")
        UpdateState (True)
    Else
        Log("Failed to connect: " & LastException)
    End If
End Sub

B4X:
Sub AStream_NewData (Buffer() As Byte)
    Dim message As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    Log(message)
'    Dim mm As MyMessage = ser.ConvertBytesToObject(Buffer)
'    txtAge.Text = mm.Age
'    txtName.Text = mm.Name
'    Dim bmp As B4XBitmap = LoadBitmapFromBytes(mm.Image)
'    bmp = bmp.Resize(pnlCanvas.Width, pnlCanvas.Height, False)
'    cvs.ClearRect(cvs.TargetRect)
'    cvs.DrawBitmap(bmp, cvs.TargetRect)
'    cvs.Invalidate
End Sub
 
Upvote 0

bskotu555

Member
Perfectly solved my problem, null is the key
Try
B4X:
Private Sub ConnectToServer(Host As String)
    Log("Trying to connect to: " & Host)
    CloseExistingConnection
    Dim client As Socket
    client.Initialize("client")
    client.Connect(Host, PORT, 10000)
    Wait For Client_Connected (Successful As Boolean)
    If Successful Then
        'astream.InitializePrefix(client.InputStream, False, client.OutputStream, "astream")
        astream.Initialize(client.InputStream, Null, "astream")
        UpdateState (True)
    Else
        Log("Failed to connect: " & LastException)
    End If
End Sub

B4X:
Sub AStream_NewData (Buffer() As Byte)
    Dim message As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    Log(message)
'    Dim mm As MyMessage = ser.ConvertBytesToObject(Buffer)
'    txtAge.Text = mm.Age
'    txtName.Text = mm.Name
'    Dim bmp As B4XBitmap = LoadBitmapFromBytes(mm.Image)
'    bmp = bmp.Resize(pnlCanvas.Width, pnlCanvas.Height, False)
'    cvs.ClearRect(cvs.TargetRect)
'    cvs.DrawBitmap(bmp, cvs.TargetRect)
'    cvs.Invalidate
End Sub
 
Upvote 0
Top