I have a problem with read data from socket. I was trying solve my problem in different way and i think my code now is the simplest possible to test it. Android have a problem to read from socket and stop in this line( inStream.read() ). After 10s( socket.setSoTimeout ) i have a message -------error: null. I want to read any data( 0-255 ) or -1, but nothing has happend. In PHP( on my server, my code) and PC( not my app ) everything is fine. What should I do?
It is my first post, so i am sorry for possible mistakes. Thanks for your help!
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" />
--edit--
My logs: http://codepaste.net/eapot2
If you got a read timeout, obviously nothing is being sent by the peer.
NB 'nothing happens' is not the same thing as 'Android crashes'. Don't misuse standard terminology.
Change port to 1024-65565. Port 502 is usually not allowed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.