简体   繁体   English

从套接字读取数据时出现问题

[英]Issue in reading data from Socket

I am facing some problem during reading data from socket If there is some null data in socket stream so the DataInputStream would not read the full data and the so at the receiving end there is exception for parsing data. 在从套接字读取数据时,我遇到一些问题。如果套接字流中有一些空数据,则DataInputStream将无法读取完整数据,因此在接收端存在解析数据的异常。 What is the right way to read the data from socket so there is no loss of data at any time ? 什么是从套接字读取数据的正确方法,以便随时都不会丢失数据? Thanks in advance. 提前致谢。

You should post the code being used to read from the socket, but to me the most likely case is that the reading code is incorrectly interpreting a 0 byte as the end of the stream similar to this code 您应该发布用于从套接字读取的代码,但对我而言,最可能的情况是,读取代码错误地将0字节解释为流的末尾,类似于此代码

InputStream is = ...;
int val = is.read();
while (0 != (val = is.read()) {
  // do something
}

But the end of stream indicator is actually -1 但流指示器的末尾实际上是-1

InputStream is = ...;
int val = is.read();
while (-1 != (val = is.read()) {
  // do something
}

EDIT: in response to your comment on using isavailable() . 编辑:回应您对使用isavailable()评论。 I assume you mean available() since there is no method on isavailable() InputStream. 我假设您的意思是available()因为isavailable() InputStream上没有任何方法。 If you're using available to detect the end of the stream, that is also wrong. 如果您使用可用来检测流的结尾,那也是错误的。 That function only tells you how many bytes can be read without blocking (ie how many are currently in the buffer), not how many bytes there are left in the stream. 该函数仅告诉您可以无阻塞地读取多少个字节(即,缓冲区中当前有多少个字节),而不是流中还有多少个字节。

I personally prefer ObjectInputStream over DataInputStream since it can handle all types including strings, arrays, and even objects. 我个人更喜欢ObjectInputStream而不是DataInputStream,因为它可以处理所有类型,包括字符串,数组甚至对象。

Yes, you can read an entire object only by 1 line receive.readObject() , but don't forget to type-cast the returned object. 是的,您只能通过1行receive.readObject()读取整个对象,但不要忘记对返回的对象进行类型转换。

read() mightbe easier since you read the whole thing in 1 line, but not accurate. read()可能会更容易,因为您只需要一行就可以读取整个内容,但是并不准确。 read the data one by one, like this: 依次读取数据,如下所示:

receive.readBoolean()
receive.readInt()
receive.readChar()
etc..
String finalString = new String("");
int finalSize = remainingData;//sizeOfDataN;
int reclen = 0;
int count_for_breaking_loop_for_reading_data = 0;
boolean  for_parsing_data  = true; 
while(allDataReceived == false) {      
 ByteBuffer databuff = ByteBuffer.allocate(dis.available());
 //   System.out.println("bis.availbale is "+dis.available());
 databuff.clear();
 databuff.flip();
 dis.read(databuff.array());
 String receivedStringN   = trimNull(databuff.array());
 finalString = finalString + receivedStringN;
 System.out.println("final string length "+finalString.length());
 if(finalString.length() == finalSize) {
    allDataReceived = true;
 }
 count_for_breaking_loop_for_reading_data++;
 if(count_for_breaking_loop_for_reading_data > 1500) {
 For_parsing_data =  false;
 Break;
 }
 }

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

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