Java Question Event in java and use in the B4A

electro179

Active Member
Licensed User
Longtime User
Hello

Hello

I can to do a new library in JAVA that sends a packet with a socket UDP. No problem

But how to do that :

Create a JAVA event for the reception and use in the B4A

example :

socket.initialize("socketudp",...)

Sub socketudp_PacketArrived (Packet As UDPPacket)

end sub


Thank you for you help
 

agraham

Expert
Licensed User
Longtime User

electro179

Active Member
Licensed User
Longtime User
hello

I have a little problem to use the BA

It's the first time that I program in JAVA
B4X:
package anywheresoftware.b4a.sample;

import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.DataInputStream;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

@Events(values={"_packetreceived (packet As string)"})

@ShortName("UdpMulticastSocket")


public class UdpMulticastSocket extends Thread{
    //  private String _eventName;
      public int CurrentPort;
      String eventName;
      BA ba;
      MulticastSocket socketMulticast;
      public  void initialize(String _eventName,int newport ,String ipadresse) throws Exception{ 
         CurrentPort = newport;
         InetAddress groupeIP = InetAddress.getByName(ipadresse);
      //   _eventName = eventName;
         socketMulticast = new MulticastSocket(CurrentPort);
         socketMulticast.joinGroup(groupeIP);
         eventName = _eventName;
         this.start();
         // new Recepteur(groupeIP, newport, eventName);
         
      }
      public void send(String Message,String ipadresse,int port) throws IOException
      {
         InetAddress groupeIP = InetAddress.getByName(ipadresse);
         DatagramPacket messagetosend = new DatagramPacket(Message.getBytes(), Message.length(),groupeIP , port);
         socketMulticast.send(messagetosend);      
      }

         @Override
         public void run() {
             DatagramPacket message;
             byte[] contenuMessage;
             String texte = "sls";
            // ByteArrayInputStream lecteur;
             boolean x = false;
             while(true) {
                 contenuMessage = new byte[1024];
                 message = new DatagramPacket(contenuMessage, contenuMessage.length);
                 try {
                    socketMulticast.receive(message);
                          texte = (new DataInputStream(new ByteArrayInputStream(contenuMessage))).readUTF();
                
                   ba.raiseEvent(?????????, eventName + "_packetreceived", new Object[]{texte});
                 }
                 catch(Exception exc) {
                      
                 }
             }
           
        }

   }

I don't know what I must write here
ba.raiseEvent( ????????, eventName + "_packetreceived", new Object[]{texte});

thank you
 

electro179

Active Member
Licensed User
Longtime User
I tried with that. I have not error but it does work

B4X:
 public void run(final BA ba) {
             DatagramPacket message;
             byte[] contenuMessage;
             String texte = "sls";
            // ByteArrayInputStream lecteur;
             boolean x = false;
             ba.raiseEvent(this, eventName + "_packetreceived", new Object[]{texte});
             while(true) {
                 contenuMessage = new byte[1024];
                 message = new DatagramPacket(contenuMessage, contenuMessage.length);
                 try {
                    socketMulticast.receive(message);
                          texte = (new DataInputStream(new ByteArrayInputStream(contenuMessage))).readUTF();
               //    if (!texte.startsWith(nom)) continue;
                   ba.raiseEvent(this, eventName + "_packetreceived", new Object[]{texte});
                 }
                 catch(Exception exc) {
                      
                 }
             }
           
        }
 

warwound

Expert
Licensed User
Longtime User
You're raising (or trying!) an event from a different thread so you need to use raiseEventFromDifferentThread instead of raiseEvent.

Search the forum for 'raiseEventFromDifferentThread' and you'll find some examples.

Martin.
 

electro179

Active Member
Licensed User
Longtime User
Mu program crashs when I receive a multicast packet

Without this line none problem
baevent.raiseEventFromDifferentThread(this, null, 0, eventName + "_packetreceived", false, null);


B4X:
package anywheresoftware.b4a.sample;

import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.ShortName;
import java.io.IOException;

import java.net.*;

import java.util.Timer;
import java.util.TimerTask;


@Events(values={"_packetreceived"})

@ShortName("UdpMulticastSocket")


public class UdpMulticastSocket {
    //  private String _eventName;
         public int CurrentPort;
         int len;
         String eventName;
         MulticastSocket serverSocket;
         byte[] receiveData = new byte[1024];
         byte[] sendData = new byte[1024];
         BA baevent;
         public  void initialize(String _eventName,int newport ,String ipadresse) throws Exception
         { 
   
            InetAddress groupeIP = InetAddress.getByName(ipadresse);
            serverSocket = new MulticastSocket(newport);
            serverSocket.joinGroup(groupeIP);
            eventName = _eventName;
             Timer timer = new Timer();
              timer.schedule(new TimerTask() {
                  public void run()
                  {
                      byte[] receiveData = new byte[1024];
                     try {
                         DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                      serverSocket.receive(receivePacket);
                     // String sentence = new String( receivePacket.getData());
                     
                      len = receivePacket.getLength();
                      if (len > 0)
                      {
                          baevent.raiseEventFromDifferentThread(this, null, 0, eventName + "_packetreceived", false, null);
                         
                         if (len == 106-42)
                            sendstatustype1("239.255.36.93",receivePacket.getPort());
                         if (len == 91-42)
                            sendstatustype1("239.255.36.93",receivePacket.getPort());
                         else if (len == 90-42)
                            sendstatustype2("239.255.36.93",3694);
                         else if (len == 78-42)
                            sendstatustype1("239.255.36.93",receivePacket.getPort());   

                      }
                  } catch (IOException e) {
                     
                  } 
                  }
              }, 0, 1);
           /*
             while(true)
             {
                     
             }*/

      }
         int compt = 0;
         int comptid = 0x10;
      public void send(String Message,String ipadresse,int port) throws IOException
      {

         InetAddress groupeIP = InetAddress.getByName(ipadresse);
         DatagramPacket messagetosend = new DatagramPacket(Message.getBytes(), Message.length(),groupeIP , port);
         serverSocket.send(messagetosend);   
      }
      public void sendstatustype1(String ipadresse,int port) throws IOException
      {
         byte[]  message = new byte[1024];
         int compt = 0;
         
         message[compt] = (byte)0x41; compt++;
         message[compt] = (byte)0x44; compt++;
         message[compt] = (byte)0x4e; compt++;
         message[compt] = (byte)0x2d; compt++;
         message[compt] = (byte)0x4e; compt++;
         message[compt] = (byte)0x45; compt++;
         
         message[compt] = (byte)0x54; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x02; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte) comptid++; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x08; compt++;
         message[compt] = (byte)0x02; compt++;
         
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte) 0xff; compt++;
         message[compt] = (byte) 0x89; compt++;
         message[compt] = (byte) 0xbc; compt++;
         message[compt] = (byte)0x77; compt++;
         message[compt] = (byte)0x37; compt++;
         message[compt] = (byte) 0xdd; compt++;
         message[compt] = (byte) 0xf7; compt++;
         message[compt] = (byte)0x01; compt++;
         message[compt] = (byte)0x45; compt++;
         message[compt] = (byte)0x44; compt++;
         message[compt] = (byte)0x54; compt++;
         message[compt] = (byte)0x54; compt++;
         message[compt] = (byte)0x36; compt++;
         message[compt] = (byte)0x32; compt++;

         message[compt] = (byte)0x30; compt++;
         message[compt] = (byte)0x30; compt++;
         message[compt] = (byte) 0xc0; compt++;
         message[compt] = (byte) 0xa8; compt++;
         message[compt] = (byte)0x01; compt++;
         message[compt] = (byte)0x63; compt++;
         message[compt] = (byte)0x0e; compt++;
         message[compt] = (byte)0x6e; compt++;
         message[compt] = (byte)0x75; compt++;
         message[compt] = (byte)0x45; compt++;
         message[compt] = (byte) 0x8b; compt++;
         message[compt] = (byte)0x10; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         
         message[compt] = (byte) 0x01; compt++;
         message[compt] = (byte) 0x02; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x01; compt++;
         message[compt] = (byte)0x16; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x01; compt++;
         message[compt] = (byte)0x18; compt++;
         message[compt] = (byte)0x02; compt++;
         message[compt] =(byte) 0x06; compt++;
         
         
         
         
   
         InetAddress groupeIP = InetAddress.getByName(ipadresse);
         DatagramPacket messagetosend = new DatagramPacket(message, compt,groupeIP , port);
         serverSocket.send(messagetosend);   
         
         
      }
      
      public void RequestStatus() throws IOException
      {
         String ipadresse = "239.255.36.93";
         int port = 3693;
         byte[]  message = new byte[1024];
         int compt = 0;
         message[compt] = (byte)0x41; compt++;
            message[compt] = (byte)0x44; compt++;
            message[compt] = (byte)0x4e; compt++;
            message[compt] = (byte)0x2d; compt++;
            message[compt] = (byte)0x4e; compt++;
            message[compt] = (byte)0x45; compt++;
            
            message[compt] = (byte)0x54; compt++;
            message[compt] = (byte)0x00; compt++;
            message[compt] = (byte)0x02; compt++;
            message[compt] = (byte)0x00; compt++;
            message[compt] = (byte)0x00; compt++;
            message[compt] = (byte)0x00; compt++;
            message[compt] = (byte) 0xB6; compt++;
            message[compt] = (byte)0x06; compt++;
            message[compt] = (byte)0x00; compt++;
            message[compt] = (byte)0x00; compt++;
            message[compt] = (byte)0xff; compt++;
            message[compt] = (byte)0x89; compt++;
            message[compt] = (byte)0xbc; compt++;
            message[compt] = (byte)0x77; compt++;
            message[compt] = (byte)0x37; compt++;
            message[compt] = (byte)0xdd; compt++;
            
            message[compt] = (byte)0xf7; compt++;
            message[compt] = (byte)0x01; compt++;
            message[compt] = (byte) 0xff; compt++;
            message[compt] = (byte) 0xff; compt++;
            message[compt] = (byte) 0xff; compt++;
            message[compt] = (byte)0xff; compt++;
            message[compt] = (byte)0xff; compt++;
            message[compt] = (byte) 0xff; compt++;
            message[compt] = (byte) 0xff; compt++;
            message[compt] = (byte)0xff; compt++;
            message[compt] = (byte)0x13; compt++;
            message[compt] = (byte)0xfd; compt++;
            message[compt] = (byte)0x61; compt++;
            message[compt] = (byte)0xf2; compt++;
            InetAddress groupeIP2 = InetAddress.getByName(ipadresse);
            DatagramPacket messagetosend2 = new DatagramPacket(message, compt,groupeIP2 , port);
            serverSocket.send(messagetosend2);
      }
      
   public void sendstatustype2(String ipadresse,int port) throws IOException
      {
         byte[]  message = new byte[1024];
         int compt = 0;
                  
         
         compt = 0;
         message[compt] = (byte)0x41; compt++;
         message[compt] = (byte)0x44; compt++;
         message[compt] = (byte)0x4e; compt++;
         message[compt] = (byte)0x2d; compt++;
         message[compt] = (byte)0x4e; compt++;
         message[compt] = (byte)0x45; compt++;
         
         message[compt] = (byte)0x54; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x02; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte) comptid++; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x08; compt++;
         message[compt] = (byte)0x02; compt++;
         
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte) 0xff; compt++;
         message[compt] = (byte) 0x89; compt++;
         message[compt] = (byte) 0xbc; compt++;
         message[compt] = (byte)0x77; compt++;
         message[compt] = (byte)0x37; compt++;
         message[compt] = (byte) 0xdd; compt++;
         message[compt] = (byte) 0xf7; compt++;
         message[compt] = (byte)0x01; compt++;
         message[compt] = (byte)0xFe; compt++;
         message[compt] = (byte)0xfd; compt++;
         message[compt] = (byte)0xfa; compt++;
         message[compt] = (byte)0x01; compt++;
         message[compt] = (byte)0x11; compt++;
         message[compt] = (byte)0x00; compt++;

         message[compt] = (byte)0x04; compt++;
         message[compt] = (byte)0x55; compt++;
         message[compt] = (byte) 0x00; compt++;
         message[compt] = (byte) 0x04; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x07; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x14; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte) 0x0d; compt++;
         message[compt] = (byte)0x36; compt++;
         message[compt] = (byte)0x32; compt++;
         message[compt] = (byte)0x30; compt++;
         message[compt] = (byte)0x30; compt++;
         message[compt] = (byte)0x45; compt++;
         
         message[compt] = (byte) 0x44; compt++;
         message[compt] = (byte) 054; compt++;
         message[compt] = (byte)0x54; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x0; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0xec; compt++;
         message[compt] =(byte) 0x3a; compt++;
         
         InetAddress groupeIP2 = InetAddress.getByName(ipadresse);
         DatagramPacket messagetosend2 = new DatagramPacket(message, compt,groupeIP2 , port);
         serverSocket.send(messagetosend2);
         
      }
     
   }

I have not idea and you thank you ?

thank you
 
Last edited:

electro179

Active Member
Licensed User
Longtime User
The log

B4X:
Installing file.
PackageAdded: package:artnetconsole.artnet
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.lang.NullPointerException
   at anywheresoftware.b4a.sample.UdpMulticastSocket$1.run(UdpMulticastSocket.java:48)
   at java.util.Timer$TimerImpl.run(Timer.java:284)
java.lang.NullPointerException
   at anywheresoftware.b4a.sample.UdpMulticastSocket$1.run(UdpMulticastSocket.java:48)
   at java.util.Timer$TimerImpl.run(Timer.java:284)
 

electro179

Active Member
Licensed User
Longtime User
It's working without parameter

but with a simple parameter I have a problem

All is made in same module (creating and event)
LOG
B4X:
Installing file.
PackageAdded: package:artnetconsole.artnet
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.lang.Exception: Sub socketinput_packetreceived(result As Int) was not found.
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:173)
   at anywheresoftware.b4a.BA$3.run(BA.java:303)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:130)
   at android.app.ActivityThread.main(ActivityThread.java:3691)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
   at dalvik.system.NativeStart.main(Native Method)
java.lang.Exception: Sub socketinput_packetreceived(result As Int) was not found.

Basic code
B4X:
Sub socketinput_packetreceived(result As Int)
    ToastMessageShow("received Multicast ",True)
End Sub

JAVA CODE
B4X:
package anywheresoftware.b4a.sample;

import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.ShortName;
import java.io.IOException;

import java.net.*;

import java.util.Timer;
import java.util.TimerTask;


@Events(values={"packetreceived (result As Int)"})

@ShortName("UdpMulticastSocket")


public class UdpMulticastSocket {
    //  private String _eventName;
         public int CurrentPort;
         int len;
         String eventName;
         MulticastSocket serverSocket;
         byte[] receiveData = new byte[1024];
         byte[] sendData = new byte[1024];
         BA baevent;
         public  void initialize(BA ba,String _eventName,int newport ,String ipadresse) throws Exception
         { 
            baevent = ba; 
            InetAddress groupeIP = InetAddress.getByName(ipadresse);
            serverSocket = new MulticastSocket(newport);
            serverSocket.joinGroup(groupeIP);
            eventName = _eventName;
             Timer timer = new Timer();
              timer.schedule(new TimerTask() {
               public void run()
                  {
                     
                     byte[] receiveData = new byte[1024];
                     try {
                         DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                      serverSocket.receive(receivePacket);
                     // String sentence = new String( receivePacket.getData());
                     
                      len = receivePacket.getLength();
                      if (len > 0)
                      {
                         int result;
                         result = 15;
                         baevent.raiseEventFromDifferentThread(this, null, 0, eventName + "_packetreceived(result As Int)", false, new Object[]{result});
                        // baevent.raiseEvent(this, eventName + "_packetreceived (result As Int)", new Object[]{result});
                         /* 
                         if (len == 106-42)
                            sendstatustype1("239.255.36.93",receivePacket.getPort());
                         if (len == 91-42)
                            sendstatustype1("239.255.36.93",receivePacket.getPort());
                         else if (len == 90-42)
                            sendstatustype2("239.255.36.93",3694);
                         else if (len == 78-42)
                            sendstatustype1("239.255.36.93",receivePacket.getPort());   */

                      }
                  } catch (IOException e) {
                     
                  } 
                  }

              }, 0, 1);
           /*
             while(true)
             {
                     
             }*/

      }
         int compt = 0;
         int comptid = 0x10;
      public void send(String Message,String ipadresse,int port) throws IOException
      {

         InetAddress groupeIP = InetAddress.getByName(ipadresse);
         DatagramPacket messagetosend = new DatagramPacket(Message.getBytes(), Message.length(),groupeIP , port);
         serverSocket.send(messagetosend);   
      }
      public void sendstatustype1(String ipadresse,int port) throws IOException
      {
         byte[]  message = new byte[1024];
         int compt = 0;
         
         message[compt] = (byte)0x41; compt++;
         message[compt] = (byte)0x44; compt++;
         message[compt] = (byte)0x4e; compt++;
         message[compt] = (byte)0x2d; compt++;
         message[compt] = (byte)0x4e; compt++;
         message[compt] = (byte)0x45; compt++;
         
         message[compt] = (byte)0x54; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x02; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte) comptid++; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x08; compt++;
         message[compt] = (byte)0x02; compt++;
         
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte) 0xff; compt++;
         message[compt] = (byte) 0x89; compt++;
         message[compt] = (byte) 0xbc; compt++;
         message[compt] = (byte)0x77; compt++;
         message[compt] = (byte)0x37; compt++;
         message[compt] = (byte) 0xdd; compt++;
         message[compt] = (byte) 0xf7; compt++;
         message[compt] = (byte)0x01; compt++;
         message[compt] = (byte)0x45; compt++;
         message[compt] = (byte)0x44; compt++;
         message[compt] = (byte)0x54; compt++;
         message[compt] = (byte)0x54; compt++;
         message[compt] = (byte)0x36; compt++;
         message[compt] = (byte)0x32; compt++;

         message[compt] = (byte)0x30; compt++;
         message[compt] = (byte)0x30; compt++;
         message[compt] = (byte) 0xc0; compt++;
         message[compt] = (byte) 0xa8; compt++;
         message[compt] = (byte)0x01; compt++;
         message[compt] = (byte)0x63; compt++;
         message[compt] = (byte)0x0e; compt++;
         message[compt] = (byte)0x6e; compt++;
         message[compt] = (byte)0x75; compt++;
         message[compt] = (byte)0x45; compt++;
         message[compt] = (byte) 0x8b; compt++;
         message[compt] = (byte)0x10; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         
         message[compt] = (byte) 0x01; compt++;
         message[compt] = (byte) 0x02; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x01; compt++;
         message[compt] = (byte)0x16; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x01; compt++;
         message[compt] = (byte)0x18; compt++;
         message[compt] = (byte)0x02; compt++;
         message[compt] =(byte) 0x06; compt++;
         
         
         
         
   
         InetAddress groupeIP = InetAddress.getByName(ipadresse);
         DatagramPacket messagetosend = new DatagramPacket(message, compt,groupeIP , port);
         serverSocket.send(messagetosend);   
         
         
      }
      
      public void RequestStatus() throws IOException
      {
         String ipadresse = "239.255.36.93";
         int port = 3693;
         byte[]  message = new byte[1024];
         int compt = 0;
         message[compt] = (byte)0x41; compt++;
            message[compt] = (byte)0x44; compt++;
            message[compt] = (byte)0x4e; compt++;
            message[compt] = (byte)0x2d; compt++;
            message[compt] = (byte)0x4e; compt++;
            message[compt] = (byte)0x45; compt++;
            
            message[compt] = (byte)0x54; compt++;
            message[compt] = (byte)0x00; compt++;
            message[compt] = (byte)0x02; compt++;
            message[compt] = (byte)0x00; compt++;
            message[compt] = (byte)0x00; compt++;
            message[compt] = (byte)0x00; compt++;
            message[compt] = (byte) 0xB6; compt++;
            message[compt] = (byte)0x06; compt++;
            message[compt] = (byte)0x00; compt++;
            message[compt] = (byte)0x00; compt++;
            message[compt] = (byte)0xff; compt++;
            message[compt] = (byte)0x89; compt++;
            message[compt] = (byte)0xbc; compt++;
            message[compt] = (byte)0x77; compt++;
            message[compt] = (byte)0x37; compt++;
            message[compt] = (byte)0xdd; compt++;
            
            message[compt] = (byte)0xf7; compt++;
            message[compt] = (byte)0x01; compt++;
            message[compt] = (byte) 0xff; compt++;
            message[compt] = (byte) 0xff; compt++;
            message[compt] = (byte) 0xff; compt++;
            message[compt] = (byte)0xff; compt++;
            message[compt] = (byte)0xff; compt++;
            message[compt] = (byte) 0xff; compt++;
            message[compt] = (byte) 0xff; compt++;
            message[compt] = (byte)0xff; compt++;
            message[compt] = (byte)0x13; compt++;
            message[compt] = (byte)0xfd; compt++;
            message[compt] = (byte)0x61; compt++;
            message[compt] = (byte)0xf2; compt++;
            InetAddress groupeIP2 = InetAddress.getByName(ipadresse);
            DatagramPacket messagetosend2 = new DatagramPacket(message, compt,groupeIP2 , port);
            serverSocket.send(messagetosend2);
      }
      
   public void sendstatustype2(String ipadresse,int port) throws IOException
      {
         byte[]  message = new byte[1024];
         int compt = 0;
                  
         
         compt = 0;
         message[compt] = (byte)0x41; compt++;
         message[compt] = (byte)0x44; compt++;
         message[compt] = (byte)0x4e; compt++;
         message[compt] = (byte)0x2d; compt++;
         message[compt] = (byte)0x4e; compt++;
         message[compt] = (byte)0x45; compt++;
         
         message[compt] = (byte)0x54; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x02; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte) comptid++; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x08; compt++;
         message[compt] = (byte)0x02; compt++;
         
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte) 0xff; compt++;
         message[compt] = (byte) 0x89; compt++;
         message[compt] = (byte) 0xbc; compt++;
         message[compt] = (byte)0x77; compt++;
         message[compt] = (byte)0x37; compt++;
         message[compt] = (byte) 0xdd; compt++;
         message[compt] = (byte) 0xf7; compt++;
         message[compt] = (byte)0x01; compt++;
         message[compt] = (byte)0xFe; compt++;
         message[compt] = (byte)0xfd; compt++;
         message[compt] = (byte)0xfa; compt++;
         message[compt] = (byte)0x01; compt++;
         message[compt] = (byte)0x11; compt++;
         message[compt] = (byte)0x00; compt++;

         message[compt] = (byte)0x04; compt++;
         message[compt] = (byte)0x55; compt++;
         message[compt] = (byte) 0x00; compt++;
         message[compt] = (byte) 0x04; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x07; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x14; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte) 0x0d; compt++;
         message[compt] = (byte)0x36; compt++;
         message[compt] = (byte)0x32; compt++;
         message[compt] = (byte)0x30; compt++;
         message[compt] = (byte)0x30; compt++;
         message[compt] = (byte)0x45; compt++;
         
         message[compt] = (byte) 0x44; compt++;
         message[compt] = (byte) 054; compt++;
         message[compt] = (byte)0x54; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x0; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0xec; compt++;
         message[compt] =(byte) 0x3a; compt++;
         
         InetAddress groupeIP2 = InetAddress.getByName(ipadresse);
         DatagramPacket messagetosend2 = new DatagramPacket(message, compt,groupeIP2 , port);
         serverSocket.send(messagetosend2);
         
      }
     
   }

thank you for you help
 
Last edited:

warwound

Expert
Licensed User
Longtime User
You are passing the wrong String as the event name:

B4X:
baevent.raiseEventFromDifferentThread(this, null, 0, eventName + "_packetreceived(result As Int)", false, new Object[]{result});

Should be (i think):

B4X:
baevent.raiseEventFromDifferentThread(this, null, 0, eventName + "_packetreceived", false, new Object[]{result});

Try removing (result As Int) from the event name.

Martin.
 

electro179

Active Member
Licensed User
Longtime User
yes, it's working


when I use a int, there are not problem
but with byte, I have an error :
signature does not match expected signature



JAVA
B4X:
package anywheresoftware.b4a.sample;

import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.ShortName;
import java.io.IOException;
import java.io.InputStream;

import java.net.*;

import java.util.Timer;
import java.util.TimerTask;


@Events(values={"packetreceived (message As Byte)"})

@ShortName("UdpMulticastSocket")


public class UdpMulticastSocket {
    //  private String _eventName;
         public int CurrentPort;
         int len;
         String eventName;
         MulticastSocket serverSocket;
         byte[] receiveData = new byte[1024];
         byte[] sendData = new byte[1024];
         BA baevent;
         public  void initialize(BA ba,String _eventName,int newport ,String ipadresse) throws Exception
         { 
            baevent = ba; 
            InetAddress groupeIP = InetAddress.getByName(ipadresse);
            serverSocket = new MulticastSocket(newport);
            serverSocket.joinGroup(groupeIP);
            eventName = _eventName;
             Timer timer = new Timer();
              timer.schedule(new TimerTask() {
               public void run()
                  {
                     
                     byte[] receiveData = new byte[1024];
                     try {
                         DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                      byte[] input = {80,81};
                         serverSocket.receive(receivePacket);
                     // String sentence = new String( receivePacket.getData());
                     
                      len = receivePacket.getLength();
                      if (len > 0)
                      {
                         int result;
                         String messagestring;
                         byte[] message;
                         result = 15;
                         message = receivePacket.getData();
                         baevent.raiseEventFromDifferentThread(this, null, 0, eventName + "_packetreceived", false, new Object[]{message});
                        

                      }
                  } catch (IOException e) {
                     
                  } 
                  }

              }, 0, 1);
           /*
             while(true)
             {
                     
             }*/

      }
         int compt = 0;
         int comptid = 0x10;
      public void send(String Message,String ipadresse,int port) throws IOException
      {

         InetAddress groupeIP = InetAddress.getByName(ipadresse);
         DatagramPacket messagetosend = new DatagramPacket(Message.getBytes(), Message.length(),groupeIP , port);
         serverSocket.send(messagetosend);   
      }
      public void sendstatustype1(String ipadresse,int port) throws IOException
      {
         byte[]  message = new byte[1024];
         int compt = 0;
         
         message[compt] = (byte)0x41; compt++;
         message[compt] = (byte)0x44; compt++;
         message[compt] = (byte)0x4e; compt++;
         message[compt] = (byte)0x2d; compt++;
         message[compt] = (byte)0x4e; compt++;
         message[compt] = (byte)0x45; compt++;
         
         message[compt] = (byte)0x54; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x02; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte) comptid++; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x08; compt++;
         message[compt] = (byte)0x02; compt++;
         
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte) 0xff; compt++;
         message[compt] = (byte) 0x89; compt++;
         message[compt] = (byte) 0xbc; compt++;
         message[compt] = (byte)0x77; compt++;
         message[compt] = (byte)0x37; compt++;
         message[compt] = (byte) 0xdd; compt++;
         message[compt] = (byte) 0xf7; compt++;
         message[compt] = (byte)0x01; compt++;
         message[compt] = (byte)0x45; compt++;
         message[compt] = (byte)0x44; compt++;
         message[compt] = (byte)0x54; compt++;
         message[compt] = (byte)0x54; compt++;
         message[compt] = (byte)0x36; compt++;
         message[compt] = (byte)0x32; compt++;

         message[compt] = (byte)0x30; compt++;
         message[compt] = (byte)0x30; compt++;
         message[compt] = (byte) 0xc0; compt++;
         message[compt] = (byte) 0xa8; compt++;
         message[compt] = (byte)0x01; compt++;
         message[compt] = (byte)0x63; compt++;
         message[compt] = (byte)0x0e; compt++;
         message[compt] = (byte)0x6e; compt++;
         message[compt] = (byte)0x75; compt++;
         message[compt] = (byte)0x45; compt++;
         message[compt] = (byte) 0x8b; compt++;
         message[compt] = (byte)0x10; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         
         message[compt] = (byte) 0x01; compt++;
         message[compt] = (byte) 0x02; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x01; compt++;
         message[compt] = (byte)0x16; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x01; compt++;
         message[compt] = (byte)0x18; compt++;
         message[compt] = (byte)0x02; compt++;
         message[compt] =(byte) 0x06; compt++;
         
         
         
         
   
         InetAddress groupeIP = InetAddress.getByName(ipadresse);
         DatagramPacket messagetosend = new DatagramPacket(message, compt,groupeIP , port);
         serverSocket.send(messagetosend);   
         
         
      }
      
      public void RequestStatus() throws IOException
      {
         String ipadresse = "239.255.36.93";
         int port = 3693;
         byte[]  message = new byte[1024];
         int compt = 0;
         message[compt] = (byte)0x41; compt++;
            message[compt] = (byte)0x44; compt++;
            message[compt] = (byte)0x4e; compt++;
            message[compt] = (byte)0x2d; compt++;
            message[compt] = (byte)0x4e; compt++;
            message[compt] = (byte)0x45; compt++;
            
            message[compt] = (byte)0x54; compt++;
            message[compt] = (byte)0x00; compt++;
            message[compt] = (byte)0x02; compt++;
            message[compt] = (byte)0x00; compt++;
            message[compt] = (byte)0x00; compt++;
            message[compt] = (byte)0x00; compt++;
            message[compt] = (byte) 0xB6; compt++;
            message[compt] = (byte)0x06; compt++;
            message[compt] = (byte)0x00; compt++;
            message[compt] = (byte)0x00; compt++;
            message[compt] = (byte)0xff; compt++;
            message[compt] = (byte)0x89; compt++;
            message[compt] = (byte)0xbc; compt++;
            message[compt] = (byte)0x77; compt++;
            message[compt] = (byte)0x37; compt++;
            message[compt] = (byte)0xdd; compt++;
            
            message[compt] = (byte)0xf7; compt++;
            message[compt] = (byte)0x01; compt++;
            message[compt] = (byte) 0xff; compt++;
            message[compt] = (byte) 0xff; compt++;
            message[compt] = (byte) 0xff; compt++;
            message[compt] = (byte)0xff; compt++;
            message[compt] = (byte)0xff; compt++;
            message[compt] = (byte) 0xff; compt++;
            message[compt] = (byte) 0xff; compt++;
            message[compt] = (byte)0xff; compt++;
            message[compt] = (byte)0x13; compt++;
            message[compt] = (byte)0xfd; compt++;
            message[compt] = (byte)0x61; compt++;
            message[compt] = (byte)0xf2; compt++;
            InetAddress groupeIP2 = InetAddress.getByName(ipadresse);
            DatagramPacket messagetosend2 = new DatagramPacket(message, compt,groupeIP2 , port);
            serverSocket.send(messagetosend2);
      }
      
   public void sendstatustype2(String ipadresse,int port) throws IOException
      {
         byte[]  message = new byte[1024];
         int compt = 0;
                  
         
         compt = 0;
         message[compt] = (byte)0x41; compt++;
         message[compt] = (byte)0x44; compt++;
         message[compt] = (byte)0x4e; compt++;
         message[compt] = (byte)0x2d; compt++;
         message[compt] = (byte)0x4e; compt++;
         message[compt] = (byte)0x45; compt++;
         
         message[compt] = (byte)0x54; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x02; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte) comptid++; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x08; compt++;
         message[compt] = (byte)0x02; compt++;
         
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte) 0xff; compt++;
         message[compt] = (byte) 0x89; compt++;
         message[compt] = (byte) 0xbc; compt++;
         message[compt] = (byte)0x77; compt++;
         message[compt] = (byte)0x37; compt++;
         message[compt] = (byte) 0xdd; compt++;
         message[compt] = (byte) 0xf7; compt++;
         message[compt] = (byte)0x01; compt++;
         message[compt] = (byte)0xFe; compt++;
         message[compt] = (byte)0xfd; compt++;
         message[compt] = (byte)0xfa; compt++;
         message[compt] = (byte)0x01; compt++;
         message[compt] = (byte)0x11; compt++;
         message[compt] = (byte)0x00; compt++;

         message[compt] = (byte)0x04; compt++;
         message[compt] = (byte)0x55; compt++;
         message[compt] = (byte) 0x00; compt++;
         message[compt] = (byte) 0x04; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x07; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x14; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte) 0x0d; compt++;
         message[compt] = (byte)0x36; compt++;
         message[compt] = (byte)0x32; compt++;
         message[compt] = (byte)0x30; compt++;
         message[compt] = (byte)0x30; compt++;
         message[compt] = (byte)0x45; compt++;
         
         message[compt] = (byte) 0x44; compt++;
         message[compt] = (byte) 054; compt++;
         message[compt] = (byte)0x54; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x0; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0x00; compt++;
         message[compt] = (byte)0xec; compt++;
         message[compt] =(byte) 0x3a; compt++;
         
         InetAddress groupeIP2 = InetAddress.getByName(ipadresse);
         DatagramPacket messagetosend2 = new DatagramPacket(message, compt,groupeIP2 , port);
         serverSocket.send(messagetosend2);
         
      }
     
   }

BASIC CODE
B4X:
Sub socketinput_packetreceived(message As Byte)
    ToastMessageShow("received Multicast port,True)
   
End Sub
 

warwound

Expert
Licensed User
Longtime User
Update the Events annotation from:

B4X:
@Events(values={"packetreceived (message As Byte)"})

To:

B4X:
@Events(values={"packetreceived (message() As Byte)"})

You're passing an array of Byte not a single Byte.
Now your b4a Sub should be declared:

B4X:
Sub socketinput_packetreceived(message() As Byte)
     ToastMessageShow("received Multicast port,True)
End Sub

Martin.
 

agraham

Expert
Licensed User
Longtime User
About the timer. In general I think if you want to do repetitive stuff it is better done by a Timer in the B4A code so the programmer has more control over what is happening and when. However I see that DatagramSocket.Received is a blocking call. A Timer does seem to be a valid solution however bear in mind this - my boldening
When a timer is no longer needed, users should call cancel(), which releases the timer's thread and other resources. Timers not explicitly cancelled may hold resources indefinitely.

You might want to consider starting your own Thread or using one from the Internal thread pool
 

electro179

Active Member
Licensed User
Longtime User
thank you
I receive one message whenever to launch void initialize(....)

run() is launched all the time or once ?


B4X:
public  void initialize(BA ba,String _eventName,int newport ,String ipadresse) throws Exception
         { 
            baevent = ba; 
            InetAddress groupeIP = InetAddress.getByName(ipadresse);
            serverSocket = new MulticastSocket(newport);
            serverSocket.joinGroup(groupeIP);
            eventName = _eventName;
            
            
            Runnable r = new Runnable() {

               @Override
                public void run() {
                    try {
                       DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                       serverSocket.receive(receivePacket);
                     // String sentence = new String( receivePacket.getData());
                     
                      len = receivePacket.getLength();
                      if (len > 0)
                      {

                         byte[] message;
                         message = receivePacket.getData();
                         baevent.raiseEventFromDifferentThread(this, null, 0, eventName + "_packetreceived", false, new Object[]{message});
         
                      }
                       } catch (Exception e) 
                       {
                      }
                    }
                };  
              BA.submitRunnable(r, this,0);
           
            
            
            
           /*
             while(true)
             {
                     
             }*/

      }

thank you
 
Last edited:

electro179

Active Member
Licensed User
Longtime User
Once every time you submit it.

this line : BA.submitRunnable(r, this,0); relaunchs the run() and howeve I send 10 messages to android I receive 1 message.
I initalize again I can receive ONE new message

thank you for you help
 
Last edited:
Top