TServerSocket and TClientSocket
I use this code to reveive data : But It's not work.Can you Help me ?
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
i:integer;
sRec : string;
begin
for i := 0 to ServerSocke开发者_C百科t1.Socket.ActiveConnections-1 do
begin
with ServerSocket1.Socket.Connections[i] do
begin
sRec:=ReceiveText;
if sRec <> '' then
begin
if RemoteAddress='192.168.0.1' then
begin
if ReceiveText='1' then
Btn1.Color:=clNavy;
ADOQuery1.Active:=True;
end;
if RemoteAddress='192.168.0.1' then
begin
if ReceiveText='2' then
Btn1.Color:=clRed;
Pnl1.Visible:=True;
end;
end;
end;
end;
end;
You are attempting to read from EVERY client connection in the TServerSocket.Socket.Connections list whenever ANY client sends data. You should be using the TCustomWinSocket parameter that the event provides instead. It tells you the exact client that is sending data.
You also have some other logic errors in your code as well.
Try this instead:
procedure TForm1.ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
sRec : string;
begin
sRec := Socket.ReceiveText;
if sRec <> '' then
begin
if Socket.RemoteAddress = '192.168.0.1' then
begin
if sRec = '1' then Btn1.Color := clNavy;
ADOQuery1.Active := True;
if sRec = '2' then Btn1.Color := clRed;
Pnl1.Visible := True;
end;
end;
end;
Or maybe you meant something more like this?
procedure TForm1.ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
sRec : string;
begin
sRec := Socket.ReceiveText;
if sRec <> '' then
begin
if Socket.RemoteAddress = '192.168.0.1' then
begin
if sRec = '1' then begin
Btn1.Color := clNavy;
ADOQuery1.Active := True;
end
else if sRec = '2' then begin
Btn1.Color := clRed;
Pnl1.Visible := True;
end;
end;
end;
end;
Replace ServerSocket1.Socket with Socket param and test again. Of couse to remove the for loop
精彩评论