How to read byte array on sockets?

A

ami

Hello,

I have problem while reading array of bytes. I have sent
array of bytes over the socket using DataOutputStream. Here is the code
for that:


byte[] ethFrame;
out.write(ethFrame, 0, ethFrame.length);

And tried to read on the other end with

in.read(bytes,offset, length);
or
in.readFully(bytes,offset, length);

but I could not print it out?

Can any one help me to read byte array ?

Please help me!!
 
Z

zero

Hello,

I have problem while reading array of bytes. I have sent
array of bytes over the socket using DataOutputStream. Here is the code
for that:


byte[] ethFrame;
out.write(ethFrame, 0, ethFrame.length);

And tried to read on the other end with

in.read(bytes,offset, length);
or
in.readFully(bytes,offset, length);

but I could not print it out?

Can any one help me to read byte array ?

Please help me!!

in.read should work.

byte[] bytes = new byte[100];
in.read(bytes, 0, b.length);

for(byte b : bytes)
Sytem.out.print(b + " ");
 
M

Matt Humphrey

ami said:
Hello,

I have problem while reading array of bytes. I have sent
array of bytes over the socket using DataOutputStream. Here is the code
for that:


byte[] ethFrame;
out.write(ethFrame, 0, ethFrame.length);

And tried to read on the other end with

in.read(bytes,offset, length);
or
in.readFully(bytes,offset, length);

but I could not print it out?

Can any one help me to read byte array ?

You'll have to give us more to go on. In what way does it not work? What
are the values for bytes, offset and length? How does the reader know how
many bytes to read--TCP is stream (not packet) oriented and you don't
necessarily receive the data in the same blocks in which you sent it. Show
your real code or produce a short program that demonstrates the problem. (My
off-the-cuff guess is that you're writing (for example) 80 bytes or whatever
ethFrame.length turns out to be, but your read buffer is 100 bytes and
you're waiting for it to fill up, which it never will. You must know the
length in advance or have some way of knowing when to stop reading.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top