• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with DataInputStream

 
Abhishek Dhama
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
We have this application based on client server architecture.
Client and server are more or less symmetrical.Now the problem
is code works fine when we run it on same machine but when we run
client and server on two different machines program raises exception.
File is transferred without any problem from client to server but when
client tries to read in from DataInputStream it raises an exception.
Message says "connection reset". On server machine file transfer is
shown to be smooth without any problems.
I am attaching part of code where it raises exception.

while(in.readBoolean()){
String filename = new StringBuffer(recievedfilename).append(recievedfilecounter).append(".zip").toString();
recievedfilecounter++;
File filehandle = BufferManager.createFile(filename);
//read length of buffer from sender side
System.out.println("timeout:"+clientSocket.getSoTimeout());
int payloadLength = in.readInt();
//create a buffer that can contain the data
byte[] buf = new byte[ payloadLength ];
//read data from sender and write it to the buffer
int read = in.read( buf , 0 , payloadLength ) ;
//write data received from sender to this file:

FileOutputStream printToFile = new FileOutputStream( filehandle );
DataOutputStream dos = new DataOutputStream( printToFile );
dos.write( buf , 0 , payloadLength );
dos.close();
Input stream is constructed here:
COB = InetAddress.getByName( ipaddress );
clientSocket = new Socket( COB , PORT );
clientSocket.setSoTimeout(2000);
clientSocket.setKeepAlive(true);
//stream for sending data
out = new DataOutputStream( clientSocket.getOutputStream() );
//stream for receiving data
in = new DataInputStream( clientSocket.getInputStream() );
System.out.println("connection opened");

It would be wonderful if someone could point out where the problem lies.
regds
/dhama
[ November 17, 2003: Message edited by: Abhishek Dhama ]
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Connection reset" usually indicates that the server has crapped out on you.
Judging from the client-side code, I take it your server puts a boolean (true) into the stream, followed by the size of the file, then the file data, and it continues to do this until all the files are processed then it puts a boolean (false) to signal the transfer is complete. I think it is extremely optimistic of you to assume that you will read an entire file in one read call, especially when working with a network connection. From the API Documentation for java.io.InputStream.read(byte[] b,int off,int len):
Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read, possibly zero. The number of bytes actually read is returned as an integer.

You will probably have to accumulate the file data over several reads until you reach the size of the file, as it is unlikely the entire file will be available at one time due to network congestion, overhead, packet processing and so on.
 
Abhishek Dhama
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Joe,
Thanks for the reply!
We have made changes as suggested by you, instead of one ambitious
read method we read file in chunks of 1024 bytes.But I think problem
still remains.
Application works fine in some runs and in some test runs bomb with
java.net.SocketException with message printed out being "connection
reset". Funny thing is that same set of file produce different results.
Any suggestions?
Regds
Dhama
 
Ed Tidwell
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having the same kind of issues.
I have a simple client and server.
Server sends data out via DataOutputStream.
Client reads data in via DataInputStream.
The very first part of the message from the server is an int
that is used to determine how to parse the stream on the client side.
After running this for a while I will get a 0 or a VERY large positive
int value which the SERVER DOES NOT put in the stream. Buffer corruption?
Another ironic twist is when I tested this last night at home using my wireless card I could ONLY SEND ONE message and the server's next message
sent out would have a zero which the client interpets as EOF.
It would appear that I'm having issues with the TCP buffer size ?
I'm considering using NIO and Channels to move the data back and forth since this is the preferred way to do this in 1.4.
Since our problems seem to be related I look forward to hearing how you solve this. :-)
Thanks,
Ed
 
Ed Tidwell
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems there are more people out there with the same problem.
Maybe we can form a rehab help clinic? :-)
Here is the link: http://forum.java.sun.com/thread.jsp?thread=177655&forum=4&message=2020381

Ed
 
Ed Tidwell
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is your answer.
http://forum.java.sun.com/thread.jsp?forum=31&thread=175707
 
Ed Tidwell
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW
My wireless card is an 11Mbit/sec connection versus
a 100Mbit/sec connection for my 10BaseT connection.
The problem is NOT the buffer size but the speed of the connection and that read() will return BEFORE it reads ALL of the bytes for the array.
Enjoy,
Ed
Omitted the word NOT which changes the meaning just a little. :-)
[ November 19, 2003: Message edited by: Ed Tidwell ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!