Java Scoket Solution java.io.EOFExceptionWhen the socket receives data, it often throws a java.io.EOFException exception, there is no clear reason and hint, search on the Internet, many people are asking this question, but did not find a practical way. After research, the problem has been solved. See the exception stack information as follows: Java.io.EOFException
At Java.io.DataInputStream.readFully (datainputstream.java:178)
At Java.io.DataInputStream.readUTF (datainputstream.java:565)
At Java.io.DataInputStream.readUTF (datainputstream.java:522), the Java explanation for this is also very vague: PublicclassEofexceptionextends IOException throws this exception when the end of a file or stream is accidentally reached during the input process.
This exception is primarily used by the data input stream to indicate the end of the stream being reached. Note that many other input operations return a special value indicating that the end of the stream is reached, rather than throwing an exception. From the above hints can be blurred to see, because do not know the end of the stream, when reaching the end of the time, the nature throws this exception. Since you do not know the ending, simply set a cache, and then read a batch of data after the output. For the duration of the insurance, it can be said that the cache set a large point, one can fully receive the desired content, so that you can read the content at once, to avoid the loop to get. Let's put this code out here: ...
PrivateStaticFinalintbuffer_size=1024*1024;
......
Socket socket =NewSockets (Cfg.getip (), Integer.parseint (Sysparamstoolkit.getproperty ("Socketport")));
String charset = Sysparamstoolkit.getproperty ("Socke.rexml.charset");//socket sent by the character set encoding
Try{
OutputStream dos = Socket.getoutputstream ();
Dos.write (Xmlcmd.getbytes (charset));
Dos.flush ();
DataInputStream dis =NewDataInputStream (Socket.getinputstream ());
Char[] data =NewChar[Buffer_size];
BufferedReader br =NewBufferedReader (NewInputStreamReader (Socket.getinputstream (), CharSet));
intLen = br.read (data);
String rexml = string.valueof (data, 0, Len);//Receive a string of data
}Catch(Exception e) {
returnfalse;
}finally{
if(Socket.isconnected ())
Socket.close ();
}
...... After the above treatment, there is no problem. The size of the cache buffer_size is determined by the size of the content you want to receive.
Java Scoket Solution Java.io.EOFException