i converted the indy client and server To TWsocket and Twsocket server and
here is the code of server after convert

Const

 Sep = '#$%^&';
  type
  TMyClient = class(TWSocketClient)
  private
    Name: String;
    IP: String;
    Connected: TDateTime;
    UniqueID: DWORD;
    Thread: Pointer;
    function RemoveTrailingCRLF(s: string): string;
    procedure ConnectionDataAvailable(Sender: TObject; ErrCode: Word);
    procedure SendClientsList;

  public
    constructor Create(AOwner : TComponent); override;
    destructor Destroy; override;
    procedure Display(Msg: String);
    procedure SendCommandWithParams(const Command, Params: String);
    procedure BroadcastMessage(Const Username : String; Const Textmsg :
String);
  end;


procedure TMyClient.SendCommandWithParams(const Command, Params: String);
begin
SendStr('1' + Command + sep + Params + #13#10);
end;


procedure TMyClient.BroadcastMessage(const Username : String; Const
Textmsg: String);
var
Client : TMyclient;
Server : TWSocketServer;
I : integer;
begin
Server := mainserver.server;
for I := 0 to server.ClientCount -1 do
begin
  Client := TMyclient(server.Client[I]);
  Client.SendCommandWithParams('TEXTMESSAGE', Username + Sep + Textmsg +
sep);
end;

end;



procedure TMyClient.ConnectionDataAvailable(Sender: TObject; ErrCode: Word);
var
  Command: String;
  handler : String;
  params : array[1..10] of String;
  I : integer;
  ReceiveParams, ReceiveStream: Boolean;
  S : string;
  ParamsCount, P: Integer;
  MS: TMemoryStream;
  Usernm : string;
  Pwd : String;
begin
  Command := ReceiveStr;
  if Command = '' then Exit;

    { Remove end of line marker }
    if Length(Command) > 1 then
    SetLength(Command, Length(Command) - 2);
    RemoveTrailingCRLF(Command);
    { Remove unused blanks }

  ReceiveParams := False;
  ReceiveStream := False;

  if Command[1] = '1'  then //command with params
  begin
    Command := Copy(Command, 2, MaxInt);
    ReceiveParams := True;
  end
   else if Command[1] = '2' then //command + memorystream
  begin
    Command := Copy(Command, 2, MaxInt);
    ReceiveStream := True;
  end
  else if Command[1] = '3' then //command with params + memorystream
  begin
    Command := Copy(Command, 2, MaxInt);
    ReceiveParams := True;
    ReceiveStream := True;
  end;

   //start see commands

     if ReceiveParams then //params is incomming
  begin
    ParamsCount := 0;
    while (Command <> '') and (ParamsCount < 10) do
    begin
      Inc(ParamsCount);
      p := Pos(Sep, Command);
      if p = 0 then
        Params[ParamsCount] := Command
      else
      begin
        Params[ParamsCount] := Copy(Command, 1, P - 1);
        Delete(Command, 1, P + 4);
      end;
    end;
  end;

  handler := trim(Params[1]);

  MS := nil;
  try
    if ReceiveStream then //stream is incomming
    begin
      MS := TMemoryStream.Create;
      //AContext.Connection.Socket.LargeStream := True;
      //AContext.Connection.Socket.ReadStream(MS, -1, False);
      MS.Position := 0;
    end;

  if handler = 'LOGIN' then
    begin
    Usernm := Params[2];
    Pwd := Params[3];
      if ParamsCount <> 3 then
      begin
        Display('INVALIDPARAMS');
        Exit;
      end;
     SendCommandWithParams('SENDINFO', intTostr(UniqueID) + Sep);

    end

  else if handler = 'TAKEMYINFO' then //login ok, add to listview
  begin
   if ParamsCount <> 2 then
      begin
        Display('INVALIDPARAMS');
        Exit;
   end;
  Display(Params[2]);
  Name := Params[2];
  AddToListView;
  end

  else if handler = 'TEXTMESSAGE' then
  begin
   BroadcastMessage(Params[2], Params[3]);
  end

  finally
  MS.Free
  end;
  end;


every thing working fine to send receive strings but how do i send receive
 memory stream for example i try to send user list like this


procedure TMyClient.SendClientsList;
var
I, J: Integer;
SL: TStringList;
MS: TMemoryStream;
Client : TMyclient;
Server : TWSocketServer;
begin
Server := mainserver.server;
 try
  SL := TStringList.Create;
    // Collect List
    for J := 0 to Server.ClientCount - 1 do
    begin
      Client := TMyclient(Server.Client[J]);
      SL.Add(Client.Name + Sep + IntToStr(Client.UniqueID) + Sep);
    end;

    // Send List
    for I := 0 to Server.ClientCount- 1 do
    begin
      Client := TMyclient(Server.Client[J]);
      if (SL.Count > 0) then
      begin
        MS := TMemoryStream.Create;
        try
          SL.SaveToStream(MS);
          MS.Position := 0;
          //what to do to send ?
        finally
          MS.Free;
        end;
      end;
    end;
  finally
  SL.Free;
  end;

end;

but how to send this memory stream to client ? in indy i usually doing this

procedure TConnection.SendCommandAndStream(Command: String; MS:
TMemoryStream);
begin
 WriteLock.Enter;
  try
    MS.Position := 0;
    with Connection.Socket do
    begin
      WriteLn('2' + Command);
      Write(ms.Size);
      WriteBufferOpen;
      Write(MS, 0);
      WriteBufferClose;
    end;
  finally
     WriteLock.Leave;
  end;
end;


and in client side to receive that stream i make check like this check the
line when its check here *if Command[1] = '2' in indy i used to read stream
like this ** //TCPClient.Socket.LargeStream := True;*
*//TCPClient.Socket.ReadStream(MS, -1, False);*

*but in ics i dont know what to do *

*procedure TForm1.TCPClientDataAvailable(Sender: TObject; ErrCode: Word);*
*var*
*Command: String;*
*  handler : String;*
*  params : array[1..10] of String;*
*  I : integer;*
*  ReceiveParams, ReceiveStream: Boolean;*
*  S : string;*
*  ParamsCount, P: Integer;*
*  MS: TMemoryStream;*
*begin*

*Command  := TCPClient.ReceiveStr;*

*if Command = '' then Exit;*

*    { Remove end of line marker }*
*    if Length(Command) > 1 then*
*    SetLength(Command, Length(Command) - 2);*
*    { Remove unused blanks }*

*  ReceiveParams := False;*
*  ReceiveStream := False;*

*  if Command[1] = '1'  then //command with params*
*  begin*
*    Command := Copy(Command, 2, MaxInt);*
*    ReceiveParams := True;*
*  end*
*   else if Command[1] = '2' then //command + memorystream*
*  begin*
*    Command := Copy(Command, 2, MaxInt);*
*    ReceiveStream := True;*
*  end*
*  else if Command[1] = '3' then //command with params + memorystream*
*  begin*
*    Command := Copy(Command, 2, MaxInt);*
*    ReceiveParams := True;*
*    ReceiveStream := True;*
*  end;*

*   //start see commands*

*     if ReceiveParams then //params is incomming*
*  begin*
*    ParamsCount := 0;*
*    while (Command <> '') and (ParamsCount < 10) do*
*    begin*
*      Inc(ParamsCount);*
*      p := Pos(Sep, Command);*
*      if p = 0 then*
*        Params[ParamsCount] := Command*
*      else*
*      begin*
*        Params[ParamsCount] := Copy(Command, 1, P - 1);*
*        Delete(Command, 1, P + 4);*
*      end;*
*    end;*
*  end;*

*  handler := trim(Params[1]);*

*     MS := nil;*
*   try*
*    if ReceiveStream then //stream is incomming*
*    begin*
*      MS := TMemoryStream.Create;*
*      //TCPClient.Socket.LargeStream := True;*
*      //TCPClient.Socket.ReadStream(MS, -1, False);*
*      MS.Position := 0;*
*    end;*
*   if handler = 'SENDINFO' then*
*    begin*
*     UniqueID := StrToint(Params[2]);*
*      if ParamsCount <> 2 then*
*      begin*
*        Display('INVALIDPARAMS');*
*        Exit;*
*      end;*
*      SendCommandWithParams(TCPClient, 'TAKEMYINFO', Lusername + sep);*
*      Display(Params[2]);*
*    end*
*    else if handler = 'TEXTMESSAGE' then*
*    begin*
*     Display(Params[2] + ' : ' + Params[3]);*
*    end;*


*   finally*
*    Ms.Free;*
*   end;*

*end;*
-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

Reply via email to