Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Socket read binary data trouble :: TCP packets lost in Java?

843790Apr 23 2007 — edited Apr 23 2007
Dear List!

I stucked in a trouble with java sockets.

I have a simple client server approach. The server is written in C++ and runs under Linux (Suse 10.2). The client is in Java and runs on Windows.
I have to send short messages between the applications like set some variables in the server side and the server sends an ack like variable set sucessfully.

It works fine. All the messages are arrived in both way and the connection handling is great also (conect, close, bind, accept...). But...

Problem:

One of these short messages is a request for some binary data (max 15 MB but with variable size) from the server.

For the short message exchange I am using BufferedReader and PrintWriter in the Java client, what is not acceptable for reading binary data while it converts the bytes based on some encoding ( so the higer hexa byte values like 90 will be decoded as 3F ).

I tryed
BufferedInputStream
,
DataInputstream
,
InputStream
classes to download this binary data.

The server first sends the size of the data (The C++ socket server shows how much data is sent - it always send them all ). I can receive it correctly, so I know how much data do I have to download.

The promlem is that some data downloads are successfull in the client side and in some cases the read() method stucks just before neraly the last read cycle.

Even if I try
read()
byte-by-byte or
read(byte[] buffer, offset, length)
through a for cycle or with while loops sometimes I can download the data sometimes not.
Every 'fifth' readout failes but no regularity can be found; sometimes works sometimes does not.

I tryed to set some closure characters in the server side like "\n" but it does not solves my problem.

TCP is "reliable" so I dont think that these packets are lost.
I tested it with Ethereal (network monitor program). Every bytes are sent and arrived as well, so no packet lost found. TCP does its job.

I can handel my problem with socket timeout but I would exactly know the reason.

I post some code, maybe I am wrong somewhere:
clientSocket = new Socket(host, port); 

out = new PrintWriter(echoSocket.getOutputStream(), true);
nonfilteredStream = clientSocket .getInputStream();
in = new BufferedReader( new InputStreamReader( nonfilteredStream   )  ); 

int message_size = Integer.parseInt( in.readLine() ); // get the length of the binary data
byte buffer[] = new byte[ message_size ];

int alreadyReaded = 0;
int currentRead = 0;
int readSize = message_size; // or some smaller integer like 1000

try
            {                                                   
                while( read < message_size )
                {
                    
                   if( (message_size-read) < readsize ) 
                   {
                       readsize = (message_size-read);
                   }// if
                                                                       
                   current = nonfilteredStream.read( buffer, read, readsize );
 
/* Sometimes the program stucks here until timeout */

                   System.out.println( "Current ::" + current );
                   
                   if(current == -1)
                   {
                       System.out.println( .. );
                       throw new Exception( ... );
                   }
                   else
                   {
                       read += current;
                   }

                   System.out.println( "Downloaded :: " + read + "..." );

                }// while  
                                
            }           
            catch( InterruptedIOException iioex )
            {
                System.out.println( ... );
            }            
            catch( IOException ioex )
            {
                System.out.println( ... );
            }// try
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 21 2007
Added on Apr 23 2007
3 comments
3,478 views