繁体   English   中英

第一个Socket I / O请求的快速响应,但在与远程串行端口通信时每隔一段时间都会减慢

[英]Fast response on first Socket I/O request but slow every other time when communicating with remote serial port

我正在使用套接字将串行命令传递给远程设备。 并且对该请求的响应被发回并打印出来。 但是,我遇到的问题是第一次是即时的,但其余的时间可能需要20秒才能收到回复。

我认为问题在于我对线程的尝试,但我并不完全确定。

                    new Thread() { 
                        @Override
                        public void run() {
                            System.out.println("opened");
                            try {
                                isSocketRetrieving.setText("Opening Socket");
                                socket = new Socket(getAddress(), getRemotePort()));

                                DataOutput = new DataOutputStream(socket
                                        .getOutputStream());

                                inFromServer = new BufferedReader(
                                        new InputStreamReader(socket
                                                .getInputStream()));

                                String line = "";

                                isSocketRetrieving.setText("Reading Stream......");

                                while ((line = inFromServer.readLine()) != null) {

                                    System.out.println(line);
                                    if (line.contains(getHandshakeRequest())) {


                                        DataOutput.write((getHandshakeResponse()toString() + "\r").getBytes());
                                        DataOutput.flush();
                                        DataOutput
                                                .write((getCommand().toString() + "\r").getBytes());
                                        DataOutput.flush();
                                        int pause = (line.length()*8*1000)/getBaud();
                                        sleep(pause);

                                    } else if (line.contains(readingObject
                                            .getExpected())) {

                                        System.out.println(line);
                                        textArea.append("value = " + line
                                                + "\n");
                                        textAreaScroll.revalidate();
                                        System.out.println("Got Value");
                                        break;
                                    }
                                }

                                System.out.println("Ended");
                                try {
                                    inFromServer.close();
                                    DataOutput.close();
                                    socket.close();
                                    isSocketRetrieving.setText("Socket is inactive...");
                                    rs232Table.addMouseListener(listener);
                                    interrupt();
                                    join();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                } catch (InterruptedException e) {
                                    System.out.println("Thread exited");
                                }
                            } catch (NumberFormatException e1) {
                                e1.printStackTrace();
                            } catch (UnknownHostException e1) {
                                e1.printStackTrace();
                            } catch (IOException e1) {
                                e1.printStackTrace();
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }.start();
   int pause = (line.length()*8*1000)/getBaud();
   sleep(pause);

因为这可能呢?

因为与串行连接相比,你的套接字连接相对较快,并且你发送相对较小的数据块,我将使用这些方法:

  • socket.setTcpNoDelay(真)
  • socket.getReceiveBufferSize()/ socket.setReceiveBufferSize() - (尝试改变大小)
  • socket.getSendBufferSize()/ socket.setSendBufferSize() - (尝试改变大小)

打开Socket后尝试设置一次。 也许这会有所帮助。

暂无
暂无

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

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