B4J Question Error in MulticastSocket

monki

Active Member
Licensed User
Longtime User
I tried to start this code from this thread https://www.b4x.com/android/forum/threads/multicastsocket.73437/#post-466959 but the error message appears

javac 1.8.0_212
src\b4j\example\b4xmainpage.java:183: error: non-static variable ba cannot be referenced from a static context
udp.init(ba, EventName, 8192, socket);



Code

B4X:
Sub Process_Globals
   Private udp As UDPSocket
End Sub

Sub AppStart (Args() As String)
   CreateMulticastSocket("udp", 1900, "239.255.255.250")
   StartMessageLoop
End Sub

Sub CreateMulticastSocket (EventName As String, Port As Int, Group As String)
   Dim multi As JavaObject
   multi.InitializeNewInstance("java.net.MulticastSocket", Array(Port))
   Dim inet As JavaObject
   multi.RunMethod("joinGroup", Array(inet.InitializeStatic("java.net.InetAddress").RunMethod("getByName", Array(Group))))
   Dim jo As JavaObject = Me
   jo.RunMethod("SetMulticastSocket", Array(EventName, multi, udp))
End Sub

Sub udp_PacketArrived (Packet As UDPPacket)

end sub

#if java
import java.net.*;
public static void SetMulticastSocket (String EventName, MulticastSocket socket, anywheresoftware.b4a.objects.SocketWrapper.UDPSocket udp) {
   udp.init(ba, EventName, 8192, socket);
}
#end if
Why?
 

monki

Active Member
Licensed User
Longtime User
Here is the Project

B4X:
#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip

Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private udp As UDPSocket
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    CreateMulticastSocket("udp", 1900, "239.255.255.250")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
End Sub





Sub CreateMulticastSocket (EventName As String, Port As Int, Group As String)
    Dim multi As JavaObject
    multi.InitializeNewInstance("java.net.MulticastSocket", Array(Port))
    Dim inet As JavaObject
    multi.RunMethod("joinGroup", Array(inet.InitializeStatic("java.net.InetAddress").RunMethod("getByName", Array(Group))))
    Dim jo As JavaObject = Me
    jo.RunMethod("SetMulticastSocket", Array(EventName, multi, udp))
End Sub

#if java
import java.net.*;
public static void SetMulticastSocket (String EventName, MulticastSocket socket, anywheresoftware.b4a.objects.SocketWrapper.UDPSocket udp) {
   udp.init(ba, EventName, 8192, socket);
}
#end if
 
Upvote 0
Top