ios - 代号一 - 无法从真实设备上的套接字读取

标签 ios sockets ios8 codenameone ios11

我正在使用 Codename One 开发一个 iOS 应用程序。我扩展了 SocketConnection 类,以便从服务器接收数据。

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

    }
}

connectionEstablished 方法中,我使用 InputStreamReader 类的 read 方法从服务器读取数据。

如果我在模拟器上运行该应用程序,它会正常工作并从服务器接收数据。当我在真实设备(iPad mini,32 位设备,iOS 版本 8.1.1 12B435,more details here;iPhone 7s,64 位设备,iOS 版本 11.2.5 15D60)上启动应用程序时,读取方法不会从服务器接收数据。其实在read方法之前可以看到println打印的字符串,但是在read方法之后看不到println打印的字符串。

问题不在服务器端,因为我开发了一个 Android 应用程序,它从同一台服务器接收数据。没有防火墙限制或其他网络限制:Android 应用程序和 Codename One 模拟器都在连接到服务器的同一本地网络或从另一个服务器接收数据时接收数据。

怎么了?

最佳答案

解决了使用 InputStream 而不是 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"); 

}

谁能解释一下为什么它与 InputStream 一起工作?

关于ios - 代号一 - 无法从真实设备上的套接字读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48382548/

相关文章:

c - Linux中使用C获取inode的远程IP

ios - NSFileManager -createFileAtPath 因 NSInvalidArgumentException 而失败

IOS Swift assetForURL 错误

ios - 如何将图像添加到uitableviewcell

ios - 编译错误 AFNetworking ,语义问题?

java - 在 Android 上接收广播消息

java - 如何在具有 I/O 多路复用的 Java 服务器中异步处理请求?

storyboard - Storyboard Xcode 6 中的自适应转场是否已弃用?

objective-c - UIImage/UIImageView 包含 UIView 缩放时重绘

ios - LaunchScreen 未显示在任何设备和项目上