来自客户端的丢包

时间:2019-05-30 15:11:11

标签: java android

好的,我的客户端最终开始丢失来自服务器的数据包。首先,几分钟后,一切正常,但是随着时间的推移(大约3分钟),数据开始变得不足。 这是我的邮政编码:

private void sendMessage(final byte[] message) {
            try {
                if (tempClientSocket.isConnected()) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            PrintWriter out = null;
                            if (Global_variable.send_message_act == true) {
                            } else {
                                Global_variable.send_message_act = true;
                                try {
                                    try {
                                        Thread.sleep(100);
                                        Global_variable.send_message_act = false;
                                    } catch (InterruptedException e) {
                                        showMessage("not sleep2", Color.RED);
                                        Global_variable.send_message_act = false;

                                    }
                                    tempClientSocket.setSendBufferSize(500000);
                                    DataOutputStream dOut = new DataOutputStream(tempClientSocket.getOutputStream());

                                    dOut.write(message, 0, message.length);           // write the message
                                    dOut.writeChar('\n');

                                    dOut.close();
                                    showMessage("TA send mess!", Color.RED);
                                    if (!tempClientSocket.isClosed()) {
                                        showMessage("Ta Disconnected", Color.RED);
                                        tempClientSocket.close();
                                    } else {
                                        showMessage("socket close", Color.RED);
                                        showMessage("Ta Disconnected", Color.RED);
                                    }

                                    //showMessage("TA send mess!", greenColor);
                                } catch (IOException e) {
                                    showMessage("not sleep", Color.RED);
                                    e.printStackTrace();
                                    Global_variable.send_message_act = false;
                                }
                            }
                        }
                    }).start();
                }
                else{
                    showMessage("Socket is not connect", Color.RED);
                }
            } catch (Exception e) {
                e.printStackTrace();
                Global_variable.send_message_act = false;
            }
        }

P.S。如有必要,我可以将所有服务器代码扔到另一个资源上。

0 个答案:

没有答案