Socket Client , abnormal behavior when sending byte

M

Max

Hi all ,
I have write a socket that send 50Kbyte of data stream at server writed in
C++ .
I have abnormal behavior when i send data from Java => to => server .

The data arrive on the server "not" in order that i have sended it :-(

frist sending operation (total : 50KByte that I have splited in 5 operation
of 10KByte)
sending (10Kbyte)
sending (10Kbyte)
sending (10Kbyte)
sending (10Kbyte)
sending (10Kbyte)

first sending receiver :
ok ! The server receive
pack 1,
pack 2,
pack 3,
pack 4,
pack 5,

second sendig :
sending (10Kbyte)
sending (10Kbyte)
sending (10Kbyte)
sending (10Kbyte)
sending (10Kbyte)

second sending receiver :
ok ! The server receive
pack 1,
pack 2,
pack 3,
pack 4,
pack 5,

"n"-sending operation :
sending (10Kbyte)
sending (10Kbyte)
sending (10Kbyte)
sending (10Kbyte)
sending (10Kbyte)

first sending receiver :
no ! The server receive
pack 2,
pack 4,
pack 5,
pack 1,
pack 3,

i not receive the "pack" in sequence : why ???

thank all
max
 
R

Ryan Stewart

Max said:
Hi all ,
I have write a socket that send 50Kbyte of data stream at server writed in
C++ .
I have abnormal behavior when i send data from Java => to => server .

The data arrive on the server "not" in order that i have sended it :-(
*snip*

Unless you can give a reason why it shouldn't behave this way, my answer is
that network protocols don't guarantee that the first message sent is the
first message received. They only guarantee that messages are taken apart
and put back together in the right order.
 
J

Jon A. Cruz

Max said:
i not receive the "pack" in sequence : why ???

thank all
max


1) Are you using UDP instead of TCP?

2) Are you closing the connection between each 10K?

3) Did you know that your 10K send could arrive in many smaller packets?
 
M

Max

1) Are you using UDP instead of TCP?

// get the socket !
Socket s= new Socket("127.0.0.1",5000);

// get the outputstream
OutputStream myO = s.getOutputStream();

// send the first 10 KByte and wait 1second
myO.write(50_kb_total_stream,0,10*1024);
myThread.sleep(1000);

// send the next 10KByte and wait 1 second
myO.write(50_kb_total_stream,10*1024,20*1024);
myThread.sleep(1000);

// and so on....
myO.write(50_kb_total_stream,20*1024,30*1024);
myThread.sleep(1000);

// and so no...
myO.write(50_kb_total_stream,30*1024,40*1024);
myThread.sleep(1000);

myO.write(50_kb_total_stream,40*1024,50*1024);
myThread.sleep(1000);
2) Are you closing the connection between each 10K?

no !
3) Did you know that your 10K send could arrive in many smaller packets?

Yes :
on the server I have a listener that receive 5KByte at time :

pseudo-code :

while(true)
{

// build the buffer
output = new byte[5*1024];

// block and read the data : attention , is possible that data
// arrived is "less" than 5KByte . Example : 16byte , 1024byte .
// So you must "trim" the array and "extract" only byte arrived and NOT
all length of output
int byte_read = myListener.receive (5*1024,output);

// send bytes arrived , now I must rebuild the correct sequence !
this.addNewBlockReceived(byte_read,output);
}
 
M

Max

Unless you can give a reason why it shouldn't behave this way, my answer
is
that network protocols don't guarantee that the first message sent is the
first message received. They only guarantee that messages are taken apart
and put back together in the right order.

TCP-IP gaurante that first information byte[] sended is the first byte[]
arrived
for single sending operation .

but :

TCP-IP don't guarante that first information byte[] sended is the first
byte[] arrived for
different sending operation ?

max
 
J

Joe Randstein

Max said:
// send the first 10 KByte and wait 1second
myO.write(50_kb_total_stream,0,10*1024);
myThread.sleep(1000);

// send the next 10KByte and wait 1 second
myO.write(50_kb_total_stream,10*1024,20*1024);
myThread.sleep(1000);

// and so on....
myO.write(50_kb_total_stream,20*1024,30*1024);
myThread.sleep(1000);

You send 10KB only at first time. Next time you send 20KB, then 30KB
and so on. The third parameter (which is length) should be always
10*1024 if I understood your correctly.
 
J

Jon A. Cruz

Max said:
// get the socket !
Socket s= new Socket("127.0.0.1",5000);

// get the outputstream
OutputStream myO = s.getOutputStream();

// send the first 10 KByte and wait 1second
myO.write(50_kb_total_stream,0,10*1024);
myThread.sleep(1000);

Ooohhh. That's bad.

sleep() is a static method that operates on whatever the current thread
happens to be.

Instead of
myThread.sleep(1000);
use
Thread.sleep(1000);

Otherwise you're liable to get confused about your threading.

// send the next 10KByte and wait 1 second
myO.write(50_kb_total_stream,10*1024,20*1024);

Aha. That's the main problem that Joe pointed out.

Check the Java docs
http://java.sun.com/j2se/1.4.2/docs/api/java/io/OutputStream.html
void write(byte[] b, int off, int len)
Writes len bytes from the specified byte array starting at offset off
to this output stream.

So as you're increasing the offset, you're also increasing the size.
 
M

Max

You send 10KB only at first time. Next time you send 20KB, then 30KB
and so on. The third parameter (which is length) should be always
10*1024 if I understood your correctly.

ops !
Stupid !

The code is right , this email is wrong : I have writed very fast this email
and I had forget
the Java code :)

max
 
M

Max

sleep() is a static method that operates on whatever the current thread
happens to be.

sorry , I have writed "pseudo-code" . This is right code :
Thread.currentThread().sleep(1000);

AnyWay I have try to use OutputStream to a File ,
I have sended many and many 10KByte block of information .

In the "debug" file , the sequence is correct :
this mind that I have problem only on the Socket .

Max
 
E

Esmond Pitt

Why are you writing in 10k blocks with sleeps in between? Why not just
write the whole thing with one write, or at least without sleeps, and
let TCP do its own flow control? It's much better at that than your
present code is.

EJP
 
M

Max

Why are you writing in 10k blocks with sleeps in between?

I want to limit the data-transfer at 10KByte /s .
I know that this method isn't very precise to limit the data-transfer at
10KByte/s .
Why not just
write the whole thing with one write, or at least without sleeps, and
let TCP do its own flow control?

Because if you have a connection that send 50KByte/s ,
I don't want than you in 1sec send at server 50KByte .

I want that you send 10KByte every (sleep) 1 sec !

max
 
E

Esmond Pitt

A much better way to do this is to limit the socket send buffer to
bandwidth*delay if you know the delay over the link, or if you want to
choke all senders, to limit the receiving socket's receive buffer.
 

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

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top