简体   繁体   English

套接字未收到任何数据

[英]Socket does not receive any data

I have a C# Server and this Java client in Android Studio: 我在Android Studio中有一个C#服务器和此Java客户端:

@Override
    public void run() {

        try {
            Socket socket = new Socket("192.168.0.107", 7778);
            BufferedReader inFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            while (true) {
                currentMessage = inFromServer.readLine();
                System.out.println(currentMessage);

                Thread.sleep(200);
            }
        }
        catch(Exception e) {
            System.out.print("Error: " + e.toString() + "\n");
        }
    }

I have tested the Server with Telnet and the messages a going out as they should, so im almost certain, that the problem somewhere in the client code. 我已经使用Telnet对服务器进行了测试,并且消息可以正常发送,几乎可以肯定的是,客户端代码中存在此问题。 I have also tried using a DataInputStream instead of a BufferedReader, but it gives me the same results. 我也尝试过使用DataInputStream而不是BufferedReader,但是它给了我相同的结果。 Also I don't get any Exeptions. 我也没有任何过人之处。

I have these Permissions in my AndroidManifest.xml: 我的AndroidManifest.xml中具有以下权限:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

readLine() blocks until there is a full line (terminated by either \\r\\n or \\n ) received in the underlying buffer (or the end of the steam has been reached). readLine()阻塞,直到在底层缓冲区中接收到整行(以\\r\\n\\n终止readLine()为止(或者已经达到蒸汽的末尾)。 So unless your server is terminating the data with a linebreak, your program will not print anything. 因此,除非您的服务器使用换行符终止数据,否则您的程序将不会打印任何内容。 Other than an Exception when the socket times out of course. 当套接字超时时,异常除外。

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

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