用java客户端等待套接字服务器响应

时间:2015-05-14 20:59:44

标签: java sockets response

此客户端必须与私人服务器进行交互。 我必须这样做:

  1. 从客户端发送阻止消息"阻止"
  2. 通过持久性tcp
  3. 请求一些xml数据
  4. 从客户端发送解锁消息"解锁"
  5. 问题在于,当我发送接收xml数据的命令时,shell保持静止到行readLine()并返回任何内容。

    这是我的客户代码:

                /* LOCK  */
                socket = new Socket(host.getHostName(), 7000);
                socket.setSoTimeout(50000);
                OutputStream out = socket.getOutputStream();
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    
                System.out.println("\nlocking...");
                out.write(lockCommand);
    
                out.flush();
                out.close();
                in.close();
                socket.close();
    
                /* SEND BOLLE COMMAND  */
                //String command = "OBolle                                   01/05/2015\ff";
                byte[] bolleCommand= {0x4f, 0x42, 0x6f, 0x6c, 0x6c, 0x65, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x30, 0x31, 0x2f, 0x30, 0x35, 0x2f , 0x32, 0x30, 0x31, 0x35, (byte) 0xff};              
                socket = new Socket(host.getHostName(), 7001);
                socket.setSoTimeout(50000);
                out = socket.getOutputStream();
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    
                System.out.println("\nsending command for receive XML data.....");
                out.write(bolleCommand);
    
                System.out.println("\nReading XML response.....");
                String response = in.readLine();
                System.out.println("in "+ response);
    
                out.close();
                in.close();
                socket.close();
    
                /* UNLOCK  */
                socket = new Socket(host.getHostName(), 7000);
                socket.setSoTimeout(50000);
                out = socket.getOutputStream();
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    
                System.out.println("\nunlocking...");
                out.write(unlockCommand);
                out.flush();
    
                out.close();
                in.close();
                socket.close();
    
                System.out.println("\nAll done...");
    

    我必须等待服务器响应,即10/15秒后返回。

    连接是TCP持久性。

    我也试着:

    //String response = in.readLine();
                //System.out.println("in "+ response);
                int dataBuffer;
                while ((dataBuffer = socket.getInputStream().read()) != -1) 
                   System.out.print((char)dataBuffer);
    

    但我有一些结果...... 我该怎么办?

    非常感谢你。

    这是返回数据的示例:

    <DocumentElement>\r\n  <ValoreTesto1 />\r\n    <ValoreTesto2 />\r\n    <ValoreTesto3 />\r\n    <ValoreTesto4 />\r\n    <ValoreTesto5 />\r\n    <ValoreTesto6 />\r\n    <ValoreTesto7 />\r\n    <ValoreTesto8 />\r\n    <ValoreTesto9 />\r\n        <QData>2015-05-09T00:00:00+02:00</QData>\r\n    
    

    更新: 准确的嗅探我明白服务器在这种模式下工作:

    将lock命令发送到7000 在7001上发送检索命令数据 发送解锁到7000 从命令2中检索数据

2 个答案:

答案 0 :(得分:3)

您需要从套接字获取输入流:

BufferedReader in =
            new BufferedReader(
                new InputStreamReader(socket.getInputStream()));
String fromServer = in.readLine();

您可以通过阅读https://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html

获得额外帮助

答案 1 :(得分:0)

String command = "OBolle                                   01/05/2015\ff";
out.write(command.getBytes());

常见问题。你正在读行,但你不是在写行。添加行终止符,或使用println().

相关问题