【问题标题】:waiting socket server response with java client [duplicate]等待带有Java客户端的套接字服务器响应[重复]
【发布时间】:2015-05-14 20:59:43
【问题描述】:

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

  1. 从客户端发送块消息“块”
  2. 通过持久 tcp 请求一些 xml 数据
  3. 从客户端发送解锁消息“Unlock”

问题是,当我发送接收 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    

更新: 通过准确的嗅探,我了解到服务器在这种模式下工作:

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

【问题讨论】:

  • out.write() 之后的 out.flush()。
  • 任何链接@zubergu
  • @michele 嗨,只是想确定一下,数据是否包含行终止符?
  • 嗨@kucing_terbang,返回的数据以\r\n结尾。我通过数据包嗅探器看到了这一点。
  • InputStream.read()不带参数的结果是一个字节,不是dataBuffer.

标签: java sockets response


【解决方案1】:

你需要从socket获取输入流:

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

您可以通过阅读获得额外帮助:https://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html

【讨论】:

  • 不起作用,请参阅我编辑的答案。谢谢
  • in.readLine() 将从服务器读取一行。您需要将其分配给变量并使用它。同样在您的第一个示例中,您正在发送到服务器然后等待回复。现在您似乎想连接到服务器,阅读一些消息,然后向服务器发送一些内容。
  • 已经不行了,我用 PacketSender 工具试试这个消息,它是正确的。我必须发送命令并等待来自服务器的 xml。
  • 答案编辑了更多信息
  • 我的回答对你原来的问题是正确的。您没有从套接字正确读取。
【解决方案2】:
String command = "OBolle                                   01/05/2015\ff";
out.write(command.getBytes());

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

【讨论】:

  • 抱歉我要更改什么?
  • @michele“添加行终止符,或使用println()”的哪一部分你不明白吗?
  • 我试过 String command = "OBolle 01/05/2015\ff\r\n" 但没用
  • 第二个选项我不明白 println ... 你的意思是 out.println(...) 吗?但这是不正确的。谢谢
  • 您还必须使用lockCommand 以及您发送的任何其他内容来完成此操作。您还应该在单个套接字上执行所有这些操作,而不是每个命令打开/关闭套接字。当我建议您可以使用 println() 时,我的意思是由几个 JDK 类导出的方法,您可以轻松地自行查找并确定如何使用。我希望如此。
猜你喜欢
  • 2015-03-02
  • 1970-01-01
  • 2016-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-19
  • 2011-10-17
相关资源
最近更新 更多