You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@mina.apache.org by Carlos Alegria Galicia <ca...@gmail.com> on 2014/07/31 03:41:59 UTC

Sending files through TCP Client/Server

Hi everyone,

I developed a MINA based client and server to send files over a TCP
connection. I am using MINA 2.0.7 on both.

   - In the client I am sending the file using a header to send file
   properties and FileRegion to actually stream the file.
   - In the server I created a Filter to read the header, and then receive
   and locally store the file content 'till the expected number of bytes are
   received.

My problem is that, intermittently, I am not able to receive the last chunk
of data from the client. By debugging, I found out that the last chunk is
actually always shorter than the receive buffer in the server. Is it
possible that I am not receiving this last chunk because the
Filter.messageReceived() method is not called on my filter because the
buffer is not completely filled with data? If this is the case, how can I
tell MINA to expect a message shorter than the receive buffer (is
session.getConfig().setReadBufferSize() a correct way to do this) ?

I am posting my server buffer reading code if is of any help:
-----------------------------
.....

Context ctx = (Context) session.getAttribute(CONTENT);
 //
// saving bytes, if available
//

if (in.hasRemaining()) {

int byteCount = in.limit();
byte data[] = new byte[byteCount];
in.get(data);

try {
ctx.out.write(data);
ctx.bytes -= byteCount;
} catch (IOException ioe) {
ctx.out.close();
nextFilter.exceptionCaught(session, ioe);
return;
}

}
 session.getConfig().setReadBufferSize(readBufferSize);
 //
// check if file is complete
//
 if (ctx.bytes <= 0) {
ctx.out.close();
nextFilter.messageReceived(session, ctx.file);
}

--------------------------------------

Thanks in advance

Re: Sending files through TCP Client/Server

Posted by Emmanuel Lécharny <el...@gmail.com>.
Le 01/08/2014 17:06, Carlos Alegria Galicia a écrit :
> Emmanuel,
>
> After debugging more the issue (and correcting the buffer reading code as
> you described), I finally found the root cause. When reading the header, I
> (incorrectly) assumed that the header should be contained in a single
> buffer. I was discarding some bytes of the header buffer that actually
> belong to the payload (the file being transmitted). This of course, is
> happening intermittently.

Yes, this is a common mistake : TCP fragmentation... You are not the
only one hit by this bus ;-)


Re: Sending files through TCP Client/Server

Posted by Carlos Alegria Galicia <ca...@gmail.com>.
Emmanuel,

After debugging more the issue (and correcting the buffer reading code as
you described), I finally found the root cause. When reading the header, I
(incorrectly) assumed that the header should be contained in a single
buffer. I was discarding some bytes of the header buffer that actually
belong to the payload (the file being transmitted). This of course, is
happening intermittently.

Thanks a lot for your help!,
Carlos Alegria





On Thu, Jul 31, 2014 at 5:31 PM, Emmanuel Lécharny <el...@gmail.com>
wrote:

> Le 31/07/2014 23:59, Carlos Alegria Galicia a écrit :
> > Emmanuel,
> >
> > Thanks a lot for your answer. After considering your comment and
> debugging
> > the client-server communication, I found out that, when the issue
> "happens"
> > (I assume this is happening when session Idle is present and there are
> > missing bytes in the state holding object), the server actually reads the
> > same amount of bytes sent by the client.
> >
> > I think then that I am incorrectly reading the input buffer in my
> Filter. I
> > am currently doing this:
> >
> > ...
> > if (in.hasRemaining()) {
> >
> >     int byteCount = in.limit();
> >   byte data[] = new byte[byteCount];
> >   in.get(data);
> >
> >   try {
> >   ctx.out.write(data);
> > ctx.bytes -= byteCount;
> >   } catch (IOException ioe) {
> > ctx.out.close();
> > nextFilter.exceptionCaught(session, ioe);
> > return;
> >   }
> >
> > }
> > ...
> >
> > Is this the proper way of retrieving the data from a buffer?
>
> Not exactly. Here, you are creating a buffer which will be potentially
> too big. You have to take care of the current position in the
> ByterBuffer when you compute the length of the buffer.
>
> Here, you should do something like :
>
> byte data[] = new byte[in.remaining()];
> in.get(data);
>
> as explained in
> http://docs.oracle.com/javase/7/docs/api/java/nio/Buffer.html#remaining%28%29
>
>

Re: Sending files through TCP Client/Server

Posted by Emmanuel Lécharny <el...@gmail.com>.
Le 31/07/2014 23:59, Carlos Alegria Galicia a écrit :
> Emmanuel,
>
> Thanks a lot for your answer. After considering your comment and debugging
> the client-server communication, I found out that, when the issue "happens"
> (I assume this is happening when session Idle is present and there are
> missing bytes in the state holding object), the server actually reads the
> same amount of bytes sent by the client.
>
> I think then that I am incorrectly reading the input buffer in my Filter. I
> am currently doing this:
>
> ...
> if (in.hasRemaining()) {
>
>     int byteCount = in.limit();
>   byte data[] = new byte[byteCount];
>   in.get(data);
>
>   try {
>   ctx.out.write(data);
> ctx.bytes -= byteCount;
>   } catch (IOException ioe) {
> ctx.out.close();
> nextFilter.exceptionCaught(session, ioe);
> return;
>   }
>
> }
> ...
>
> Is this the proper way of retrieving the data from a buffer?

Not exactly. Here, you are creating a buffer which will be potentially
too big. You have to take care of the current position in the
ByterBuffer when you compute the length of the buffer.

Here, you should do something like :

byte data[] = new byte[in.remaining()];
in.get(data);

as explained in http://docs.oracle.com/javase/7/docs/api/java/nio/Buffer.html#remaining%28%29


Re: Sending files through TCP Client/Server

Posted by Carlos Alegria Galicia <ca...@gmail.com>.
Emmanuel,

Thanks a lot for your answer. After considering your comment and debugging
the client-server communication, I found out that, when the issue "happens"
(I assume this is happening when session Idle is present and there are
missing bytes in the state holding object), the server actually reads the
same amount of bytes sent by the client.

I think then that I am incorrectly reading the input buffer in my Filter. I
am currently doing this:

...
if (in.hasRemaining()) {

    int byteCount = in.limit();
  byte data[] = new byte[byteCount];
  in.get(data);

  try {
  ctx.out.write(data);
ctx.bytes -= byteCount;
  } catch (IOException ioe) {
ctx.out.close();
nextFilter.exceptionCaught(session, ioe);
return;
  }

}
...

Is this the proper way of retrieving the data from a buffer?

Thanks,
Carlos Alegria


On Thu, Jul 31, 2014 at 2:16 AM, Emmanuel Lécharny <el...@gmail.com>
wrote:

> Le 31/07/2014 03:41, Carlos Alegria Galicia a écrit :
> > Hi everyone,
> >
> > I developed a MINA based client and server to send files over a TCP
> > connection. I am using MINA 2.0.7 on both.
> >
> >    - In the client I am sending the file using a header to send file
> >    properties and FileRegion to actually stream the file.
> >    - In the server I created a Filter to read the header, and then
> receive
> >    and locally store the file content 'till the expected number of bytes
> are
> >    received.
> >
> > My problem is that, intermittently, I am not able to receive the last
> chunk
> > of data from the client. By debugging, I found out that the last chunk is
> > actually always shorter than the receive buffer in the server. Is it
> > possible that I am not receiving this last chunk because the
> > Filter.messageReceived() method is not called on my filter because the
> > buffer is not completely filled with data?
>
> No. As soon as 1 single byte of data is received, a messageReceived
> event is generated (but usually, you will get more than 1 byte : you get
> as much data as the socket.read() gave).
>
> You have most certainly some other problem, like your own code is not
> calling your handler when it receives less that what the buffer you are
> storing the incoming data is expected to hold.
>
> It's hard to tell without having the entire code of your server.
>
> Another possibility is that the client is faulty. Are you sure
> everything is correctly sent ? At this point, it's a possibility that
> the code that manage FileRegion is faulty. A code that reproduces the
> problem could help debugging MINA.
>
> At this point, I would suggest you add a Logger Filter to see if you get
> everything on the server side, and that you emit everything on the
> client side.
>
> On my side, I can double check the FileRegion handling in MINA, to be sure.
>
>
>

Re: Sending files through TCP Client/Server

Posted by Emmanuel Lécharny <el...@gmail.com>.
Le 31/07/2014 03:41, Carlos Alegria Galicia a écrit :
> Hi everyone,
>
> I developed a MINA based client and server to send files over a TCP
> connection. I am using MINA 2.0.7 on both.
>
>    - In the client I am sending the file using a header to send file
>    properties and FileRegion to actually stream the file.
>    - In the server I created a Filter to read the header, and then receive
>    and locally store the file content 'till the expected number of bytes are
>    received.
>
> My problem is that, intermittently, I am not able to receive the last chunk
> of data from the client. By debugging, I found out that the last chunk is
> actually always shorter than the receive buffer in the server. Is it
> possible that I am not receiving this last chunk because the
> Filter.messageReceived() method is not called on my filter because the
> buffer is not completely filled with data? 

No. As soon as 1 single byte of data is received, a messageReceived
event is generated (but usually, you will get more than 1 byte : you get
as much data as the socket.read() gave).

You have most certainly some other problem, like your own code is not
calling your handler when it receives less that what the buffer you are
storing the incoming data is expected to hold.

It's hard to tell without having the entire code of your server.

Another possibility is that the client is faulty. Are you sure
everything is correctly sent ? At this point, it's a possibility that
the code that manage FileRegion is faulty. A code that reproduces the
problem could help debugging MINA.

At this point, I would suggest you add a Logger Filter to see if you get
everything on the server side, and that you emit everything on the
client side.

On my side, I can double check the FileRegion handling in MINA, to be sure.