Something like this ? (Out of my head, not tested)
for ClientNo := 0 to WSocketServer1.ClientCount - 1 do begin
if Client[ClientNo].PeerAddr = '192.168.1.123' then begin
Client[ClientNo].SendStr('HelloWorld'#13#10);
break;
end;
end;
Hi Francois,
Could you please check the following code of both server and client
procedures and tell me what is missing in order to be able to get the client
number into the "DISPLAY" with the all other info.This is code writen by you
i think. I have add few things and there might be mistakes.
I am certain, I will be okay after that.
Thanks a lot in advance.
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