Hello
My library for the multicast is too slow.
When I receive a message, the main program slows
Can you help me to improve the code
thank you
My library for the multicast is too slow.
When I receive a message, the main program slows
Can you help me to improve the code
thank you
B4X:
package com.example;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.*;
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,ipaddress as String,len as int)"})
@ShortName("Udpmulticastsocket")
@Version(0.01f)
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)
{
String ipaddress2;
InetAddress ipaddress21 = receivePacket.getAddress();
String ipaddressstr = ipaddress21.toString();
ipaddressstr = ipaddressstr.substring(1, ipaddressstr.length());
byte[] message;
message = receivePacket.getData();
baevent.raiseEventFromDifferentThread(this, null, 0, eventName + "_packetreceived", false, new Object[]{message,ipaddressstr,len});
}
} catch (IOException e) {
}
}
}, 0, 1);
}
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);
}
}