Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 Apr : Re: Problem send/recv a string using winsock

www.cryer.info
Managed Newsgroup Archive

Re: Problem send/recv a string using winsock

Subject:Re: Problem send/recv a string using winsock
Posted by:"Jean-Francois Goulet" (goulet..@vif.com)
Date:Sat, 8 Apr 2006 22:09:47

Hi! Sorry for making everyone lose their time but I found the error! The
thing is that this code was developped in an exe before and has been moved
into a dll. To do so, I had to change all the String variables into PChar in
order to be safe with other languages. When doing this, I forgot to actually
change the way the send function was called. The way I do it is probably
good for String variables but for PChar, I need to call it this way, using
the dereferenced pointer instead:

RemotePath: PChar;
SocketSend(S, RemotePath^, Length(RemotePath), 0, ErrMsg);

I know, RemotePath is actually a PChar, not a String as I posted previously.
When I posted that code I though that it would be good for me to mentioned
the type of the RemotePath and FileName variable so I typed it instead of
copy/paste. My mistake!

Sorry about that and thanks anyway!
//JF

"Jean-Francois Goulet" <gouletje@vif.com> wrote in message
news:44383d4c$1@newsgroups.borland.com...
> Hi! I'm attempting to setup a remote debugging feature in my IDE and right
> now, I'm stuck with the following problem: I'm trying to send a string but
> I only receive part of the string. More concretely, that string is a path.
> The problem is that the string is sent correctly (at least is seems like
> it is) and when i'm trying to receive it using winsock's recv function i
> only get all the string besides the first character. Now, I suspect the
> problem is more due to the buffer i use when i call recv.
>
> Here is the code that's related to the send part:
>
> RemotePath: String;
> SocketSend(S, RemotePath[1], Length(RemotePath), 0, ErrMsg);
>
> // This function send datas to the specified socket
> function SocketSend(S: TSocket; var Buf; Len, Flags: Integer;
> ErrorMessage: PChar): Boolean;
> var
>  Status: Integer;
> begin
>  try
>    Result := True;
>
>    // Send data length
>    Status := send(S, Len, sizeof(Len), Flags);
>    if Status <> sizeof(Len) then
>      raise ESocketException.Create(ErrorMessage);
>
>    // Send datas
>    Status := send(S, Buf, Len, Flags);
>    if Status <> Len then
>      raise ESocketException.Create(ErrorMessage);
>  except
>    on E: ESocketException do
>    begin
>      // Display error message
>      Result := False;
>      Application.MessageBox(PChar(E.Message), 'LuaEdit',
> MB_OK+MB_ICONERROR);
>    end;
>  end;
> end;
>
>
> And now here is the code related to the receive part:
>
>
> FileName: String;
>
> SetLength(FileName, 2048);
> SocketReceive(S, FileName[1], 0, ErrMsg);
>
> // This function recieve datas from the specified socket
> function SocketReceive(S: TSocket; var Buf; Flags: Integer; ErrorMessage:
> PChar): Boolean;
> var
>  Len, Status, TotalRecv, TotalTime: Integer;
>  StartedTime: Cardinal;
>  ErrMsg: String;
> begin
>  try
>    TotalRecv := 0;
>    TotalTime := 0;
>    StartedTime := GetTickCount();
>
>    // Recieve data length4
>    while ((TotalRecv <> sizeof(Len)) and (TotalTime < iTimeOut)) do
>    begin
>      Status := recv(S, Len, sizeof(Len), Flags);
>      ErrMsg := SysErrorMessage(GetLastError());
>
>      if Status = sizeof(Len) then
>        TotalRecv := TotalRecv + Status;
>
>      TotalTime := GetTickCount() - StartedTime;
>      Application.ProcessMessages;
>      Sleep(20);
>    end;
>
>    if TotalTime >= iTimeOut then
>      raise ESocketException.Create(ErrorMessage + ErrMsg);
>
>    TotalRecv := 0;
>    TotalTime := 0;
>    StartedTime := GetTickCount();
>
>    // Recieve datas
>    while ((TotalRecv <> Len) and (TotalTime < iTimeOut)) do
>    begin
>      Status := recv(S, Buf, Len, Flags);
>      ErrMsg := SysErrorMessage(GetLastError());
>
>      if Status = Len then
>        TotalRecv := TotalRecv + Status;
>
>      TotalTime := GetTickCount() - StartedTime;
>      Application.ProcessMessages;
>      Sleep(20);
>    end;
>
>    if TotalTime >= iTimeOut then
>      raise ESocketException.Create(ErrorMessage + ErrMsg);
>  except
>    on E: ESocketException do
>    begin
>      // Display error message
>      Result := False;
>      Application.MessageBox(PChar(E.Message), 'LuaEdit',
> MB_OK+MB_ICONERROR);
>    end;
>  end;
> end;
>
>
> The string sent contains 'C:\RemoteTest\Caller.lua' and I only receive
> ':\RemoteTest\Caller.lua' ??? Does anyone has an idea of what's going on
> here? Thanks in advance!
>
> //JF

Replies:

none

In response to:

www.cryer.info
Managed Newsgroup Archive