等待 java 客户端的套接字服务器响应
waiting socket server response with java client
此客户端必须与私人服务器交互。
我必须这样做:
- 从客户端发送一个块消息"Block"
- 通过持久性 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
检索数据
您需要从套接字获取输入流:
BufferedReader in =
new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String fromServer = in.readLine();
您可以通过阅读获得额外的帮助:https://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html
String command = "OBolle 01/05/2015\ff";
out.write(command.getBytes());
常见问题。你在读台词,但不是在写台词。添加行终止符,或使用 println().
此客户端必须与私人服务器交互。 我必须这样做:
- 从客户端发送一个块消息"Block"
- 通过持久性 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
检索数据您需要从套接字获取输入流:
BufferedReader in =
new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String fromServer = in.readLine();
您可以通过阅读获得额外的帮助:https://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html
String command = "OBolle 01/05/2015\ff";
out.write(command.getBytes());
常见问题。你在读台词,但不是在写台词。添加行终止符,或使用 println().