TCP / IP Socket

psdos

Active Member
Licensed User
Longtime User
Hello colleagues, because you will see in my second application I'm doing pretend controlled from my Galaxy Note a control card TCP / IP, in case some were wondering is this specifically.

Denkovi Assembly Electronics Ltd.: Wi-Fi IEEE 802.11 b / g Data Acquisition I / O Module

Well, the fact is that using Astream, if I managed to send Sockets to activate the relays, although I must say that does not always respond quickly, and is not an issue of the card.

My dilemma is that this card every time you sent a Socket, she responds with the state, something like an ACK.

example:

Set all relays (Whole PortA) ON. Actually all the OJ are in high level.
Send: 00ASG = FFFF;
Receive: 00AS0 = 0;

If someone would kindly tell me where to find a clear example where i see how to send / receive data. Also this card has the ability to use as a method of encriptacio RC4 to see if someone tells me if i can use in B4a.

Greetings and many thanks in advance.
 

psdos

Active Member
Licensed User
Longtime User
:sign0142: :sign0098:

It solves the issue, I put it here if you anyone, since I have not seen anything similar forum.

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim AStreams As AsyncStreams
    Dim Server As ServerSocket
    Dim Socket1 As Socket

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim EditText1 As EditText
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   
   Activity.LoadLayout("11")
   If FirstTime Then
        Server.Initialize(5500, "Server")
        Server.Listen
        'Log("MyIp = " & Server.GetMyIP)
      ToastMessageShow(("MyIp = " & Server.GetMyIP),True)
    End If
    EditText1.Initialize("EditText1")
    EditText1.ForceDoneButton = True
    Activity.AddView(EditText1, 10dip, 10dip, 300dip, 60dip)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
   If UserClosed Then
        Log("closing")
        AStreams.Close
        Socket1.Close
    End If
End Sub

Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
    If Successful Then
        ToastMessageShow("Connected", False)
        Socket1 = NewSocket
        AStreams.InitializePrefix(Socket1.InputStream, False, Socket1.OutputStream, "AStreams")
    Else
        ToastMessageShow(LastException.Message, True)
    End If
    Server.Listen
End Sub

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

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

Sub Button1_Click 'Button Sent
   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 Button2_Click 'Button Connect
   Socket1.Initialize("Socket1")
   'Set correct ServerIp 
   Socket1.Connect("192.168.0.100",1010,5000)' IP, Port, Timeout
End Sub

Sub Socket1_Connected(Connected As Boolean)As Boolean 
   If Connected = True Then
      ToastMessageShow("Conectado",True)
      AStreams.Initialize(Socket1.InputStream,Socket1.OutputStream,"Astreams")
   Else
      ToastMessageShow("Servidor no disponible",True)
   End If
   
End Sub
 
Upvote 0

psdos

Active Member
Licensed User
Longtime User
RC4 encryption

Now, I would like someone to show me if I can use RC4 encryption in the delivery receipt of these sockets.

Thank you very much.
 
Upvote 0

wl

Well-Known Member
Licensed User
Longtime User
Unfortunately RC4 is currently not supported by the encryption library, it seems.

Wim
 
Upvote 0

psdos

Active Member
Licensed User
Longtime User
Thanks wl for you rapid reply, is possible convert this code of VB-NET to B4A?


B4X:
Imports System.Text



Public Shared Function rc4(ByVal message As String, ByVal password As String) As String

        Dim i As Integer = 0
        Dim j As Integer = 0
        Dim cipher As New StringBuilder
        Dim returnCipher As String = String.Empty

        Dim sbox As Integer() = New Integer(256) {}
        Dim key As Integer() = New Integer(256) {}

        Dim intLength As Integer = password.Length

        Dim a As Integer = 0
        While a <= 255

            Dim ctmp As Char = (password.Substring((a Mod intLength), 1).ToCharArray()(0))

            key(a) = Microsoft.VisualBasic.Strings.Asc(ctmp)
            sbox(a) = a
            System.Math.Max(System.Threading.Interlocked.Increment(a), a - 1)
        End While

        Dim x As Integer = 0

        Dim b As Integer = 0
        While b <= 255
            x = (x + sbox(b) + key(b)) Mod 256
            Dim tempSwap As Integer = sbox(b)
            sbox(b) = sbox(x)
            sbox(x) = tempSwap
            System.Math.Max(System.Threading.Interlocked.Increment(b), b - 1)
        End While

        a = 1

        While a <= message.Length

            Dim itmp As Integer = 0

            i = (i + 1) Mod 256
            j = (j + sbox(i)) Mod 256
            itmp = sbox(i)
            sbox(i) = sbox(j)
            sbox(j) = itmp

            Dim k As Integer = sbox((sbox(i) + sbox(j)) Mod 256)

            Dim ctmp As Char = message.Substring(a - 1, 1).ToCharArray()(0)

            itmp = Asc(ctmp)

            Dim cipherby As Integer = itmp Xor k

            cipher.Append(Chr(cipherby))
            System.Math.Max(System.Threading.Interlocked.Increment(a), a - 1)
        End While

        returnCipher = cipher.ToString
        cipher.Length = 0

        Return returnCipher

    End Function







How to use source:

rc4("Text","password")



Thanks.
 
Last edited:
Upvote 0

psdos

Active Member
Licensed User
Longtime User
Code for RC4 encryption convert from VB.NET to B4A.

B4X:
Sub rc4(message As String, password As String) As String

      Dim i As Int
      i = 0
        Dim j As Int
      j = 0
        Dim cipher As StringBuilder
      cipher.Initialize
        Dim returnCipher As String
      returnCipher = ""
      
        Dim sbox(256) As Int
        Dim key(256) As Int

        Dim intLength As Int
      intLength = password.Length

        Dim a As Int
      a = 0
        Do While a <= 255

            Dim ctmp As Char
         ctmp = password.SubString(a Mod intLength)
            key(a) = Asc(ctmp)
            sbox(a) = a
            a = a + 1
        Loop

        Dim x As Int
      x = 0

        Dim b As Int
      b = 0
        Do While b <= 255
            x = (x + sbox(b) + key(b)) Mod 256
            Dim tempSwap As Int
         tempSwap = sbox(b)
            sbox(b) = sbox(x)
            sbox(x) = tempSwap
            b = b + 1
          Loop

        a = 1

        Do While a <= message.Length

            Dim itmp As Int
         itmp = 0

            i = (i + 1) Mod 256
            j = (j + sbox(i)) Mod 256
            itmp = sbox(i)
            sbox(i) = sbox(j)
            sbox(j) = itmp

            Dim k As Int
         k = sbox((sbox(i) + sbox(j)) Mod 256)

            Dim ctmp As Char
         ctmp = message.Substring(a - 1)

            itmp = Asc(ctmp)

            Dim cipherby As Int
         cipherby = Bit.Xor(itmp, k)

            cipher.Append(Chr(cipherby))
            a = a + 1
        Loop

        returnCipher = cipher.ToString
       
      Return returnCipher

    End Sub
 
Upvote 0
Top