socket read and write bytes - need some help

Hi

I'm new to socket programing,

I need to write byte array to socket and I'm looking for best practice + examples references.

Most of the examples I found where talking about writing and reading chars using Writer, and Reader.

I will be very gratefull, for good references.

I need it badly in my work.

Thanks

Oded

[365 byte] By [odedetzion1a] at [2007-10-3 7:25:56]
# 1

Same thing, only you tie the buffer (reader/writer) to a socket. If you use TCP sockets - then add about 20% to get the total data sent over the network, while you can just use the bytes read from the buffer as the data payload size.

If you use UDP, then you'll need to change some things as well, and the data payload will contain some of the components of TCP if you retransmit or try to make it more reliable.

watertownjordana at 2007-7-15 2:24:38 > top of Java-index,Archived Forums,Socket Programming...
# 2

thanks,

1. I can't use writer and reader as that data is binary.

2. Are there any performance issues that I have to know about?

3. Can you send me any code sample, please?

4. Any references on the internet...

5. I'm trying to understand the idea behind the folowing code, which is part of the socket read-write: (sorry for the indentation)

Thanks again

========================================

private byte[] readWhileAvailable(InputStream inputStream) throws IOException {

byte[] samllBuff = new byte[512];

byte[] bigBuff = new byte[4096];

// will use it only if data is more then 1 bigBuff

Vector collectBigBuff = null;

int byteCount = inputStream.read(samllBuff);

if (byteCount < 0)

return new byte[0];

System.arraycopy(samllBuff, 0, bigBuff, 0, byteCount);

int index = byteCount;

int totalByteCount = byteCount;

int lastBufferCount = byteCount;

// if there is more data

if (inputStream.available() > 0) {

while ((inputStream.available() > 0) && byteCount > 0) {

byteCount = inputStream.read(samllBuff);

if ((totalByteCount + byteCount) <= 4096) {System.arraycopy(samllBuff, 0, bigBuff, index, byteCount);

} else {

// in case data is more then 4096 store current data

// empty big buffer for new reading

if (collectBigBuff == null) {collectBigBuff = new Vector();

}

byte[] toStore = new byte[lastBufferCount]; System.arraycopy(bigBuff, 0, toStore, 0, lastBufferCount);

collectBigBuff.add(toStore.clone());

index = 0;

bigBuff = new byte[4096];lastBufferCount = 0;

}

index = index + byteCount;

totalByteCount = totalByteCount + byteCount;

lastBufferCount = lastBufferCount + byteCount;

}

}

byte[] buff = new byte[totalByteCount];

// one big buffer was eangh

if (collectBigBuff == null) {

System.arraycopy(bigBuff, 0, buff, 0, totalByteCount);

} else {

int ind = 0;

for (int i = 0; i < collectBigBuff.size(); i++) {

byte[] bf = (byte[]) collectBigBuff.get(i);

System.arraycopy(bf, 0, buff, ind, bf.length);

ind = ind + bf.length;

}

}

return buff;

}

odedetzion1a at 2007-7-15 2:24:38 > top of Java-index,Archived Forums,Socket Programming...
# 3

That code is 100% pure rubbish from start to finish. Forget about it. Throw it away. Do it now, do not pass GO, do not collect $200.

You can rewrite that in 6 lines of code without the umpteen buffers and zillion bugs and data movement and general inefficiency, but you don't want to just read an input stream into a possibly infinite byte array while input is available in the first place. This concept makes numerous assumptions which are simply false, such as that memory is infinite and all the data arrives together, so the whole thing was pointless to begin with. I haven't seen such drivel for over a week.

What you really want to do is to read the input stream and put it somewhere a piece at a time and stop at end of stream. See the java.io package and the Sockets tutorial that comes with the JDK.

Just use BufferedInputStream and BufferedOutputStream with reasonably large buffers.

ejpa at 2007-7-15 2:24:38 > top of Java-index,Archived Forums,Socket Programming...
# 4

Thanks,

Still wondering what is the practical use of available() method.

Is there any place where I can read more about sockets examples, and programing.

Most of the examples I found are reading of characters or objects

I wrote the following, is it ok?

private byte[] readFromStream(InputStream inputStream) throws IOException {

int bufferSize = 2048;

byte[] buf = new byte[bufferSize];

BufferedInputStream in = new BufferedInputStream(inputStream);

int index = 0;

while(true) {

int oneByte = in.read();

if(oneByte < 0) { //end of stream

break;

}

if(index < buf.length) {

buf[index] = (byte)oneByte;

index++;

}

else { //reallocate buffer

buf = reallocate(buf,bufferSize);

}

}

return buf;

}

private byte[] reallocate(byte[] array, int addSize) {

byte[] tempArray = new byte[array.length + addSize];

System.arraycopy(array, 0, tempArray, 0, array.length);

return tempArray;

}

odedetzion1a at 2007-7-15 2:24:38 > top of Java-index,Archived Forums,Socket Programming...
# 5

You have to get rid of this concept of turning the entire input stream into a byte[] array. Redesign your interface so it knows where to put the data as well as where it gets it from. Something like:

int readAndWrite(InputStream in, OutputStream out) throws Exception;

The input stream could contain gigabytes!

Basic I/O code for bytes goes like this:

InputStream in;

OutputStream out;

byte[] buffer = new byte[16384]; // or bigger if you like, not much smaller please

int count;

while ((count = in.read(buffer)) > 0)

out.write(buffer,0,count);

out.flush(); // or out.close(); if you are really finished with it

in.close(); // when you are finished with it.

If you must turn input streams into byte arrays, have a look at java.io.ByteArrayOutputStream, it's already done.

And have a look at the I/O and Sockets tutorials in the JDK Javadoc.

ejpa at 2007-7-15 2:24:38 > top of Java-index,Archived Forums,Socket Programming...
# 6
1. ALL data on a computer/network is binary, it's all they know!
watertownjordana at 2007-7-15 2:24:38 > top of Java-index,Archived Forums,Socket Programming...
# 7
There are many examples of writing/reading byte streams - just check out the Java Tutorials on writing network apps, or file i/o.
watertownjordana at 2007-7-15 2:24:38 > top of Java-index,Archived Forums,Socket Programming...
# 8
1. Why didn't you wrap it with bufferedStream as you recommended?2. What is the available() method is practically used for?thank you very much,Oded
odedetzion1a at 2007-7-15 2:24:38 > top of Java-index,Archived Forums,Socket Programming...
# 9

> 1. Why didn't you wrap it with bufferedStream as you

> recommended?

Because I''m reading 16k bytes at a time into my own buffer, so there is no advantage - in fact there's the disadvantage of double data movement. If I was reading one byte at a time, as per your examples, I would certainly buffer it.

> 2. What is the available() method is practically used

> for?

Practically nothing.

ejpa at 2007-7-15 2:24:38 > top of Java-index,Archived Forums,Socket Programming...
# 10

In my case I don't read bytes from the socket input stream just to copy them to the output stream, there is a process in the middle.

Anyway I can adopt the idea of using the ByteArrayOutputStream, instead of realocating memory by myself.

You have been very helpfull to me...

thanks

odedetzion1a at 2007-7-15 2:24:38 > top of Java-index,Archived Forums,Socket Programming...
# 11
> You have been very helpfull to me...> thanksUm, yeah, that's because ejp rewrote the code for the poster.
watertownjordana at 2007-7-15 2:24:38 > top of Java-index,Archived Forums,Socket Programming...