Android Question Socket1 sends to my computer ?

HarryPottier

Member
Licensed User
I have looked at different versions of "Socket1" here from the forum.
But these "Socket1" do not send a text message to my PC.
Of course this is because I do not understand the procedure.
I'm looking for B4A program code that sends and receives text via the Winsock protocol (Socket1).
Thanks a lot in advance.

With this example Visual Basic (VB6) code I can send and receive text messages between 2 Windows PCs via Winsock.


B4X:
Dim ProperIP as String
ProperIP "192.168.178.1"

Private Sub InitUDP()

    Dim sOwnIP As String
    Dim nPort As Integer    ' UDP-Port

        ' Set UDP protocol
        Winsock1.Protocol = sckUDPProtocol

        ' Port to be monitored
        nPort = 12345


        ' Determine IP
        ProperIP = Winsock1.LocalIP


        ' Bind port to IP
        Winsock1.Bind nPort, sOwnIP

End Sub


Private Sub ipAndroidSend()

    ' Port number at which the data is to be sent
    Winsock1.RemotePort = 12345

    ' Name or IP of the recipient
    Winsock1.RemoteHost = "192.168.178.2"  ' (2) is the second computer

    ' send data
    Winsock1.SendData "Good morning, Hello World"

End Sub


Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim sData As String

      ' The incoming data is received in a variable, (sData)...
      Winsock1.GetData sData

      Label1.Caption = sData

     ' MsgBox sData

End Sub
 

rraswisak

Active Member
Licensed User
Hope this will help you

B4X:
Sub Process_Globals
    Private server As ServerSocket
End Sub

Sub Globals
    Private port As Int = 5614
    Private astream As AsyncStreams
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If (FirstTime = True) Then
        Activity.LoadLayout("Main")
        server.Initialize(port,"server")
    End If
End Sub

Sub btnConnect_Click
    Do While True
        server.Listen
        Wait For server_NewConnection (Successful As Boolean, NewSocket As Socket)
        If Successful Then
                If astream.IsInitialized Then astream.Close
                astream.Initialize(NewSocket.InputStream, NewSocket.OutputStream,"astream")
        End If
    Loop
End Sub

Sub astream_NewData (Buffer() As Byte)
        Dim buff As String = BytesToString(Buffer,0,Buffer.Length,"utf8")
    
        Dim response As String = buff & " message has been received"

        astream.Write(response.GetBytes("utf8"))
End Sub
 
Upvote 0

Makumbi

Well-Known Member
Licensed User
Hope this will help you

B4X:
Sub Process_Globals
    Private server As ServerSocket
End Sub

Sub Globals
    Private port As Int = 5614
    Private astream As AsyncStreams
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If (FirstTime = True) Then
        Activity.LoadLayout("Main")
        server.Initialize(port,"server")
    End If
End Sub

Sub btnConnect_Click
    Do While True
        server.Listen
        Wait For server_NewConnection (Successful As Boolean, NewSocket As Socket)
        If Successful Then
                If astream.IsInitialized Then astream.Close
                astream.Initialize(NewSocket.InputStream, NewSocket.OutputStream,"astream")
        End If
    Loop
End Sub

Sub astream_NewData (Buffer() As Byte)
        Dim buff As String = BytesToString(Buffer,0,Buffer.Length,"utf8")
   
        Dim response As String = buff & " message has been received"

        astream.Write(response.GetBytes("utf8"))
End Sub

how about sending info how can i send using your example
 
Upvote 0

HarryPottier

Member
Licensed User
Excuse me
The program example unfortunately crashes
Error occurred on line: 29 (Main)
29 = Activity.LoadLayout("Main")
furthermore I cannot see if the example sends to a specific IP address
Example: 192.168.178.54
 
Upvote 0

rraswisak

Active Member
Licensed User
First, you need to create a layout and named it as Main (main.bal), in the visual designer add a button and set Name property to btnConnect.

The code act as server-side and when the button was clicked then app will start listening connection from your VB6 app.
You have to know the IP address of android device. (remember both VB6 app and android device must join at the same network).

In the VB6 side, connect to your android device ip address using winsock control and start to send data. Once received, the android will send you back with the respond
B4X:
Dim response As String = buff & " message has been received"

astream.Write(response.GetBytes("utf8"))
 
Upvote 0

HarryPottier

Member
Licensed User
I have 2 laptops with VB6 running a test program with "winsock control".
I can well observe that the two are exchanging

In the sample program I have set the button btnConnect.
When activating the button this line shows no reaction
B4X:
Wait For server_NewConnection (Successful As Boolean, NewSocket As Socket)
no "successful"
 
Upvote 0

HarryPottier

Member
Licensed User
With this "B4A_Network" I get the following error message.
I am surprised that the "from Port" (port 55300) is changing and I have no way to set it.
Shouldn't both ports always be the same?

Failed to connect: (SocketTimeoutException) java.net.SocketTimeoutException:
failed to connect to /192.168.178.54 (port 51042) from /192.168.178.52 (port 55300) after 10000ms
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Are you sure your Firewall (or Router) is not blocking anything?
 
Upvote 0
Top