Android Question Socket MemoryStream Connect B4A

Amanda Sura

Member
Licensed User
I have this code in vb.net.
Connect perfectly!

B4X:
Public Shared Sub Send(ByVal msg As String)


        Try

            Using MS As New MemoryStream

                Dim B As Byte() = SB(msg)

                Dim L As Byte() = SB(B.Length & CChar(vbNullChar))



                MS.Write(L, 0, L.Length)

                MS.Write(B, 0, B.Length)



                S.Poll(-1, SelectMode.SelectWrite)

                S.Send(MS.ToArray, 0, MS.Length, SocketFlags.None)

            End Using

        Catch ex As Exception

            isDisconnected()

        End Try

    End Sub
I tried this on b4a

B4X:
    If socket1.Connected = True Then
        Dim xStringBytes() As Byte = xEnvio.GetBytes("UTF8")
        If astream.IsInitialized  Then
            astream.Write(xStringBytes)
        End If
    End If
Does not work :(
 

OliverA

Expert
Licensed User
If astream is not initialized, this code will not sent anything
 

Amanda Sura

Member
Licensed User
[QUOTE = "OliverA, postagem: 649334, membro: 97072"] Se astream não for inicializado, esse código não enviará nada [/ QUOTE]

This initialized ...
 

OliverA

Expert
Licensed User
Code? How are you initializing the astream? How are you setting up the socket connection? You only give us a very small code section to look at. No error messages?
 

Amanda Sura

Member
Licensed User
The connection is made with success, then

B4X:
    AStreamsClient.Initialize(Socket2.InputStream,Socket2.OutputStream,"AStreamsClient")
It returns no error.

But using this code I have the error in Visual Studio
B4X:
    astream.InitializePrefix(socket1.InputStream, False, socket1.OutputStream, "astream")
B4X:
The string "..." in value bytes can not be converted to Long
 

OliverA

Expert
Licensed User
astream.InitializePrefix(socket1.InputStream, False, socket1.OutputStream, "astream")
Your VB code does not produce prefix mode data. To see what it takes to do that, you can look at the C# implementation by @Erel: https://www.b4x.com/android/forum/t...ement-asyncstreams-prefix-mode.30741/#content
BTW, I'm totally unclear what you are trying to do and where the error appears. Are you sending stuff from B4A to your VB.NET program? Are you receiving information? What is xEnvio? Both code examples in your first post deal with sending (the VB.NET code and the B4A code), but you have no receiving code set up/posted. So first, make up your mind on who sends and who receives first. Get that working (for example, make VB.NET send and B4A receive). Once you get that working, then try it the other way around. Unless you are willing to properly handle B4X's prefix mode in VB.NET, don't use it. You may implement your own (maybe that's what you are doing in you VB.NET sample), since AsyncStreams does not guarantee that an item will arrive / be sent in one packet.
 

Amanda Sura

Member
Licensed User
Why b4j does not have some skins I need.

I have an example ready with b4j, it was very fast.
But I can not seem to leave the beautiful layout with b4j, so I'm trying to do it in vb.net
 

Amanda Sura

Member
Licensed User
This is the problem, I can not connect to vb.net

By b4j I can perfectly.

But vb.net exists more beautiful skins for the user
 

OliverA

Expert
Licensed User
This is the problem, I can not connect to vb.net
Again, you've not really given us enough to see if there is a problem. You only have given us
1) The sent routine in VB, that seem to sent a minimum of 2 packets
a) A string encoded length with a 0 byte ending (in the style of C string handling)
b) The actual string you are sending
Note: Both seem to be encoded to bytes via some function called SB, but without code, I'm just guessing.
2) The sent routine in B4A which just seems to sent the contents of the string xEnvio

What you don't show is is the receiving code of either! On top of that, if 1) is an attempt to emulate the prefix mode that is available in B4X's AsyncStreams, then that is totally wrong. Again, I'm guessing here since you have not responded to my previous question(s).

Looking at your VB.NET code in post #1, your B4A code should receive a minimum of two NewData events, one for your encoded length, another (or more) for the string you are sending. But, again, I'm guessing since you have not shown us the NewData event code for the B4A side. Also, how are you determining the address to use in you VB.NET address to sent data to you B4A app?
 
Top