UDP Help needed

juergen_pool

Member
Licensed User
Longtime User
Hi,

I have a VB.NET program on Windows 7, a jukebox with touch screen operation. This sends via UDP / Broadcast current status information (current title, next ttitle, all "Strings") over LAN. With a background thread receives commands (skip, pause, new title for playlist, also "Strings"). So I can from any client over UDP receive status messages and send commands to control the jukebox. That works great. Now I want the same from my Archos tablet because B4A 1.5 supports UDP. For a first test I have the example of the "Doc for Network 1.10" taken under UDPSocket, adjusted only ports and set IP to 255.255.255.255. Unfortunately, it don't work. Neither I receive any status messages nor my commands received from the jukebox.

Before, I had already done it over TCP, which operated flawlessly under B4A. But I want to switch to UDP, because then I can avoid managing registration and de-registrations of clients.

The examples I've found so far for B4A/UDP use fix IP-Addresses, no Broadcast.
Can someone give me some tip's? Or can i do it under B4A only with fixed IP's?

Sorry for my bad english, i'm using google-tanslate!
 

molder26

Member
Licensed User
Longtime User
Hi juergen_pool, the new version of network library v1.10 support udp and can resolve host name, then you can use dinamic ip.
Why do you set IP to 255.255.255.255? Your lan ip is 255.255.255.0?
Regards
 
Upvote 0

juergen_pool

Member
Licensed User
Longtime User
Hi molder26,

thank you for reply. i'm using Network Library 1.10. All documents i'm read so far say

Destination Address of 255.255.255.255, indicating a network broadcast

and so i use this ip for my UDP-Transfer. All of my Windows-Clients use this special ip and it works. The Clients received all status information from my jukebox and all the commands they sendig are noticed. Only the b4a-aplication receives no packets, and no send packet received by the jukebox.

regards
Jürgen
 
Upvote 0

juergen_pool

Member
Licensed User
Longtime User
here is the code

Hi,

her the b4a-Code:

B4X:
'Activity module
Sub process_globals
    Dim UDPSocket As UDPSocket
   
End Sub

Sub Globals

End Sub
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        UDPSocket.Initialize("UDP", 0,5000)
   
    End If
    Dim Packet As UDPPacket
   
    Dim data() As Byte
   
    data = "Hello from Android".GetBytes("ASCII")
    Packet.Initialize(data, "255.255.255.255", 6000)
   'Packet.Initialize(data, "192.168.178.30", 6000)
    UDPSocket.Send(Packet)

End Sub
Sub UDP_PacketArrived (Packet As UDPPacket)
    Dim msg As String
    msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "ASCII")
    Msgbox("Message received: " & msg, "")
End Sub

and here the methods/subs from my Jukebox-prog. because hole pgm is to big, i have created am small sample based on the original methods/subs:

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


Public Class Form1
    Dim WaitThread As Threading.Thread
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.Show()
        Application.DoEvents()
        WaitThread = New Threading.Thread(AddressOf Wait4Command)
        WaitThread.IsBackground = True
        WaitThread.Start()

    End Sub


    Private Sub ReceiveCommandX(ByVal params As String)
        '// this Sub receives the commands (from Wait4Command)
        TextBox1.Text = params
        ListBox1.Items.Add(params)
    End Sub

    Delegate Sub ReceiveCommand(ByVal params As String)

    Sub Wait4Command()
        Dim udpClient As New UdpClient(5000)
        Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
        Dim receiveBytes As [Byte]()
        Dim returnData As String
        Try
            While True
                receiveBytes = UdpClient.Receive(RemoteIpEndPoint)
                returnData = RemoteIpEndPoint.Address.ToString() & ": " & Encoding.ASCII.GetString(receiveBytes)
                Me.Invoke(New ReceiveCommand(AddressOf ReceiveCommandX), returnData)
            End While
        Catch e As Exception
            MsgBox(e.ToString)
        End Try
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim o As New Object
        o = TextBox2.Text

        Dim s As String = DirectCast(o, String)
        Dim p As New Net.IPEndPoint(Net.IPAddress.Broadcast, 6000)
        Dim u As New UdpClient()
        u.ExclusiveAddressUse = False
        u.EnableBroadcast = True
        u.DontFragment = True
        u.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, True)
        u.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, True)
        u.Client.Bind(New Net.IPEndPoint(Net.IPAddress.Any, 6000))
        Dim b() As Byte = System.Text.Encoding.ASCII.GetBytes(s)
        u.Send(b, b.Length, p)
        u.Close()
    End Sub
End Class

Have edit this, because i had attached the wrong b4a-code. Now it ist last version, also don't work.
 
Last edited:
Upvote 0

juergen_pool

Member
Licensed User
Longtime User
Solved

Sorry, i'm only confused about the example in network.lib Documentation. This will never receive a packet, port for listen not set. i'm edit the buffersize instead.
 
Upvote 0
Top