Default buffer size used in Buffered Input/output Stream...

alan1968

Active Member
Licensed User
Longtime User
I have in my logs this message

Default buffer size used in Buffered Input/output Stream constructor. It would be better to be explicit if an 8k buffer is required.

Is it possible to change the buffer in / out a TCP connection with B4A?

or I have to do this in java
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This message is not related to the sockets.
You can set the buffer size with the reflection library:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim socket1 As Socket
   socket1.Initialize("socket1")
   SetBufferSize(socket1, 8192)
End Sub

Sub SetBufferSize(Sock As Socket, Size As Int)
   Dim r As Reflector
   r.Target = Sock
   Dim native_socket As Object
   native_socket = r.GetField("socket")
   r.Target = native_socket
   r.RunMethod2("setSendBufferSize", Size, "java.lang.int")
   r.RunMethod2("setReceiveBufferSize", Size, "java.lang.int")
End Sub
 
Upvote 0

alan1968

Active Member
Licensed User
Longtime User
thx! Erel
have the same message in my logs

Need we add that line?

Socket1.Connect(adr ip,port,timeout)

Ideally I need a 8192 byte buffer for a given IP address and a port but I do not know how.

:BangHead:
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Default buffer size

Erel,
I have integrated your routine into my code, but see no difference in the incoming buffer size.
The application sending astream data is sending in 64,500 byte arrays (chunks).
I set the buffersize using your sub routine and continue to receive partial chunks of data in the range of 1000 bytes.
Do i need to re-initialize the socket or do you have advice on this?
thanks,
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
default buffer size used in Buffered Input/Output

Thanks, Erel. My communications do include a "two byte header" specifying the length of the packet. I'll create a packet processor to accumulate the data and then pass the completed packet (based upon the length bytes) to the data processor.
Interesting they chose such a small packet size. I guess the blocking issue determines the max.
 
Upvote 0

sioconcept

Active Member
Licensed User
Longtime User
I've another Method, Beacuse sometimes SocketSize don't work everytimes

This method cut the Stream in little buffers, so Socket isn't too big to be send.

Use "FullBuffer" as String or Directly "Send" as Byte; be carefull with Charset.

B4X:
   BufferSizeMax = 2048 'Put here the max size of desired buffer
   Charset = "UTF-8" ' OR ISO-8859-1 if you are in latin
   Send = FullBuffer.GetBytes(Charset)
   Log("Bytes Send : " & Send.Length)

   For i = 0 To Send.Length -1 Step BufferSizeMax
      BufferSize = Send.Length - 1
      If (i + BufferSizeMax) > (Send.Length - 1) Then 
         BufferSize = Send.Length Mod BufferSizeMax
      Else
         BufferSize = BufferSizeMax
      End If
      AStreams.Write2(Send, i, BufferSize)
   Next

   Do While AStreams.OutputQueueSize > 0
   Log("Astreams Queue : " & AStreams.OutputQueueSize)
      Sleep(10)
   Loop
   AStreams.Close

Use a timer or the sleep function to temporize the out buffer.

B4X:
Sub Sleep(Ms As Int)
   Dim Ti As Long
   Ti = DateTime.Now + (Ms)
   Do While DateTime.Now < Ti
      DoEvents
   Loop
End Sub

Maybe this can help you :sign0087:
 
Upvote 0

dunski

Member
Licensed User
Longtime User
Was useful to me thanks

I've another Method, Beacuse sometimes SocketSize don't work everytimes

This method cut the Stream in little buffers, so Socket isn't too big to be send.

Use "FullBuffer" as String or Directly "Send" as Byte; be carefull with Charset.

B4X:
   BufferSizeMax = 2048 'Put here the max size of desired buffer
   Charset = "UTF-8" ' OR ISO-8859-1 if you are in latin
   Send = FullBuffer.GetBytes(Charset)
   Log("Bytes Send : " & Send.Length)

   For i = 0 To Send.Length -1 Step BufferSizeMax
      BufferSize = Send.Length - 1
      If (i + BufferSizeMax) > (Send.Length - 1) Then 
         BufferSize = Send.Length Mod BufferSizeMax
      Else
         BufferSize = BufferSizeMax
      End If
      AStreams.Write2(Send, i, BufferSize)
   Next

   Do While AStreams.OutputQueueSize > 0
   Log("Astreams Queue : " & AStreams.OutputQueueSize)
      Sleep(10)
   Loop
   AStreams.Close

Use a timer or the sleep function to temporize the out buffer.

B4X:
Sub Sleep(Ms As Int)
   Dim Ti As Long
   Ti = DateTime.Now + (Ms)
   Do While DateTime.Now < Ti
      DoEvents
   Loop
End Sub

Maybe this can help you :sign0087:

HI there Sio,

I just stumbled on this post as I was searching all the documentation and forum for some kind of a sleep function.
This is the only thing I could find.
So I used it and it works brilliant for my project where I have to send many http requests and needed to just slow it down a bit.
Thanks a million.

Dunski:sign0098:
 
Upvote 0

AlexChiu

New Member
Licensed User
Longtime User
Hi Erel,I use socket to receive webcam picture from pc,but the pictures size too large.How can I coding to success receive?thanks!!
 
Upvote 0
Top