我从套接字读取数据时遇到问题。我试图以不同的方式解决我的问题,我认为我的代码现在是最简单的测试它。Android 在从套接字读取并停止在这一行( inStream.read() )时遇到问题。10 秒后(socket.setSoTimeout)我有一条消息--------错误:null。我想读取任何数据(0-255)或-1,但什么也没发生。在 PHP(在我的服务器上,我的代码)和 PC(不是我的应用程序)中,一切都很好。我应该怎么办?
这是我的第一篇文章,所以我很抱歉可能出现的错误。谢谢你的帮助!
try{
socket = new Socket( "192.168.0.150", 502 );
socket.setSoTimeout( 10000 );
outStream = new DataOutputStream( socket.getOutputStream() );
inStream = new DataInputStream( socket.getInputStream() );
System.out.println("----------" + outStream);
System.out.println("----------" + inStream );
outStream.write( 0x01 );
outStream.write( 0x03 );
outStream.write( 0x10 );
outStream.write( 0x00 );
outStream.write( 0x00 );
outStream.write( 0x04 );
outStream.write( 0x40 );
outStream.write( 0xC9 );
outStream.flush();
System.out.println("----------ok 1");
/////////////////////////////
// Thread.sleep( 50 );
System.out.println("----------Test1: " + socket.isConnected() );
System.out.println("----------Test2: " + socket.isInputShutdown() );
int dat = inStream.read();
System.out.println("----------ok2: " );
} catch( Exception e ){
e.printStackTrace(); // <-- edit
System.out.println( "----------Error: " + e.getMessage() );
}
------------
<uses-permission android:name = "android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name = "android.permission.UPDATE_DEVICE_STATS" />
<uses-permission android:name = "android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name = "android.permission.WAKE_LOCK" />
<uses-permission android:name = "android.permission.INTERNET" />
- 编辑 -