拿分来!
服务器:
unit IndyTCPServer;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdCustomTCPServer, IdTCPServer,
StdCtrls, ComCtrls, IdContext;
type
TForm1 = class(TForm)
ListView1: TListView;
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
Edit1: TEdit;
Button3: TButton;
IdTCPServer1: TIdTCPServer;
Button4: TButton;
procedure Button2Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure IdTCPServer1Execute(AContext: TIdContext);
procedure IdTCPServer1Disconnect(AContext: TIdContext);
procedure IdTCPServer1BeforeConnect(AContext: TIdContext);
procedure IdTCPServer1Connect(AContext: TIdContext);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses WinSock;
{$R *.dfm}
function IPAddrToName(IPAddr : String): String;
var
SockAddrIn: TSockAddrIn;
HostEnt: PHostEnt;
WSAData: TWSAData;
begin
WSAStartup($101, WSAData);
SockAddrIn.sin_addr.s_addr:= inet_addr(PChar(IPAddr));
HostEnt:= gethostbyaddr(@SockAddrIn.sin_addr.S_addr, 4, AF_INET);
if HostEnt<>nil then
begin
result:=StrPas(Hostent^.h_name)
end
else
begin
result:='';
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
I : Integer;
Context: TIdContext;
begin
if not IdTCPServer1.Active then
Button1.Caption := '停止服务(&S)'
else begin
Button1.Caption := '启动服务(&S)';
for I := ListView1.Items.Count - 1 downto 0 do
begin
Context:= TIdContext(ListView1.Items.SubItems.Objects[0]);
if Context <> nil then
begin
ListView1.Items.Delete;
Context.Connection.Socket.Close;
end;
end;
ListView1.Tag := 0;
end;
IdTCPServer1.Active := Not IdTCPServer1.Active;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
Context: TIdContext;
begin
if ListView1.ItemIndex < 0 then
begin
ShowMessage('请选择一个发送对象!');
Exit;
end;
Context := TIdContext(ListView1.Selected.SubItems.Objects[0]);
if Context <> nil then
Context.Connection.Socket.Close;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
Context: TIdContext;
begin
if Trim(Edit1.Text) = '' then Exit;
if ListView1.ItemIndex < 0 then
begin
ShowMessage('请选择一个发送对象!');
Exit;
end;
Context := TIdContext(ListView1.Selected.SubItems.Objects[0]);
if Context <> nil then
Context.Connection.Socket.WriteLn(Edit1.Text);
end;
procedure TForm1.Button4Click(Sender: TObject);
var
I : Integer;
Context: TIdContext;
begin
if Trim(Edit1.Text) = '' then Exit;
for I := 0 to ListView1.Items.Count - 1 do
begin
Context := TIdContext(ListView1.Items.SubItems.Objects[0]);
if Context <> nil then
Context.Connection.Socket.WriteLn(Edit1.Text);
end;
end;
procedure TForm1.IdTCPServer1BeforeConnect(AContext: TIdContext);
begin
Memo1.Lines.Add(Format('用户请求连接:[IP:%s, Port: %d]', [AContext.Binding.PeerIP, AContext.Binding.PeerPort]));
end;
procedure TForm1.IdTCPServer1Connect(AContext: TIdContext);
begin
Memo1.Lines.Add(Format('用户已连接:[IP:%s, Port: %d]', [AContext.Binding.PeerIP, AContext.Binding.PeerPort]));
with ListView1.Items.Add do
begin
Caption := IntToStr(ListView1.Tag);
SubItems.AddObject(AContext.Binding.PeerIP, AContext);
SubItems.Add(IntToStr(AContext.Binding.PeerPort));
SubItems.Add(FormatDateTime('yyyy-mm-dd hh:nn:ss', now));
//不推荐增加主机名 要么从客户端发往服务器 我就先这么写了
SubItems.Add(IPAddrToName(AContext.Binding.PeerIP));
ListView1.Tag := ListView1.Tag + 1;
end;
end;
procedure TForm1.IdTCPServer1Disconnect(AContext: TIdContext);
var
I : Integer;
Context: TIdContext;
begin
Memo1.Lines.Add(Format('用户断开连接:[IP:%s, Port: %d]', [AContext.Binding.PeerIP, AContext.Binding.PeerPort]));
for I := ListView1.Items.Count - 1 downto 0 do
begin
Context:= TIdContext(ListView1.Items.SubItems.Objects[0]);
if Context = AContext then
begin
ListView1.Items.Delete;
Break;
end;
end;
end;
procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
var
S : string;
begin
S := AContext.Connection.Socket.ReadLn;
if S <> '' then
Memo1.Lines.Add(Format('收到来自[%s:%d]的消息: %s', [AContext.Binding.PeerIP, AContext.Binding.PeerPort, S]));
end;
end.
客户端:
unit TCPClient;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient;
type
TForm2 = class(TForm)
IdTCPClient1: TIdTCPClient;
Memo1: TMemo;
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure IdTCPClient1Disconnected(Sender: TObject);
procedure IdTCPClient1Connected(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
procedure ReadSrvMsg;
implementation
const
RSMTimer: LongWord = 300;
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
begin
if not IdTCPClient1.Connected then
IdTCPClient1.Connect
else
IdTCPClient1.Disconnect;
end;
procedure TForm2.Button2Click(Sender: TObject);
begin
if not IdTCPClient1.Connected then Exit;
if Trim(Edit1.Text) = '' then Exit;
IdTCPClient1.Socket.WriteLn(Edit1.Text);
end;
procedure TForm2.IdTCPClient1Connected(Sender: TObject);
begin
Memo1.Lines.Add('连接成功! ' + IdTCPClient1.Host);
SetTimer(Handle, RSMTimer, 20, @ReadSrvMsg);
Button1.Caption := '断开连接(&D)';
end;
procedure TForm2.IdTCPClient1Disconnected(Sender: TObject);
begin
Memo1.Lines.Add('已断开连接!');
KillTimer(Handle, RSMTimer);
Button1.Caption := '连接(&C)';
end;
procedure ReadSrvMsg;
var
S: string;
begin
S := Form2.IdTCPClient1.Socket.ReadLn('', 10);
if S <> '' then
Form2.Memo1.Lines.Add('收到服务器消息: ' + S);
end;
end.