服务器套接字在一段时间后未从客户端套接字接收数据

时间:2018-08-09 18:14:41

标签: java serversocket

我正在开发用于连接医院的XL-200实验室机器的应用程序。 但是在我将服务器代码作为波纹管代码运行之后,它将完美运行一次。但是,当我尝试从实验室机器服务器发送数据时,不会收到任何数据。我使用了示例第三方软件。当我使用它与系统完美配合。而且当我在实验室设备中关闭该软件时,它也会显示一条断开连接的消息。但是我关闭应用程序时也没有显示。 我认为保持与客户端实验室设备的连接有问题。请任何人提供帮助

公共类SysmexTest {

public static String message = "";
public static String barcode = "";
public static int open_msg = 0;

public static int val = 1;
public static Socket activeSocket = null;

public static String changeStr(String my_ss) {
    String ss = my_ss;

    ss = my_ss.replace("\1", "[SOH]").replace("\12", "[LF]").replace("\2", "[STX]").replace("\3", "[ETX]").replace("\4", "[EOT]").replace("\5", "[ENQ]").replace("\6", "[ACK]").replace("\15", "[CR]").replace("\25", "[NAK]");

    return ss;
}

public static void main(String[] args) throws Exception {
    ServerSocket serverSocket = new ServerSocket(6000, 100,
            InetAddress.getByName("192.168.1.165"));

    System.out.println("Server started  at:  " + serverSocket);

    while (true) {
        System.out.println("Waiting for a  connection...");
        activeSocket = serverSocket.accept();
        activeSocket.setKeepAlive(true);

        System.out.println("Received a  connection from  " + activeSocket);

        handleClientRequest(activeSocket);
    }

}

public static void handleClientRequest(Socket socket) {

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                BufferedReader socketReader = null;
                BufferedWriter socketWriter = null;
                socketReader = new BufferedReader(new InputStreamReader(
                        socket.getInputStream()));
                socketWriter = new BufferedWriter(new OutputStreamWriter(
                        socket.getOutputStream()));

                String outMsg = "";

                String print_val = "";

                while (true) {

                    int myVal = socketReader.read();
                     System.out.println("Client " + Character.toString((char) myVal));

                    if (myVal == 5) {
                        System.out.println("Client [ENQ]");
                    }

                    if (myVal == 5) {

                        outMsg = "\6";
                        socketWriter.write(outMsg);
                        socketWriter.flush();
                        System.out.println("Sent [ACK]");
                    } else {

                        print_val = print_val + changeStr(Character.toString((char) myVal));

                        System.out.println("Client Un : " + print_val);

                        outMsg = "\6";
                        socketWriter.write(outMsg);
                        socketWriter.flush();


                    }


                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }).start();

}

0 个答案:

没有答案