@joelkatz said:
Why would you expect writing zero bytes to a socket to silently do nothing?
http://java.sun.com/j2se/1.5.0/docs/api/java/io/OutputStream.html
public void write(byte[] b,
int off,
int len)
throws IOException
"The write
method of OutputStream
calls the write method of one argument on each of the bytes to be written out"
So calling it with len = 0 should be equivalent to calling the one-byte write method 0 times, ie doing nothing.
@joelkatz said:
And you're losing packets because TCP does not preserve message boundaries. There is no requirement that each send operation result in one packet being sent. A single send operation can result in two packets being sent and two send operations can result in one packet being sent. TCP is a byte-stream protocol.
Could you please quote the part of my post where I said I was losing
packets? I've just re-read what I wrote and can't see that anywhere.
Of course it's reasonable to assume that a complete stranger you've only just met on a forum probably doesn't know what they're talking about, because nine times out of ten you'll be completely correct. But believe me, I know what a byte stream protocol is, because I've had to pound this one into enough thick skulls myself during my time as a programmer.
Got a Blackberry JDE handy? Try this:
import java.io.*;
import javax.microedition.io.*;
class Main extends Thread
{
public static void main(String[] args)
{
Main obj = new Main();
obj.start();
}
public void run()
{
SocketConnection sc;
String url = "socket://192.168.5.10:12345;deviceside=true";
InputStream is;
OutputStream os;
OutputStreamWriter osw;
try
{
sc = (SocketConnection) Connector.open(url);
is = sc.openInputStream();
os = sc.openOutputStream();
test(os);
}
catch(Exception e)
{
System.out.println("Exception: "+e);
}
}
public void test(OutputStream os)
{
byte b = 0x00;
for (int i=6000;i<8000;i++)
{
System.out.println("Size is " + i + "\n");
byte buf[] = new byte[i];
for (int j=0;j<i;j++)
buf[j] = b;
try
{
os.write(buf);
b++;
}
catch(java.io.IOException e)
{
System.out.println("IOException: "+e);
}
}
}
}
See how it writes byte values that increase by 1 at each step? Point it at something like "nc -l -p 12345 | xxd" as the listener. Here's a sample of what I get after it's been running for a few seconds:
004fed0: bfbf bfbf bfbf bfbf bfbf bfbf bfbf bfbf ................
004fee0: bfbf bfbf bfbf bfbf bfbf bfbf bfbf bfbf ................
004fef0: bfbf bfbf bfbf bfbf c9c9 c9c9 c9c9 c9c9 ................
004ff00: c9c9 c9c9 c9c9 c9c9 c9c9 c9c9 c9c9 c9c9 ................
004ff10: c9c9 c9c9 c9c9 c9c9 c9c9 c9c9 c9c9 c9c9 ................
004ff20: c9c9 c9c9 c9c9 c9c9 c9c9 c9c9 c9c9 c9c9 ................
Where did bytes 0xc0 to 0xc8 go?