Bug? Connect to server with protocole UDP

Status
Not open for further replies.

zegmano

New Member
Hello,

when I call my server with my library, it works only in Debug mode but crashes in Release mode.
And i added permission in manifest: AddPermission(android.permission.INTERNET)

The problem is on all my phones, they are with the following android version: 8.0, 9.0 and 11.0.
It’s a huawei mate 9 lite, asus zen phone 6 (I believe) and the latest xiaomi mi 10t pro

B4a:
Dim socket As ClientSocket

Dim dataString As String = "{ 'event': '2', 'client': { 'email': 'exemple@gmail.com', 'password': '123456789'} }"  

socket.connectToServer("192.168.104.111", 5000) '<--- Ip, Port
socket.sendString(dataString) '<--- Data to be sent to the server
Log(socket.receiveString) '<--- Receiving data from the server

And my library

My library:
@ShortName("ClientSocket")
@Permissions(values = { "android.permission.INTERNET" })
public class ClientSocket {

    private DatagramSocket l_DSsocket;
    private InetAddress    serverAddress;

    private String l_sHostname = "127.0.0.1";
    private int    l_iPort     = 80;

    /*
     *  Management for connection to the server
     */
    public boolean connectToServer(String l_sHostname, int l_iPort) {
        this.l_iPort          = l_iPort;
        this.l_sHostname      = l_sHostname;

        try {
            this.serverAddress = InetAddress.getByName(l_sHostname);
            this.l_DSsocket        = new DatagramSocket();
        } catch (UnknownHostException e) {
            BA.LogError("ClientSocket: Unable to convert and assign InetAddress.getByName()" + e);
        } catch (SocketException e) {
            BA.LogError("ClientSocket: Unable to assign DatagramSocket()" + e);
        }
        return true;
    }

    public boolean disconnectToServer() throws SocketException {
        l_DSsocket.close();
        l_DSsocket = new DatagramSocket();
        return true;
    }

    /*
     *   Data management
     */
    public boolean sendString(String data) throws IOException, ClassNotFoundException {
        byte[] buf = data.getBytes();
        DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddress, l_iPort);
        try {
            l_DSsocket.send(packet);
        } catch (IOException e) {
            BA.LogError("ClientSocket: Unable to send data of server" + e);
        }

        return true;
    }

    public String receiveString() {
        byte[] size = new byte[256];
        byte[] buf = new byte[0];

        DatagramPacket packetSize = new DatagramPacket(size, size.length, serverAddress, l_iPort);
        try {
            l_DSsocket.receive(packetSize);

            buf = new byte[Integer.parseInt(new String(size).trim())];
            DatagramPacket packetData = new DatagramPacket(buf, buf.length, serverAddress, l_iPort);
            l_DSsocket.receive(packetData);
        } catch (IOException e) {
            BA.LogError("ClientSocket: Unable to receive data of server" + e);
        }

        return new String(buf).trim();
    }

    @ShortName("testForDebug")
    public static class testForDebug {
        public void testfunction_ConnectToServer() throws IOException {
            DatagramSocket client = new DatagramSocket();
            InetAddress add = InetAddress.getByName("192.168.104.111");

            String str = "HelloWorld !!";
            byte[] buf = str.getBytes();
            DatagramPacket packet = new DatagramPacket(buf, buf.length, add, 5000);
            client.send(packet);
            client.close();
        }
    }
}
 
Last edited:
Status
Not open for further replies.
Top