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

www.cryer.info
Managed Newsgroup Archive

Problem send/recv a string using winsock

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

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:

www.cryer.info
Managed Newsgroup Archive