Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 Mar : Re: SendBuf - how to do with tmemorystream?

www.cryer.info
Managed Newsgroup Archive

Re: SendBuf - how to do with tmemorystream?

Subject:Re: SendBuf - how to do with tmemorystream?
Posted by:"Angela Bodnar" (angela.bodn..@noris.org)
Date:2 Aug 2006 12:41:07

I did not receive a response from anyone, so I found some code
which I modified (see below).  This code works until I get to
11504 bytes.  At which point it starts reading 0 bytes and
sends no more data, but continues forever in the loop.
It works for streams <11504 bytes.
Also, the Sleep does not work.  It finds -1 bytes sent, but
does not wait.
Any ideas?
Thanks,
Angela

procedure SendBinaryData(Socket: TCustomWinSocket; Stream: TStream);
var iBytesRead,  iSize,  iDataSent: Int64;
   buf: array [0..1023] of Byte;
begin
try
ZeroMemory(@buf, SizeOf(buf));
iDataSent := 0;
iSize := Stream.Size;
sendlen := Socket.SendBuf(iSize, SizeOf(iSize));
while iDataSent < iSize do
  begin
  iBytesRead := Stream.Read(buf, SizeOf(buf));  // read into buf
  sendlen := Socket.SendBuf(buf, iBytesRead);   // send buf
  if sendlen = -1 then  // didn't send anything
    begin // blocking send, resend
    SendWait;   // 8/2/06 write to sendbuff.txt
    windows.Sleep(250); // wait 250 milliseconds
    Continue;
    end;
  Inc(iDataSent, iBytesRead); // increment
  SendBuff;  // 8/2/06 write to sendbuff.txt
  end;
   except
    on E: Exception do
    begin
     SendFail;
     merrorout := control_number_out + ' ' + inttostr(seq) + ' SendBinaryData fail' + Inttostr(E.HelpContext) + ' ' + E.Message;
     frmEMERSend.memo3.Text := 'SendBinaryData failed ' + mtextout;
     frmEMERSend.lblSentTime.Caption := '';
     HandleError;  // also inserts into leberror and sets mSendError = true
    end;
end; // try/except
end;


"Angela Bodnar" <angela.bodnar@noris.org> wrote:
>
>Hello,
>I have a app that currently uses SendStream or SendText from
>a ClientSocket.socket.  I want to change it to use SendBuf
>because it clogs up after I try to send something too large
>and I need to start sending images anyway.
>I like your sample code here, but how do I use the SendBuf
>function with a TMemoryStream?  Also, I do not have the option
>of disconnecting the socket on failure because it must stay
>up all the time since it sends and receieves asyncronously
>continually.
>Thanks,
>Angela
>
>
>"Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> wrote:
>>
>>"Ivan Kossey" <spamers.sollen.sterben@cablenet.de> wrote in message
>>news:4422d0b1@newsgroups.borland.com...
>>
>>> It is not mentioned
>>> 1. What is the type of buf (PChar, string,...)?
>>
>>Actually, it does.  It is an untyped parameter, analagous to a Pointer.
>>Which means that you can pass anything to it that represents a memory
>>address.
>>
>>> 2. What integer value returns the function?
>>
>>It returns how many bytes were actually accepted.  It can be less then the
>>amount specified by the bufsize parameter, if there is not enough room for
>>the entire buffer.  If the the return value indicates that not all of the
>>buffer was sent, then you have to adjust your input pointer accordingly and
>>call SendBuf() again to sent the remaining data.
>>
>>Here are a few examples:
>>
>>    var
>>        buffer: array[0..11] of Byte;
>>        idx, sent: Integer;
>>    begin
>>        // fill the buffer as needed ...
>>        idx := 0;
>>        while idx < SizeOf(buffer) do
>>        begin
>>            sent := ...SendBuf(buffer[idx], SizeOf(buffer)-idx);
>>            if sent = -1 then begin // blocking send, resend
>>                Sleep(50);
>>                Continue;
>>            end;
>>            if sent = 0 then Break; // disconnected
>>            Inc(idx, sent);
>>        end;
>>    end;
>>
>>
>>    var
>>        buffer: array of Byte;
>>        idx, sent: Integer;
>>    begin
>>        SetLength(buffer, 12);
>>        // fill the buffer as needed ...
>>        idx := 0;
>>        while idx < Length(buffer) do
>>        begin
>>            sent := ...SendBuf(buffer[idx], Length(buffer)-idx);
>>            if sent = -1 then begin // blocking send, resend
>>                Sleep(50);
>>                Continue;
>>            end;
>>            if sent = 0 then Break; // disconnected
>>            Inc(idx, sent);
>>        end;
>>    end;
>>
>>
>>    var
>>        s: String;
>>        idx, sent: Integer;
>>    begin
>>        s := 'something';
>>        idx := 0;
>>        while idx < Length(s) do
>>        begin
>>            sent := ...SendBuf(PChar(s)+idx, Length(s)-idx);
>>            if sent = -1 then begin // blocking send, resend
>>                Sleep(50);
>>                Continue;
>>            end;
>>            if sent = 0 then Break; // disconnected
>>            Inc(idx, sent);
>>        end;
>>    end;
>>
>>
>>    var
>>        value: Integer;
>>        ptr: PByte;
>>        idx, sent: Integer;
>>    begin
>>        value := 12345;
>>        idx := 0;
>>        while idx < SizeOf(i) do
>>        begin
>>            sent := ...SendBuf(PByte(@value)[idx], SizeOf(value)-idx);
>>            if sent = -1 then begin // blocking send, resend
>>                Sleep(50);
>>                Continue;
>>            end;
>>            if sent = 0 then Break; // disconnected
>>            Inc(idx, sent);
>>        end;
>>    end;
>>
>>
>>Gambit

Replies:

In response to:

www.cryer.info
Managed Newsgroup Archive