Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在android中获取TCP响应_Android_Sockets_Tcp_Timeout - Fatal编程技术网

无法在android中获取TCP响应

无法在android中获取TCP响应,android,sockets,tcp,timeout,Android,Sockets,Tcp,Timeout,代码应该作为客户端连接到TCP服务器,发送命令并接收响应 代码连接并发送命令,但在socket.getInputStream处超时,即使连接的服务器接收到命令并应响应,但已使用PC上的TCP客户端程序进行检查 以下是任务的代码: public class MyClientTask extends AsyncTask<Void, Void, Void> { String dstAddress; int dstPort; String command;

代码应该作为客户端连接到TCP服务器,发送命令并接收响应

代码连接并发送命令,但在socket.getInputStream处超时,即使连接的服务器接收到命令并应响应,但已使用PC上的TCP客户端程序进行检查

以下是任务的代码:

public class MyClientTask extends AsyncTask<Void, Void, Void> {

    String dstAddress;
    int dstPort;
    String command;
    String response = "";

    MyClientTask(String addr, int port, String cmd){
        dstAddress = addr;
        dstPort = port;
        command = cmd;
    }

    @Override
    protected Void doInBackground(Void... arg0) {

        Socket socket = null;
        InputStream inputStream;

        try {
            socket = new Socket();
            socket.connect(new InetSocketAddress(dstAddress, dstPort),2000);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
            byte[] buffer = new byte[1024];
            int bytesRead;

            PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
            out.println(command);

            inputStream = socket.getInputStream();
            socket.setSoTimeout(20000);
            while ((bytesRead = inputStream.read(buffer)) != -1){
                byteArrayOutputStream.write(buffer, 0, bytesRead);
                response += byteArrayOutputStream.toString("UTF-8");
            }

        }catch (UnknownHostException e){
            e.printStackTrace();
            response = "UnknownHostException: " + e.toString();
        } catch (IOException e) {
            e.printStackTrace();
            response = "IOException: " + e.toString();
        } catch (Throwable e) {
            e.printStackTrace();
            response = "Throwable: " + e.toString();
        }finally{
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        textResponse.setText(response);
        super.onPostExecute(result);
    }

}//MyClientTask

您正在读取响应,直到流结束。在对等方关闭连接之前,不会出现流结束。你得到了一个读取超时:因此,可能他没有关闭连接。或者你的超时时间太短了。两秒钟不算多


您需要一种正确的方式来读取响应,或者在部分到达时处理它们。

更正:它实际上在inputStream.ReadBuffer处超时。当读取请求具有读取buffer.length字节时,它不会停止吗?减少缓冲区的大小并没有帮助,当然它确实有帮助,但它位于一个只在流末尾终止的循环中。我没有说任何关于缓冲区大小的事情,也没有减少它。循环似乎不是问题,它在:inputStream.readbuffer上超时。也就是说,将整个循环替换为bytesRead=inputStream.readbuffer,0,1;仍然超时。用什么替换整个循环?如果您的意思是在单次读取或循环中的第一次读取时仍有超时,则可能是服务器根本没有响应,或者您的超时太短。您确定发送的命令有效吗?如果命令真的是一行,那么响应肯定也是一行?在这种情况下,您可以使用BufferedReader.readLine而不是所有这些代码。您是否尝试了更长的超时时间?这些请求真的是排队吗?