A M
A M

Reputation: 849

Codename One - Can't read from socket on real device

I'm developing an iOS app, using Codename One. I extended the SocketConnection class, in order to receive data from a server.

class CustomSocketConnection extends SocketConnection {

    private OutputStream os;
    private InputStream is;
    private InputStreamReader isr;
    private String rawMessage;

    public CustomSocketConnection(){
        os = null;
        is = null;
        isr = null;
        rawMessage = null;
    }

    public synchronized String getRawMessage(){
        return rawMessage;
    }

    @Override
    public void connectionError(int errorCode, String message) {
        rawMessage = null;
        ToastBar.showErrorMessage("Error Connecting. ErrorCode: " + errorCode + " Message: " + message);
        System.out.println(">>>CustomSocketConnection, connectionError. Error Connecting. ErrorCode: " + errorCode + " Message: " + message);
    }

    @Override
    public void connectionEstablished(InputStream is, OutputStream os) {
        System.out.println(">>>CustomSocketConnection, connectionEstablished");

        char termination = '\n';

        int length = 1024;
        char[] buffer = new char[length];
        byte[] bufferByte = new byte[length];
        StringBuilder stringBuilder = new StringBuilder();
        System.out.println(">>>CustomSocketConnection, connectionEstablished, prima del while");

        try {
            InputStreamReader isr = new InputStreamReader(is, "UTF-8");
            System.out.println(">>>CustomSocketConnection, connectionEstablished, prima della read");

            while(true){
                System.out.println(">>>CustomSocketConnection, loop di read, inizio");
                int read = isr.read();
                System.out.println(">>>CustomSocketConnection, loop di read, subito dopo la read");
                char c = (char) read;
                if (read == -1) rawMessage = null;

                System.out.println(">>>CustomSocketConnection, loop di read, letto: " + c);

                while(c != termination){
                    stringBuilder.append(c);
                    read = isr.read();
                    c = (char) read;
                    if (read == -1) rawMessage = null;
                }
                rawMessage = stringBuilder.toString();
                if(rawMessage != null) doActions(rawMessage);

                //System.out.println(">>>CustomSocketConnection, connectionEstablished, ho letto: " + rawMessage + "FINE");
            }


        } 
        catch (IOException ex) {
            System.out.println(">>>CustomSocketConnection, connectionEstablished, errore: " + ex.getMessage());
        }

        System.out.println(">>>CustomSocketConnection, connectionEstablished, dopo il while"); 

    }

    private void doActions(String msg){
        //do something

    }
}

In the connectionEstablished method, I read data from server, using the read method of the InputStreamReader class.

If I run the app on the simulator, it works properly and it receives data from server. When I launch the app on real devices (iPad mini, 32-bit device, iOS version 8.1.1 12B435, more details here; iPhone 7s, 64-bit device, iOS version 11.2.5 15D60), the read method doesn't receive data from server. In fact, I can see the string printed by the println before the read method, but I can't see the string printed by the println after the read method.

The issue is not server-side, because I developed an Android app and it receive data from the same server. There aren't firewall restrictions or other network limitations: Android app and the Codename One simulator both receive data when connected on the same local network of the server or from another one.

What's wrong?

Upvotes: 1

Views: 126

Answers (1)

A M
A M

Reputation: 849

Solved using InputStream instead of InputStreamReader.

@Override
public void connectionEstablished(InputStream is, OutputStream os) {
    System.out.println(">>>CustomSocketConnection, connectionEstablished");

    char termination = '\n';

    StringBuilder stringBuilder = new StringBuilder();
    System.out.println(">>>CustomSocketConnection, connectionEstablished, prima del while");

    try {
        System.out.println(">>>CustomSocketConnection, connectionEstablished, prima della read");

        while(true){
            System.out.println(">>>CustomSocketConnection, loop di read, inizio");
            int read = is.read();
            System.out.println(">>>CustomSocketConnection, loop di read, subito dopo la read");
            char c = (char) read;
            if (read == -1) rawMessage = null;

            System.out.println(">>>CustomSocketConnection, loop di read, letto: " + c);

            while(c != termination){
                stringBuilder.append(c);
                read = is.read();
                c = (char) read;
                if (read == -1) rawMessage = null;
            }
            rawMessage = stringBuilder.toString();
            if(rawMessage != null) doActions(rawMessage);

            //System.out.println(">>>CustomSocketConnection, connectionEstablished, ho letto: " + rawMessage + "FINE");
        }


    } 
    catch (IOException ex) {
        System.out.println(">>>CustomSocketConnection, connectionEstablished, errore: " + ex.getMessage());
    }

    System.out.println(">>>CustomSocketConnection, connectionEstablished, dopo il while"); 

}

Can anyone explain me why it's working with InputStream?

Upvotes: 1

Related Questions