Android Question Send string text from B4A into vb.net

eng.khalidvb

Member
Licensed User
Longtime User
Hi

could any one have an idea about send string text from B4A into vb.net

I have already complete the first part is send string text from vb.net into B4A and it successfully done.

But I can not understand what is the code should be to send string text from B4A into vb.net

here is my code for B4A

B4X:
'Activity module
Sub Process_Globals
  Dim ServerSocket1 As ServerSocket
  Dim Socket1 As Socket
  Dim Timer1 As Timer
  Dim InputStream1 As InputStream
'  Dim OutputStream1 As OutputStream
  Dim raf As RandomAccessFile
End Sub

Sub Globals
  Dim lblName As Label
    Dim lbl_connection As Label
    Dim txt_recive As EditText
    Private btn_send As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
  If FirstTime Then
      Timer1.Initialize("Timer1", 200)
      File.MakeDir(File.DirRootExternal, "android") 'probably already exists
  End If
  'Initialize the server socket if it is not initialized.
  'ServerSocket1 will not be initialized when FirstTime=True and after the user
  'closed the activity and then opened it again (by pressing on the back key)
  If ServerSocket1.IsInitialized = False Then
      ServerSocket1.Initialize(2222, "ServerSocket1")
  End If
  ToastMessageShow("My IP: " & ServerSocket1.GetMyIP, True)
  Activity.LoadLayout("1")
End Sub
Sub ServerSocket1_NewConnection (Successful As Boolean, NewSocket As Socket)
  If Successful Then
      Socket1 = NewSocket
      Timer1.Enabled = True
      InputStream1 = Socket1.InputStream
'      OutputStream1 = Socket1.OutputStream
      lbl_connection.Text= "Connected"
      ToastMessageShow("Connected", True)
  Else
      Msgbox(LastException.Message, "Error connecting")
  End If
  ServerSocket1.Listen 'Continue listening to new incoming connections
End Sub

Sub Activity_Resume
  ServerSocket1.Listen
End Sub

Sub Activity_Pause (UserClosed As Boolean)
  If UserClosed Then
      Timer1.Enabled = False
      Socket1.Close
      ServerSocket1.Close 'stop listening
  End If
End Sub

Sub Timer1_Tick
  Dim BC As ByteConverter
 
  If InputStream1.BytesAvailable > 0 Then
      Timer1.Enabled = False
     
      Dim buffer(8192) As Byte
      raf.Initialize3(buffer, True)

      InputStream1.ReadBytes(buffer, 0, buffer.Length)
      txt_recive.Text=(BC.StringFromBytes(buffer,"UTF-8"))
     
  Timer1.Enabled = True
  End If
End Sub

Sub btn_send_Click
   
    ' what is the code should be here to send string text into the text box in the vb.net
   
End Sub

what is the code should be in btn_send to send the text into the vb.net ?


Here is my vb.net code

B4X:
Imports System.Threading
Imports System.Net
Imports System.Net.Sockets

Public Class Form1

    Dim ip As IPAddress = IPAddress.Parse("127.0.0.1")
    ' the port must be the same in client ans server
    Dim port As Integer = 2222
    Dim sock As New TcpClient()
    Dim Listener As New TcpListener(2222)
    Dim Client As New TcpClient
    Dim Message As String = ""

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try

        ip = IPAddress.Parse(TextBox_ip.Text)
        port = TextBox_port.Text
            sock.Connect(ip, port)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        sock.Client.Send(System.Text.Encoding.UTF8.GetBytes(TextBox1.Text))
    End Sub
    Private Sub Listening()
        Listener.Start()
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        Listener.Stop()
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim ListThread As New Thread(New ThreadStart(AddressOf Listening))
        ListThread.Start()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If Listener.Pending = True Then
            Message = ""
            Client = Listener.AcceptTcpClient()

            Dim Reader As New StreamReader(Client.GetStream())
            While Reader.Peek > -1
                Message = Message + Convert.ToChar(Reader.Read()).ToString
            End While

            MsgBox(Message, MsgBoxStyle.OkOnly)
        End If
    End Sub
End Class


Dose any one have any idea? ... plz help I am new in B4A

Modify my code ..plz

:(:(:(
 

realblue

Member
Licensed User
Longtime User
Try this :
B4X:
dim c As String = "Test"
Dim UDPSocket1 As UDPSocket
UDPSocket1.Initialize("UDP", < Port Address here >,65535)
Dim Packet As UDPPacket
Packet.Initialize(c.GetBytes("UTF8"),  <IpAddress>, <PortAddress>)
UDPSocket1.Send(Packet)
 
Upvote 0

eng.khalidvb

Member
Licensed User
Longtime User
Hi
I tried your code but it dosent work

this my full code in B4A

B4X:
'Activity module
Sub Process_Globals
  Dim ServerSocket1 As ServerSocket
  Dim Socket1 As Socket
  Dim Timer1 As Timer
  Dim InputStream1 As InputStream
'  Dim OutputStream1 As OutputStream
  Dim raf As RandomAccessFile
End Sub

Sub Globals
  Dim lblName As Label
    Dim lbl_connection As Label
    Dim txt_recive As EditText
    Private btn_send As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
  If FirstTime Then
      Timer1.Initialize("Timer1", 200)
      File.MakeDir(File.DirRootExternal, "android") 'probably already exists
  End If
  'Initialize the server socket if it is not initialized.
  'ServerSocket1 will not be initialized when FirstTime=True and after the user
  'closed the activity and then opened it again (by pressing on the back key)
  If ServerSocket1.IsInitialized = False Then
      ServerSocket1.Initialize(2222, "ServerSocket1")
  End If
  ToastMessageShow("My IP: " & ServerSocket1.GetMyIP, True)
  Activity.LoadLayout("1")
End Sub
Sub ServerSocket1_NewConnection (Successful As Boolean, NewSocket As Socket)
  If Successful Then
      Socket1 = NewSocket
      Timer1.Enabled = True
      InputStream1 = Socket1.InputStream
'      OutputStream1 = Socket1.OutputStream
      lbl_connection.Text= "Connected"
      ToastMessageShow("Connected", True)
  Else
      Msgbox(LastException.Message, "Error connecting")
  End If
  ServerSocket1.Listen 'Continue listening to new incoming connections
End Sub

Sub Activity_Resume
  ServerSocket1.Listen
End Sub

Sub Activity_Pause (UserClosed As Boolean)
  If UserClosed Then
      Timer1.Enabled = False
      Socket1.Close
      ServerSocket1.Close 'stop listening
  End If
End Sub

Sub Timer1_Tick
  Dim BC As ByteConverter
 
  If InputStream1.BytesAvailable > 0 Then
      Timer1.Enabled = False
     
      Dim buffer(8192) As Byte
      raf.Initialize3(buffer, True)

      InputStream1.ReadBytes(buffer, 0, buffer.Length)
      txt_recive.Text=(BC.StringFromBytes(buffer,"UTF-8"))
     
  Timer1.Enabled = True
  End If
End Sub

Sub btn_send_Click
    ' what is the code should be here to send string text into the text box in the vb.net
   
Dim c As String = "Test"
Dim UDPSocket1 As UDPSocket
UDPSocket1.Initialize("UDP",  2222 ,65535)
Dim Packet As UDPPacket
Packet.Initialize(c.GetBytes("UTF8"),  "127.0.0.1", 2222)
UDPSocket1.Send(Packet)
   
End Sub



And here my code in vb.net

B4X:
Imports System.Threading
Imports System.Net
Imports System.Net.Sockets
Imports System.Text


Public Class Form1

    Dim ip As IPAddress = IPAddress.Parse("127.0.0.1")
    ' the port must be the same in client ans server
    Dim port As Integer = 2222
    Dim sock As New TcpClient()
    Dim server As New TcpListener(2222)
    Dim client As New TcpClient
    Dim stream As NetworkStream

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try

        ip = IPAddress.Parse(TextBox_ip.Text)
        port = TextBox_port.Text
            sock.Connect(ip, port)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        sock.Client.Send(System.Text.Encoding.UTF8.GetBytes(TextBox1.Text))
    End Sub
    Private Sub Listening()

    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim ListThread As New Thread(New ThreadStart(AddressOf Listening))
        ListThread.Start()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Me.Text = "Waiting...."
        Dim str As String
        server.Start()
        Client = server.AcceptTcpClient
        stream = Client.GetStream()
        Dim r_byt(Client.ReceiveBufferSize) As Byte
        stream.Read(r_byt, 0, Client.ReceiveBufferSize)
        str = Encoding.ASCII.GetString(r_byt)
        TextBox1.Text = str
    End Sub
End Class


see here Timer1_tick this code used to receive the string text from B4A app after sending. What actually happens may be need to create also the connection from B4A to the vb.net then can send string text to the vb.net. Please can modify my code into sides B4A and vb.net.

please help I need it importantly:(:(:(:(
 
Upvote 0

stari

Active Member
Licensed User
Longtime User
VB.NET code for recieve:

B4X:
  Public Sub ReadData(ByVal TcpClient As TcpClient)
        Try
            Dim bytes(10024) As Byte
            Dim Data As String
            Dim FinalString As String
            If TcpClient.Connected Then
                Stream = TcpClient.GetStream()
            Else
                Return
            End If


            If Stream.CanRead = True Then
                If Stream.DataAvailable = True Then
                    Data = Stream.Read(bytes, 0, bytes.Length)
                    FinalString = Encoding.ASCII.GetString(bytes)
                    'ListBox1.Items.Add(FinalString)
                    ListBox1.Items.Add(FinalString)
                    decode_data(FinalString)
                End If
            End If

        Catch ex As SocketException
            'txtRecieve.Text &= "Error: " & ex.Message & Environment.NewLine
            txtLog.Text = "Error: " & ex.Message & Environment.NewLine
        End Try

    End Sub

To send from B4A, after success connection.

Try:
dim podatki as string
Dim buffer() AsByte
buffer = podatki.GetBytes ("ISO-8859-1")
AStreams.Write (buffer)
 
Upvote 0

stari

Active Member
Licensed User
Longtime User
Like this:
B4X:
Sub AStreams_NewData (buffer() As Byte)
    Dim msg As String
    msg = BytesToString(buffer, 0, buffer.Length, "ISO-8859-1")
    Dim msga() As String
    msga = Regex.Split (",",msg)
    ToastMessageShow(msg, True)
End Sub
 
Upvote 0

eng.khalidvb

Member
Licensed User
Longtime User
Thanks, but still I got error and I lose my app maybe some sources damage:(:(

I will upload a new one please if you have time help me . it simple one just explain to me what the code should be in B4A

and what is the code it should be in vb.net .

first of all I want make connection between vb.net to the B4A and it should show connection state label in both side or msgbox.

see this pic:

vb_net.png



see the first text box for IP address
Connect is the connection button
Stop : to stop connection
label4 : to show the connection state

Receive text to receive the text which is send from B4A


here is the second pic B4A

SC20140320_112037.png


connection stat : the connection will show here if it is connect or not

edit text box : here it will be the text which will send it into the vb.net

send text : to send the text to the vb.net



please help me and what is the code will be in both sides in vb.net and B4A

my application vb.net and B4A uploaded plz modify it ..

through this link:
http://www.crocko.com/D87065E8CC074562A4F1CAE4126F1849/vb.net_and_B4A.rar


:(:(:(:(:(

Thanks
 
Upvote 0

stari

Active Member
Licensed User
Longtime User
AStreams, as Erel say, works perfectly. Look in attachment, this work for me also Ok.
 

Attachments

  • comm.zip
    6.5 KB · Views: 647
Upvote 0

eng.khalidvb

Member
Licensed User
Longtime User
I dont understand much. please just make the neccassry change or give me complete example for B4A and vb.net which I uploaded. Just I want to make the connection between vb.net and B4A and then send string text from B4A to vb.net ? that all please give me full example it will be more easy to me to understand and it will decrease my questions :D . please help ?
 
Upvote 0

eng.khalidvb

Member
Licensed User
Longtime User
I know that my friend but I am under pressure its my last course to be graduate from university and there is no time to learn at this moment i need to finish my project before two weeks because of that i need help with ready code. I hope you understand my situation:(:(:(. I need this code be ready fast plz help ...:(:(
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
but I am under pressure its my last course to be graduate from university and there is no time to learn at this moment i need to finish my project before two weeks because of that i need help with ready code.

So i suggest to you "Begin the project earlier and LEARN it earlier!" Like you wrote... It is YOUR graduate, not ours!

I hope you understand my situation:(:(:(

Definetively NO!

I need this code be ready fast plz help ...:(:(

You were TO LAZY to learn in semester and we should do YOUR homework now for you?

Maybe some other will do the work for you. I will not.
 
Upvote 0

eng.khalidvb

Member
Licensed User
Longtime User
thanks for your replay. I am not lazy by other way I will finish it if you help me or not. and I will upload my final project spicily for you to be understand I am not lazy person and I will be better than you in B4A be sure of that. :cool:
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
thanks for your replay. I am not lazy by other way I will finish it if you help me or not. and I will upload my final project spicily for you to be understand I am not lazy person and I will be better than you in B4A be sure of that. :cool:

I dont need a penis-length-check. I know what i can and what i have done coding in the last 25 Years.
Good luck to your graduate
 
Upvote 0

eng.khalidvb

Member
Licensed User
Longtime User
It show me error here unused variable

B4X:
Dim myipn As String

can you create one button on that after click on it should start connect .

one more thing how can i send text from B4A to vb.net

what is the code will be to send string text and what is the code the receive the text from B4A text-box into vb.net text-box

is that possible to do.

Thanks Stari
 
Upvote 0

stari

Active Member
Licensed User
Longtime User
Hm, with this you can communicate PC-Android.
You must connect from VB.NET, Android is server in this case.
IP you cas see on the screen.
 

Attachments

  • comm1.zip
    7.8 KB · Views: 591
Upvote 0
Top