1个回答
展开全部
1. tcpclient从tcpserver接收数据:
TReceiveThread = class(TThread)
private
FMsg: string;
protected
procedure Execute; override;
end;
var
FThread: TReceiveThread;
procedure TReceiveThread.Execute;
begin
while not Terminated do
begin
if Form1.IdTCPClient1.Connected then
begin
FMsg := Form1.IdTCPClient1.ReadLn;
Sleep(100);
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FThread := TReceiveThread.Create(False);
IdTCPClient1.Host := Edit1.Text;
IdTCPClient1.Port := 10001;
if not IdTCPClient1.Connected then
IdTCPClient1.Connect;
end;
2. tcpserver向tcpclient发送数据
procedure TForm1.SpeedButton1Click(Sender: TObject);
var
i : Integer;
AThread : TIdPeerThread;
begin
try
with IdTCPServer1.Threads.LockList do
for I := 0 to Count - 1 do
begin
AThread := Items[I];
if AThread.Connection.Socket.Binding.PeerIP = 'hostname ' then
AThread.Connection.WriteLn(s);
end;
finally
IdTCPServer1.Threads.UnlockList;
end;
end;
TReceiveThread = class(TThread)
private
FMsg: string;
protected
procedure Execute; override;
end;
var
FThread: TReceiveThread;
procedure TReceiveThread.Execute;
begin
while not Terminated do
begin
if Form1.IdTCPClient1.Connected then
begin
FMsg := Form1.IdTCPClient1.ReadLn;
Sleep(100);
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FThread := TReceiveThread.Create(False);
IdTCPClient1.Host := Edit1.Text;
IdTCPClient1.Port := 10001;
if not IdTCPClient1.Connected then
IdTCPClient1.Connect;
end;
2. tcpserver向tcpclient发送数据
procedure TForm1.SpeedButton1Click(Sender: TObject);
var
i : Integer;
AThread : TIdPeerThread;
begin
try
with IdTCPServer1.Threads.LockList do
for I := 0 to Count - 1 do
begin
AThread := Items[I];
if AThread.Connection.Socket.Binding.PeerIP = 'hostname ' then
AThread.Connection.WriteLn(s);
end;
finally
IdTCPServer1.Threads.UnlockList;
end;
end;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询