【发布时间】:2015-05-14 20:59:43
【问题描述】:
此客户端必须与私人服务器交互。 我必须这样做:
- 从客户端发送块消息“块”
- 通过持久 tcp 请求一些 xml 数据
- 从客户端发送解锁消息“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.