Ios 代号一-Can';无法从实际设备上的套接字读取数据
我正在开发一个iOS应用程序,使用代号为One。我扩展了SocketConnection类,以便从服务器接收数据Ios 代号一-Can';无法从实际设备上的套接字读取数据,ios,sockets,ios8,codenameone,ios11,Ios,Sockets,Ios8,Codenameone,Ios11,我正在开发一个iOS应用程序,使用代号为One。我扩展了SocketConnection类,以便从服务器接收数据 class CustomSocketConnection extends SocketConnection { private OutputStream os; private InputStream is; private InputStreamReader isr; private String rawMessage; public C
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;iPhone 7s,64位设备,iOS版本11.2.5 15D60),读取方法不会从服务器接收数据。事实上,我可以看到println在read方法之前打印的字符串,但是我看不到println在read方法之后打印的字符串
问题不在于服务器端,因为我开发了一个Android应用程序,它从同一台服务器接收数据。没有防火墙限制或其他网络限制:Android应用程序和代号为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吗?您的设备在同一个网络中吗?似乎与网络、wifi等相关。您使用的URL和端口是什么?可能是您的开发机器上的防火墙阻止了外部通信?我使用的是公共IP地址和端口8888。我把运行模拟器的电脑和iPad连接到同一个网络上:模拟器读取数据,iPad不接收数据。这到底卡在哪里?它是在查找过程中卡住了还是什么都没有发生。我注意到您使用了
System.out
而不是Log.p()
,这将允许您在事后查看日志。使用网络监视器工具来检查流向服务器的流量也将有助于查明问题。我在您的代码中遗漏了这一点InputStreamReader
将对read()
的调用转换为缓冲调用,因为它需要将字节转换为字符。为此,您通常需要不止一个。此外,缓冲读取速度更快。因此,当您编写read()
时,它会转换为read(arrayOf8192Bytes)
。