(死亡历程)Delphi7 自带的控件 IdTCPClient,IdTCPServer 客户端发送给服务端,服务端转发给其它客户端

客户端

private

{ Private declarations }

FThread: TThread;

public

{ Public declarations }

iFileHandle:integer;

i: integer;

host,port,qm:string;

sFile:TFileStream;

F : Textfile;

str,nbzy_bh:string;

end;

TReadThread = class(TThread)

private

FClient: TIdTCPClient;

FMessage,nbzy_qm: string;

FForm: TFormLjf2;

procedure UpdateForm;

protected

procedure Execute; override;

public

constructor Create(AForm: TFormLjf2; qm: string; AClient: TIdTCPClient);

end;

var

FormLjf2: TFormLjf2;

implementation

uses ljf999;

{$R *.dfm}

procedure TFormLjf2.FormShow(Sender: TObject);

var

i,j:Integer;

begin

//得到要发送的客户端IP

i:=pos(')',Self.Caption); //查找分隔符

host:=copy(Self.Caption,i+1,Length(Self.Caption)-i);

//连接到服务器

IdTCPClient2.Host:=FormLjf999.host;

IdTCPClient2.Port:=StrToInt(FormLjf999.port);

//得到用户编号

str:=Self.Caption;

i:=pos('(',str);

j:=pos(')',str);

nbzy_bh:=copy(str,i+1,j-(i+1));

qm:=copy(str,1,i-1);;

Memo1.SetFocus;

//这里是线程,2个参数,一个是窗口,一个是接收信息的IdTCPClient 2017-1-5

FThread := TReadThread.Create(Self, qm, IdTCPClient2);

end;

procedure TFormLjf2.SpeedButton1Click(Sender: TObject);

var

Tid: DWord;

begin

if Memo1.Text='' then

begin

ShowMessage('请输入内容');

Memo1.SetFocus;

Exit;

end;

if not IdTCPClient2.Connected then

begin

ShowMessage('连接未建立。');

exit;

end;

IdTCPClient2.WriteLn(host+'-'+Memo1.Text);

Memo2.Lines.Add('我 '+DateTimeToStr(Now)+ #13#10 +' '+Memo1.Text);

PostMessage(Memo2.Handle, WM_VSCROLL, SB_BOTTOM, 0);

Memo1.Text:='';

//Backspace键

keybd_event(8,0,0,0);

keybd_event(8,0,KEYEVENTF_KEYUP,0);

end;

procedure TFormLjf2.FormCloseQuery(Sender: TObject; var CanClose: Boolean);

begin

OutputDebugString('notify thead to quit.');

FThread.Terminate;

OutputDebugString('wait thead finish.');

//FThread.WaitFor;

OutputDebugString(PChar('thread finished.'));

if IdTCPClient2.Connected then

begin

IdTCPClient2.Disconnect;

end;

end;

procedure TFormLjf2.FormClose(Sender: TObject; var Action: TCloseAction);

begin

//Action := caFree;

Self.Free;

end;

{ TReadThread }

constructor TReadThread.Create(AForm: TFormLjf2; qm: string; AClient: TIdTCPClient);

begin

FClient := AClient;

FForm := AForm;

nbzy_qm := qm;

FreeOnTerminate := True;

inherited Create(False);

end;

procedure TReadThread.Execute;

begin

inherited;

while not Terminated do

begin

if Terminated then

begin

Break;

end;

if not FClient.Connected then

begin

try

FClient.Connect(100);

except

end;

if not FClient.Connected then

begin

Sleep(100);

continue;

end;

end;

FMessage := FClient.ReadLn(#$D, 1);

if Length(FMessage) > 0 then

Synchronize(UpdateForm);

end;

if FClient.Connected then

FClient.Disconnect;

end;

procedure TReadThread.UpdateForm;

begin

FForm.Memo2.Lines.Add(nbzy_qm+' '+DateTimeToStr(Now));

FForm.Memo2.Lines.Add(' '+FMessage);

end;

end.

服务端接收并转发

procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);

var

rbyte:array[0..4096] of byte;

sFile:TFileStream;

cmd,FileSize:integer;

str,FileName:string;

iFileHandle:integer;

iFileLen,cnt:integer;

buf:array[0..4096] of byte;

ip,fileqm,sendStr:string;

i,j:integer;

sm:TStringStream;

msize:Integer;

begin

//while AThread.Connection.Connected do  //注意这里

with AThread.Connection do

begin

Try

str:=AThread.Connection.ReadLn;   //接收文件大小及文件名

if POS('|',str)>0 then

begin

//

end

else

begin

i:=pos('-',str); //查找分隔符

ip:=copy(str,1,i-1); //提取文件名

sendStr:=copy(str,i+1,Length(str)-i);

IdTCPServer1.Threads.UnlockList;

with IdTCPServer1.Threads.LockList do

begin

try

for i:=0 to Count - 1 do

begin

try

AThread := Items[i];

if AThread.Connection.Socket.Binding.PeerIP = ip then

begin

AThread.Connection.WriteLn(sendStr);//这里是服务端发送给客户端的

end;

except

AThread.Stop;

end;

end;

finally

IdTCPServer1.Threads.UnlockList;

end;

end;

end;

Finally

//Athread.Connection.Disconnect;

//AThread.Connection.WriteBuffer('错误',SizeOf('错误'),True);

end;

end;

end;

procedure TForm1.IdTCPServer1Disconnect(AThread: TIdPeerThread);//客户端退出触发 IdTCPServer1Disconnect

begin

Memo1.Lines.Add(AThread.Connection.Socket.Binding.PeerIP+' 已退出');

try

IdTCPServer1.Threads.LockList.Remove(Athread);

finally

IdTCPServer1.Threads.UnlockList();

end;

//AThread.Connection.Disconnect;

end;

procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);

var

strSql,qm,bh,str:string;

i:Integer;

begin

Memo1.Lines.Add(qm+' '+AThread.Connection.Socket.Binding.PeerIP+' 已连接');

end;

procedure TForm1.FormDestroy(Sender: TObject);

begin

IdTCPServer1.Active:=False;

Application.Terminate;

end;

procedure TForm1.IdTCPServer1Exception(AThread: TIdPeerThread;

AException: Exception);

begin

Memo1.Lines.Add('客户端'+inttostr(athread.handle)+'异常断开');

del(AThread);

if AThread.Connection.Connected then

AThread.Connection.Disconnect;

end;

————————————————

版权声明:本文为CSDN博主「luojianfeng」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/luojianfeng/article/details/54097231

(0)

相关推荐