源码如下
............
var
Form1: TForm1;
history:tinifile;
myname,yourname,ip,sendtext,receivetext:string;
implementation
uses Unit2;
{$R *.DFM}
procedure TForm1.N1Click(Sender: TObject);
begin
form2.show;
end;
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if(key=vk_return)then
begin
richedit1.Lines.Add(myname+' 说:"'+edit1.text+'" '+timetostr(time));
clientsocket1.socket.sendtext(edit1.text);
edit1.clear;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
history:=tinifile.create('c:/web.ini');
myname:=history.readstring('name','name','');
ip:=history.readstring('ip','ip','');
serversocket1.Open;
end;
procedure TForm1.ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
begin
yourname:=socket.receivetext;
treeview1.items.add(treeview1.selected,yourname);
treeview1.FullExpand;
richedit1.lines.add('欢迎'+yourname+'加入聊天'+' '+timetostr(time));
clientsocket1.socket.sendtext(myname);
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
receivetext:=socket.receivetext;
richedit1.Lines.add(receivetext);
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
clientsocket1.Socket.sendtext(myname);
end;
procedure TForm1.ToolButton1Click(Sender: TObject);
begin
clientsocket1.host:=ip;
clientsocket1.Active:=true;
serversocket1.active:=true;
clientsocket1.Open;
end;
procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
showmessage('无法建立连接');
end;
end.
ip地址等均已正确填写