套接字长读数据

发布于 2024-11-08 18:30:57 字数 2178 浏览 0 评论 0原文

这是我的代码:

private String receiveData(String sjson) {
        Log.i(TAG,"send request: " + sjson);
        String jstr="";
        try {
            OutputStream out = s.getOutputStream();
            out.write(sjson.getBytes());
            out.flush();
            //out.close();
            Log.v(TAG,"sended data");
            BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
            char[] cbuf = new char[1];
            input.read(cbuf);                   
            String size = new String(cbuf);
            while (input.read(cbuf) != 0) {
                if((new String(cbuf)).equals("{") == true) 
                    break;
                size = size + new String(cbuf);
            }
            char[] jbuf = new char[Integer.valueOf(size)];
            input.read(jbuf);
            jstr = "{" + new String(jbuf);
        }catch (Exception e) {
            Log.e(TAG,e.toString());
        }
        Log.d(TAG,"responce: " + jstr);
        return jstr;
    }

    public void connectSocket() {
        Log.v(TAG,"connecting Socket: "+URL+":"+PORT);
        try {
            s = new Socket(URL, PORT);
            Log.v(TAG,"connect Socket!");
            ERROR_CODE = 0;
        }catch (Exception e) {
            Log.e(TAG,e.toString());
            ERROR_CODE = ERROR_SOCKET_CONNECT_SUCCESSFULL;
        }
        Log.e(TAG,getErrorMsg(ERROR_CODE));
    }

    public void closeSocket() {
        Log.v(TAG,"closeSocket");
        try {
            s.close();
        }catch (Exception e) {
            Log.e(TAG,e.toString());
        }
    }

在服务器上,答案不到一秒。在客户端,经过 1 分钟后才读取数据。

应用程序停止在 input.read(cbuf); 等待答案。

日志:

05-23 06:35:17.540: VERBOSE/Utilits(358): Auth: 77.221.129.100:10598
05-23 06:35:17.660: INFO/Utilits(358): send request: 0119{"data":{"password":"12345","imei":"000000000000001"},"method":"login"}
05-23 06:36:17.909: DEBUG/Utilits(358): responce: {"response":{"success":true,"user":{"id":"6","properties":{"auto":"model":"audi","color":"ffff","number":"td123r"}},"is_driver":"1"}}}

为什么要花这么长时间才能阅读答案?

This is my code:

private String receiveData(String sjson) {
        Log.i(TAG,"send request: " + sjson);
        String jstr="";
        try {
            OutputStream out = s.getOutputStream();
            out.write(sjson.getBytes());
            out.flush();
            //out.close();
            Log.v(TAG,"sended data");
            BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
            char[] cbuf = new char[1];
            input.read(cbuf);                   
            String size = new String(cbuf);
            while (input.read(cbuf) != 0) {
                if((new String(cbuf)).equals("{") == true) 
                    break;
                size = size + new String(cbuf);
            }
            char[] jbuf = new char[Integer.valueOf(size)];
            input.read(jbuf);
            jstr = "{" + new String(jbuf);
        }catch (Exception e) {
            Log.e(TAG,e.toString());
        }
        Log.d(TAG,"responce: " + jstr);
        return jstr;
    }

    public void connectSocket() {
        Log.v(TAG,"connecting Socket: "+URL+":"+PORT);
        try {
            s = new Socket(URL, PORT);
            Log.v(TAG,"connect Socket!");
            ERROR_CODE = 0;
        }catch (Exception e) {
            Log.e(TAG,e.toString());
            ERROR_CODE = ERROR_SOCKET_CONNECT_SUCCESSFULL;
        }
        Log.e(TAG,getErrorMsg(ERROR_CODE));
    }

    public void closeSocket() {
        Log.v(TAG,"closeSocket");
        try {
            s.close();
        }catch (Exception e) {
            Log.e(TAG,e.toString());
        }
    }

At server the answer is less than a second. At the client it passes 1 minute before reading data.

Apps stoped at input.read(cbuf); waiting for answer.

Logs:

05-23 06:35:17.540: VERBOSE/Utilits(358): Auth: 77.221.129.100:10598
05-23 06:35:17.660: INFO/Utilits(358): send request: 0119{"data":{"password":"12345","imei":"000000000000001"},"method":"login"}
05-23 06:36:17.909: DEBUG/Utilits(358): responce: {"response":{"success":true,"user":{"id":"6","properties":{"auto":"model":"audi","color":"ffff","number":"td123r"}},"is_driver":"1"}}}

Why does it take so long to read an answer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘味果▽酱 2024-11-15 18:30:57

您到底希望该方法做什么?它里面有bug,但它做了它应该做的事情。

  • 创建 InputStreamReader 时应指定编码/字符集
  • 为什么要从开始到“{”逐个字符读取
  • 为什么要为点击“{”之前读取的每个字符创建一个字符串
  • 为什么要在循环中附加字符串?如果必须附加,请使用 StringBuilder。
  • input.read 返回一个整数,表示您已收到多少字节/字符
    它永远不能保证它将填充缓冲区。因此您可能无法获取所有数据。
  • 为什么不关闭资源?

..现在解释为什么它可能会很慢。服务器是否正在刷新数据?如果没有,请确保服务器正在刷新数据。

What on earth do you expect that method to do? There are bugs in it, and it does things that it should do.

  • You should specify encoding/charset when you create the InputStreamReader
  • Why do you read character by character from start to "{"
  • Why do you create a string for each character that you read before you hit "{"
  • Why do you append strings in a loop? Use a StringBuilder if you must append.
  • input.read returns an integer that says how many bytes/character that you have received
    It's never guaranteed that it will fill the buffer. So you might not get all data.
  • Why aren't you closing resources?

.. and now to why it might be slow. Is the server flushing the data? If not, make sure that the server is flushing the data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文