Socket : can i limit the applet bandwidth (data-transfer) client-to-server ?

M

Max

Hello ,
I have builded ServerSide Socket with Java and ClientSocket still in Java
(applet) .

The Client send very amouth of data to server (1Mbyte, 3Mbyte) .
Now , I need to limit the bandwidth that Applet client can transfer to
server .

Example :
- 30 KByte/s
- 50 KByte/s

The client can't send more that 30KByte/s to server , example .
In order to do this , I think that there are only one solution :

1 - buffer the data in great byte[] array that store ALL data that client
must send!
2 - send the first 30KByte buffered
3 - use Thread.sleep(1000)
4 - send the NEXT 30KByte buffered
5 - repet from [4]
6 - byte[] array is totally send , finish...

Before starting to write this , i want to ask if there are other solutions .
Thanks a lot for any suggestions ...

Best Regard,
max
 
M

Matt Parker

Max said:
Hello ,
I have builded ServerSide Socket with Java and ClientSocket still in Java
(applet) .

The Client send very amouth of data to server (1Mbyte, 3Mbyte) .
Now , I need to limit the bandwidth that Applet client can transfer to
server .

Example :
- 30 KByte/s
- 50 KByte/s

The client can't send more that 30KByte/s to server , example .
In order to do this , I think that there are only one solution :

1 - buffer the data in great byte[] array that store ALL data that client
must send!
2 - send the first 30KByte buffered
3 - use Thread.sleep(1000)
4 - send the NEXT 30KByte buffered
5 - repet from [4]
6 - byte[] array is totally send , finish...

Before starting to write this , i want to ask if there are other solutions
. Thanks a lot for any suggestions ...

Best Regard,
max

Almost there, but you don't need to create the complete byte array first.
I've written this before for transferring large files to an applet, via
SOAP in my case, but would apply to sockets equally well, except you'd make
a new socket connection for each chunk (like HTTP) and this will act as
your wait but free up other threads as well. Your not limiting the
bandwidth here (though that could be possible with this method), but
allowing other threads to access their resources as well.

You need to keep the class dealing with the chunking instantiated on the
server for the duration of the read and have some kind of mechanism to
identify which file you want the chunk for (I maintained a pool and used a
unique token to identify the file). I can't give you the whole solution
since I don't own the code, but I'll give you some stripped down code that
you can get to grips with. And on re-reading some of my (old) code I can
see a glaring inefficiency too. See if you can spot that as well...

Matt

private int chunkSize = 16000; // ~16kb
private boolean readOpened;
private BufferedInputStream readBuffer = null;

public boolean openForRead()
{
try
{
readBuffer = new BufferedInputStream(new FileInputStream(new
File(fileName)));
readOpened = true;

return (true);
}
catch (IOException e)
{
// Clean up streams

return (false);
}
}

public DataChunkBean getNextChunk()
{
if (!readOpened)
{
return (null);
}

byte[] byteBuffer = new byte[chunkSize];
DataChunkBean chunkBean = new DataChunkBean();

try
{
int bytesRead = readBuffer.read(byteBuffer, 0, byteBuffer.length);

chunkBean.setChunkLength(bytesRead);
chunkBean.setChunkArray(byteBuffer);

if (bytesRead == -1)
{
chunkBean.setLastChunk(true);
readBuffer.close();
}

return (chunkBean);
}
catch (IOException e)
{
return (null);
}
}
 
E

Esmond Pitt

To limit the incoming bandwidth use a smaller socket receive buffer, set
to (desired bandwidth) x delay, where bandwidth is in bytes/seconf and
delay is in seconds for the link you are expecting to use. Product is in
bytes. The sender cannot send faster than (desired bandwidth).

EJP
 
M

Max

bytes. The sender cannot send faster than (desired bandwidth).


OK , if I use :

byte[] b = new byte[1024*10];
read (b);
Thread.sleep(1000);

to read from the Socketclient and I send 1024*20 bytes of data ,
the receiver will receive the first 1024*10 bytes of data .

After this , the SENDER not send the next 1024*10 Kbyte of data
before that the client recall :

byte[] b2 = new byte[1024*10];
read (b2);

It's right ?

max
 
J

Jon A. Cruz

Max said:
bytes. The sender cannot send faster than (desired bandwidth).

[SNIP]
It's right ?

Just remember that you have the set of the entire world's
internet-connected computers as potential buffers inbetween. Keep that
in mind.
 
E

Esmond Pitt

No, it's not right. It has nothing to do with application buffers. I am
talking about the socket receive buffer, which you control with
Socket.setReceiveBufferSize(). The sending TCP's send buffer cannot
write to the network unless it knows the receiving TCP has space in the
target socket's receive buffer.
bytes. The sender cannot send faster than (desired bandwidth).



OK , if I use :

byte[] b = new byte[1024*10];
read (b);
Thread.sleep(1000);

to read from the Socketclient and I send 1024*20 bytes of data ,
the receiver will receive the first 1024*10 bytes of data .

After this , the SENDER not send the next 1024*10 Kbyte of data
before that the client recall :

byte[] b2 = new byte[1024*10];
read (b2);

It's right ?

max
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top