Use only Socket to send/receive (?)

poseidon

Member
Licensed User
Longtime User
AsyncStreams receive unicode (?)

B4X:
Sub Process_Globals
    Dim AStreams As AsyncStreams
    Dim Socket1 As Socket
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("main")
End Sub

Sub Socket1_Connected (Successful As Boolean)
    If Successful = False Then
        Msgbox(LastException.Message, "Error connecting")
        Return
    End If
      
       AStreams.Initialize(Socket1.InputStream, Socket1.OutputStream, "AStreams")
      
   Dim buffer() As Byte
   Dim ff As String 
   ff="123456789012345678901234567890"
        buffer = ff.GetBytes("UTF8")
        AStreams.Write(buffer)
End Sub 

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

Sub Button1_Click
    Socket1.Initialize("Socket1")
    Socket1.Connect("10.0.0.136" , 50959, 20000)
End Sub

I can read the response from AStreams_NewData
thats great, but there is no unicode... :(

instead of GREEK I got chinese... btw my server is ASP based
 
Last edited:

mc73

Well-Known Member
Licensed User
Longtime User
As Erel said, check your server's encoding. I am greek too, I had a bit of hard time with correctly adjusting my data but found the way.
In the case of using a vb or vb.net server, google for utf-8 decode/encode functions. They will solve the problem.
 
Upvote 0

poseidon

Member
Licensed User
Longtime User
for the history

source of EncodeUTF8
B4X:
http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=44569&lngWId=1

--

@ VB
B4X:
Private Declare Function GetACP Lib "Kernel32" () As Long
Private Declare Function WideCharToMultiByte Lib "Kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, lpUsedDefaultChar As Long) As Long

Public Const CP_UTF8 = 65001
Public Const CP_UTF7 = 65000
Public Const CP_UNICODELITTLE = 1200
Public Const CP_UNICODEBIG = 1201

Public Function WToA(ByVal st As String, Optional ByVal cpg As Long = -1, Optional lFlags As Long = 0) As String
    Dim stBuffer As String
    Dim cwch As Long
    Dim pwz As Long
    Dim pwzBuffer As Long
    Dim lpUsedDefaultChar As Long
    
    If cpg = -1 Then cpg = GetACP()
    pwz = StrPtr(st)
    cwch = WideCharToMultiByte(cpg, lFlags, pwz, -1, 0&, 0&, ByVal 0&, ByVal 0&)
    stBuffer = String$(cwch + 1, vbNullChar)
    pwzBuffer = StrPtr(stBuffer)
    cwch = WideCharToMultiByte(cpg, lFlags, pwz, -1, pwzBuffer, Len(stBuffer), ByVal 0&, ByVal 0&)
    WToA = Left$(stBuffer, cwch - 1)
End Function

--then this can be posted as UTF8!!
MsgBox.Show(StrConv(WToA("ΑΝΔΡΟΙΔάέίώςΆΈΙΏΣ", CP_UTF8, 0), vbUnicode))

@device :
B4X:
Sub AStreams_NewData (Buffer() As Byte)
   Msgbox(BytesToString(Buffer,0,Buffer.Length,"UTF8"),"TEST")
End Sub
 
Upvote 0

andrewtheart

Member
Licensed User
Longtime User
Is there any risk of losing some of the transferred data by using async stream and sockets?


AsyncStreams receive unicode (?)

B4X:
Sub Process_Globals
    Dim AStreams As AsyncStreams
    Dim Socket1 As Socket
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("main")
End Sub

Sub Socket1_Connected (Successful As Boolean)
    If Successful = False Then
        Msgbox(LastException.Message, "Error connecting")
        Return
    End If
   
       AStreams.Initialize(Socket1.InputStream, Socket1.OutputStream, "AStreams")
   
   Dim buffer() As Byte
   Dim ff As String
   ff="123456789012345678901234567890"
        buffer = ff.GetBytes("UTF8")
        AStreams.Write(buffer)
End Sub

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

Sub Button1_Click
    Socket1.Initialize("Socket1")
    Socket1.Connect("10.0.0.136" , 50959, 20000)
End Sub

I can read the response from AStreams_NewData
thats great, but there is no unicode... :(

instead of GREEK I got chinese... btw my server is ASP based
 
Upvote 0
Top