Well, without knowing more on your setup, one way I can see to
differentiate your clients is with their IP. The client IP is
available in Client[x].PeerAddr.
I don't have any setup.
I am just trying to see the number of the client connected in the memo
that's all,
like this: 0 - Client connected. Remote: 127.0.0.1/49716 Local:
127.0.0.1/443
or like this: [0]Client connected. Remote: 127.0.0.1/49716 Local:
127.0.0.1/443
or like this: (0) Client connected. Remote: 127.0.0.1/49716 Local:
127.0.0.1/443
This will help me to see which client to send the command.
That's what I and others are trying to do.
I still don't get any clear help.
guys,
Please see the codes and help me to start with this.
I will not be able to continue or learn anything if I simply can't make a
ready made demo to send a simple command.
Now,
Where and How do I define this to get that number into the memo??
Here is procedures "OnDataAvailable" and "ProcessData" of the SERVER
-----------------------------------------------------------------------------------------------------------------------------------------------------
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*}
procedure TSimpleSslServerForm.ClientDataAvailable(
Sender : TObject;
Error : Word);
begin
with Sender as TTcpSrvClient do begin
{ We use line mode. We will receive complete lines }
RcvdLine := ReceiveStr;
{ Remove trailing CR/LF }
while (Length(RcvdLine) > 0) and
IsCharInSysCharSet(RcvdLine[Length(RcvdLine)], [#13, #10]) do
RcvdLine := Copy(RcvdLine, 1, Length(RcvdLine) - 1);
Display('Received from ' + GetPeerAddr + ': ''' + RcvdLine + '''');
ProcessData(Sender as TTcpSrvClient);
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*}
procedure TSimpleSslServerForm.ProcessData(Client: TTcpSrvClient);
var
I : Integer;
P : Pointer;
AClient : TTcpSrvClient;
begin
{ We could replace all those CompareText with a table lookup }
if CompareText(Client.RcvdLine, 'help') = 0 then
Client.SendStr('Commands are:' + #13#10 +
' exit' + #13#10 +
' who' + #13#10 +
' time' + #13#10 +
' exception' + #13#10 +
' binary [size]' + #13#10)
else if CompareText(Copy(Client.RcvdLine, 1, 6), 'binary') = 0 then
begin
I := StrToIntDef(Copy(Client.RcvdLine, 7, MaxInt), 0);
if I <= 0 then
Client.SendStr('500 Error binary size not spezified'#13#10)
else begin
if I > MaxWord then
begin
Client.SendStr('500 Error binary size limited to ' +
IntToStr(MaxWord) + ' bytes'#13#10);
Exit;
end
else
Client.SendStr('200 OK Binary ' + IntToStr(I) +
' bytes requested'#13#10);
Inc(I, SizeOf(THdrRec));
GetMem(P, I);
try
FillChar(P^, I, '1');
PHdrRec(P)^.ID := 0; // any value < 32 marks = valid
binary data.
PHdrRec(P)^.Size := I - SizeOf(THdrRec);
PAnsiChar(P)[I - 1] := 'E';
Client.Send(P, I);
finally
FreeMem(P);
end;
end;
end
else if CompareText(Client.RcvdLine, 'exit') = 0 then
{ We can't call Client.Close here because we will immediately }
{ reenter DataAvailable event handler with same line because }
{ a line is removed from buffer AFTER it has been processed. }
{ Using CloseDelayed will delay Close until we are out of }
{ current event handler. }
Client.CloseDelayed
else if CompareText(Client.RcvdLine, 'time') = 0 then
{ Send server date and time to client }
Client.SendStr(DateTimeToStr(Now) + #13#10)
else if CompareText(Client.RcvdLine, 'who') = 0 then begin
{ Send client list to client }
Client.SendStr('There are ' +
IntToStr(SslWSocketServer1.ClientCount) +
' connected users:' + #13#10);
for I := SslWSocketServer1.ClientCount - 1 downto 0 do begin
AClient := TTcpSrvClient(SslWSocketServer1.Client[I]);
Client.SendStr(AClient.PeerAddr + ':' + AClient.GetPeerPort + '
' +
DateTimeToStr(AClient.ConnectTime) + #13#10);
end;
end
else if CompareText(Client.RcvdLine, 'exception') = 0 then
{ This will trigger a background exception for client }
PostMessage(Client.Handle, Client.FMsg_WM_TRIGGER_EXCEPTION, 0, 0)
else
if Client.State = wsConnected then
Client.SendStr('Unknown command: ''' + Client.RcvdLine + '''' +
#13#10);
end;
--------------------------------------------------------------------------------------------------------------------------------------------------------
And here is "OnDataAvailable" for the client.
-----------------------------------------------------------------------------------------------------
procedure TfrmMain.SockDataAvailable(Sender: TObject; ErrCode: Word);
var
Buf : array [0..255] of AnsiChar;
Len : Integer;
begin
Len := TCustomLineWSocket(Sender).Receive(@Buf, Sizeof(Buf) - 1);
if Len <= 0 then
Exit;
Buf[Len] := #0;
if not Sock.LineMode then
{ Normal mode, data is just a buffer with all caracters }
memoMain.Lines.Add('DataAvailable (' + IntToStr(Len) +' bytes): '''
+
String(StrPas(Buf)) + '''')
else begin
{ Line mode, buffer contains exactly one line, terminated by the }
{ LineEnd string, unless our buffer is too small in which case }
{ the line is truncated. We'll get the end of line on the next }
{ call to Receive. }
memoMain.Lines.Add('Line: ''' + RemoveEndOfLine(String(StrPas(Buf)))
+ '''');
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