VB.net and B4A

hexc0de

New Member
I'm determined to use VB.net with B4A... i know that b4ppc can easily handle my problems... but i'd so really appreciate with someone could help me with sample vb.net source code for:

1) Transfer of a file from desktop to device
2) sending/receiving strings to/from device and desktop

i know the protocol for file transfer and all... but i'm not exactly able to get it done in vb.net... and i don't know how to send/receive text from desktop->device and device-> desktop....

could someone please take a minute or two to just help me out with actual source code? =)
 
Last edited:

Penko

Active Member
Licensed User
Longtime User
I doubt anyone would post you code unless you show some progress(I don't blame you, just want to make you realize it).

Regarding your question, it's not hard:
- transfer can be done via so many different ways that I maybe can't count them all. But basically, sockets are capable of relaying information. Therefore, what you need is to convert the files to bytes and send them via sockets. You need asynchronious communication as you want to be able to sendToDevice and sendToServer and also receive on both places.

Start here: http://www.b4x.com/forum/basic4android-getting-started-tutorials/7001-android-network-tutorial.html

In VB.NET just create a Socket Listener server and make it listen at the desired port.

You may also check: http://www.b4x.com/forum/basic4android-getting-started-tutorials/7669-asyncstreams-tutorial.html

Agraham's BytesToString library does perfect job for byte-to-string convertion.
 
Last edited:
Upvote 0

hexcode

New Member
Thank you... =)

Ok... i got text transfer from Desktop -> Device... here's the code for those interested....

VB.net

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

Public Class Form2
    Dim ip As IPAddress = IPAddress.Parse("127.0.0.1")
    Dim port As Integer = 10130
    Dim sock As New TcpClient()
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        ip = IPAddress.Parse(TextBox_IP.Text)
        port = TextBox_Port.Text
        sock.Connect(ip, port)

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        sock.Client.Send(System.Text.Encoding.UTF8.GetBytes(TextBox1.Text))
    End Sub
End Class


B4A: (Altered network example)
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 lblSize As Label
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
      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)
       ToastMessageShow(bc.StringFromBytes(buffer,"UTF-8"),True)
   Timer1.Enabled = True
   End If
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…