简体   繁体   English

在Java 1.8中读取套接字的inputstream错误

[英]Error with reading inputstream of socket in java 1.8

With the following code an error occurs with reading the inputstream. 使用以下代码,读取输入流时会发生错误。 I use the following code: 我使用以下代码:

public void run() {
    try {
        SocketAddress sockaddr = new InetSocketAddress(nServer, nPort);
        nsocket = new Socket();
        nsocket.connect(sockaddr, 10 * 1000);
        if (nsocket.isConnected()) {
            nsocket.setSoTimeout(20 * 1000); 
            nis = nsocket.getInputStream();
            nos = nsocket.getOutputStream();
            if (nProtocol.equals("ntripv1")) {
                String requestmsg = "GET /" + nMountpoint + " HTTP/1.0\r\n";
                requestmsg += "User-Agent: NTRIP LefebureNTRIPClient/20131124\r\n";
                requestmsg += "Accept: */*\r\n" + "Connection: close\r\n";
                if (nUsername.length() > 0) {
                    requestmsg += "Authorization: Basic " + ToBase64(nUsername + ":" + nPassword) +"\r\n";
                }
                requestmsg += "\r\n";
                nos.write(requestmsg.getBytes());
            } else {
            }

            byte[] buffer = new byte[4096];
            int read = nis.read(buffer, 0, 4096);
            while (read != -1) {
                byte[] tempdata = new byte[read];
                System.arraycopy(buffer, 0, tempdata, 0, read);

                try {
                    dataMessenger.send(Message.obtain(null, MSG_NETWORK_GOT_DATA, tempdata));
                } catch (RemoteException e2) {
                }
                read = nis.read(buffer, 0, 4096);
            }
        }
    } catch (SocketTimeoutException ex) {
        try {
            dataMessenger.send(Message.obtain(null, MSG_NETWORK_TIMEOUT));
        } catch (RemoteException e2) {
        }
    } catch (Exception e) {
            LogMessage(e.getLocalizedMessage());
        e.printStackTrace();
    } finally {
        try {
            nis.close();
            nos.close();
            nsocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            dataMessenger.send(Message.obtain(null, MSG_NETWORK_FINISHED));
        } catch (RemoteException e2) {
        }
    }
}

The server sides works well with other applications. 服务器端可与其他应用程序很好地协同工作。 This code also make a connection with the server and gets some date but after a little while it quites at nis.read. 该代码还与服务器建立连接并获得一些日期,但过了一段时间,它就会出现在nis.read上。

The codes is written with java 1.8 for android studio. 该代码是使用Android Studio的Java 1.8编写的。

Does anyone have an idea to get all the information from the server and read all the information so it can be used in the application. 是否有人有想法从服务器获取所有信息并读取所有信息,以便可以在应用程序中使用它。

Update The error is given with the following line in the while-loop: 更新 while循环中的以下行给出了错误:

read = nis.read(buffer, 0, 4096); 

Update 2 The given error is: System.err: java.net.SocketException: Connection reset 更新2给定的错误是:System.err:java.net.SocketException:连接重置

Always .flush() on the outputstream when you are done sending a message. 完成发送消息后,请始终在输出流上使用.flush()。 Think of it like a commit if you must, but its really just a buffering matter. 如果需要,可以将其视为一次提交,但这实际上只是一个缓冲问题。

Secondly. 其次。 it is very possible that the server is using http keep-alive and doesn't hangup (even though you put Connection: close , which is the default for HTTP/1.0 too); 服务器很有可能正在使用http keep-alive并且没有挂断(即使您将Connection: close设置为HTTP / 1.0的默认设置)也是如此; then it's the client side's job to hang up. 然后挂断是客户端的工作。 To do so you need to detect the content is finished, so you must support both content-length response header if present, or 'chunked' transport encoding (likely if the answer is any larger than some arbitrary amount like a few kB). 为此,您需要检测内容是否已完成,因此,您必须同时支持content-length响应标头(如果存在)或“分块”传输编码(可能的话,如果答案大于某个任意量,例如几kB)。 HTTP is a nice sport...! HTTP是一项不错的运动……!

You can also try the no http version on the GET line (implies HTTP 0.9) which I think simply doesn't support http keep-alive. 您也可以尝试在GET行上使用no http版本(暗示HTTP 0.9),我认为它根本不支持http keep-alive。

If the server is presuming keep-alive is on (see https://en.wikipedia.org/wiki/HTTP_persistent_connection ) then that would explain the behavior. 如果服务器假定保持活动状态为打开(请参阅https://en.wikipedia.org/wiki/HTTP_persistent_connection ),则可以说明该行为。 See also https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Connection 另请参阅https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Connection

I recommend you install Wireshark and sniff the traffic. 我建议您安装Wireshark并监听流量。 It's relatively intuitive to use. 使用起来相对直观。 You will see if all data was sent and if the server really forgot to close (TCP FIN). 您将看到是否已发送所有数据,以及服务器是否真的忘记关闭(TCP FIN)。

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

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