简体   繁体   English

在Java中从套接字读取未格式化的数据

[英]reading unformatted data from socket in java

I have a server socket application which listens on particular ip and port. 我有一个服务器套接字应用程序,它监听特定的IP和端口。 Then I have client socket which reads input from server and give response. 然后我有客户端套接字,它从服务器读取输入并给出响应。 But my problem is that server do not give formatted line termination characters at the end. 但是我的问题是服务器最后没有给出格式化的行终止符。 so when i use buffered reader readline method it hangs. 因此,当我使用缓冲读取器readline方法时,它会挂起。 so how should i read the data send by server. 所以我应该怎样读出的数据由服务器发送。 here server is basically a API(METER). 这里的服务器基本上是一个API(METER)。

Here is sample request/response sequence between server and client. 这是服务器和客户端之间的示例请求/响应序列。

Request: 8/2/2012 3:28:24 PM.60764 (+0.0000 seconds)
 53 0D                                             S.              
Answer: 8/2/2012 3:28:24 PM.87364 (+0.2656 seconds)
 53 50 31 39 35 30 34 37 38 34 0D                  SP19504784.     
Request: 8/2/2012 3:28:24 PM.92064 (+0.0000 seconds)
 53 48 0D                                          SH.             
Answer: 8/2/2012 3:28:24 PM.02964 (+0.1094 seconds)
 53 48 0D                                          SH.             
Request: 8/2/2012 3:28:24 PM.02964 (+0.0000 seconds)
 53 0D                                             S.              
Answer: 8/2/2012 3:28:24 PM.13964 (+0.1094 seconds)
 53 50 31 39 35 30 34 37 38 34 0D                  SP19504784.     
Request: 8/2/2012 3:28:24 PM.20164 (+0.0000 seconds)
 53 48 0D                                          SH.             
Answer: 8/2/2012 3:28:24 PM.31164 (+0.1094 seconds)
 53 48 0D                                          SH.             
Request: 8/2/2012 3:28:24 PM.31164 (+0.0000 seconds)
 80                                                ?               
Answer: 8/2/2012 3:28:24 PM.46764 (+0.1563 seconds)
 FC                                                ü               
Request: 8/2/2012 3:28:24 PM.46764 (+0.0000 seconds)
 F1                                                ñ               
Answer: 8/2/2012 3:28:25 PM.84264 (+0.3750 seconds)
 8A 31 39 35 30 34 37 38 34 88                     ?19504784?      
Request: 8/2/2012 3:28:25 PM.90464 (+0.0000 seconds)
 F1                                                ñ               
Answer: 8/2/2012 3:28:25 PM.02964 (+0.1250 seconds)
 8A 01 41 32 4D 47 30 31 42 B1                     ?.A2MG01B±      
Request: 8/2/2012 3:28:26 PM.81164 (+0.0000 seconds)
 CA F0 00 00 00 00 00 00 00 3A                     Êð.......:      
Answer: 8/2/2012 3:28:26 PM.02964 (+0.2188 seconds)
 C4 00 05 00 C9 31 36 38 C4 B7 B7 3F 2E C9 74 5A   Ä...É168Ä··?.ÉtZ
 02 00 02 00 FE FC C9 A1 0D 07 00 FB FF FD FC C9   ....þüÉ¡...ûÿýüÉ
 1E 0E FD FF FE FF FD FC C9 B8 FE 4E FF FA 01 FF   ..ýÿþÿýüɸþNÿú.ÿ
 FF B9 D0 F9 F0 3F D0 BF 50 9F                     ÿ¹Ðùð?пP?      

You cannot use readline() if the server doesn't send line endings. 如果服务器不发送行尾,则不能使用readline() However, something in the raw protocol has to indicate when a response is complete. 但是,原始协议中的某些内容必须指示响应何时完成。 This can be one or more of 这可以是一个或多个

  1. a specific character sequence 特定的字符序列
  2. the response to each command ("Request" in your sample) has a known, fixed length 对每个命令的响应(示例中的“请求”)具有已知的固定长度
  3. each response contains the length embedded in it at a fixed offset from the start 每个响应都包含嵌入其中的长度,该长度与起始位置有固定的偏移量

You will have to do your own buffering and detect the end of each message yourself. 您将必须做自己的缓冲并自己检测每个消息的结尾。 Without a detailed description of the protocol being used, that's about as specific an answer as you can expect. 如果没有对所使用协议的详细说明,那将是您所期望的具体答案。

You could try with one of the read method of BufferedReader, and process any breaks in the data yourself. 您可以尝试使用BufferedReader的read方法之一,然后自己处理数据中的任何中断。 That's probably the best control you'll have. 那可能是您拥有的最佳控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM