dcsimg
NIO, read a specified number of bytes from a socketchannel<br>
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Florian_Weigand
Posted On:   Thursday, June 27, 2002 06:08 AM

I have a problem with reading from a SocketChannel. I receive messages with fixed sizes (but sizes may vary) over network from a C-server. The few methods in the NIO.channel package that read from the channel simply collect all available bytes when called. The problem is, the C-server may have already sent several messages at that moment and my java program cannot detect where a new message starts. I tried to send the message size in front, but I do not know how to read a specified number of bytes from the channel. Is there a way using the NIO package? Or did I go the wrong way? I would like to use the NIO packa   More>>

I have a problem with reading from a SocketChannel.

I receive messages with fixed sizes (but sizes may vary)

over network from a C-server. The few methods in the

NIO.channel package that read from the channel simply

collect all available bytes when called. The problem is,

the C-server may have already sent several messages at

that moment and my java program cannot detect where a new

message starts.

I tried to send the message size in front, but I do not

know how to read a specified number of bytes from the

channel. Is there a way using the NIO package?

Or did I go the wrong way? I would like to use the NIO

package's channels and no streams.


these are the lines I use from the package:

bb = ByteBuffer.allocateDirect(bufsize);

sockchn = SocketChannel.open();



and that is the "receive"-method:

sockchn.read(bytebuffer);



and I want something like this:

sockchn.read(bytebuffer, number of bytes to read);




The two existing overloaded "read"-methods, I found in

the nio.channel package do not match, they use their offset

& length argument as an array index, not as a byte length.



Please, can you help me ?



Florian

   <<Less
About | Sitemap | Contact