简体   繁体   English

Android-inputStream.read()-我什么也看不到

[英]Android - inputStream.read() - I can not read anything

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() ). Android有一个问题,需要从套接字读取并停止在此行(inStream.read())。 After 10s( socket.setSoTimeout ) i have a message -------error: null. 10s(socket.setSoTimeout)之后,我收到一条消息-------错误:null。 I want to read any data( 0-255 ) or -1, but nothing has happend. 我想读取任何数据(0-255)或-1,但是什么也没发生。 In PHP( on my server, my code) and PC( not my app ) everything is fine. 在PHP(在我的服务器上,我的代码)和PC(而不是我的应用程序)中,一切都很好。 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 我的日志: 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'. 注意:“什么都没有发生”与“ Android崩溃”不同。 Don't misuse standard terminology. 不要滥用标准术语。

Change port to 1024-65565. 将端口更改为1024-65565。 Port 502 is usually not allowed. 通常不允许使用端口502。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM