?? unit1.pas
字號:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdIPMCastServer, IdBaseComponent, IdComponent, IdIPMCastBase,
FunLIB,IdIPMCastClient,IdSocketHandle, StdCtrls, ExtCtrls, IdTCPServer,
IdTCPConnection, IdTCPClient;
type
TForm1 = class(TForm)
Memo1: TMemo;
Memo2: TMemo;
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
IdIPMCastClient1: TIdIPMCastClient;
IdIPMCastServer1: TIdIPMCastServer;
Label1: TLabel;
Label2: TLabel;
Button3: TButton;
Image1: TImage;
IdTCPClient1: TIdTCPClient;
IdTCPServer1: TIdTCPServer;
Image2: TImage;
procedure Button2Click(Sender: TObject);
procedure IdIPMCastClient1IPMCastRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
procedure Button1Click(Sender: TObject);
procedure CastMyIP;
procedure FormCreate(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
private
{ Private declarations }
public
LocalIP:String;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button2Click(Sender: TObject);
begin
IdIPMCastServer1.Send('1');
end;
procedure TForm1.IdIPMCastClient1IPMCastRead(Sender: TObject;
AData: TStream; ABinding: TIdSocketHandle);
var
IP:String;
Port:Integer;
Bf:TStringStream;
ProgId,k:Integer;
MsgTxt,DestIP:String;
begin
//對方傳入機器IP地址 及 端口號
IP :=ABinding.PeerIP;
Port := ABinding.PeerPort;
bf := TStringStream.Create('');
bf.CopyFrom(AData,0);
if bf.DataString<>'' then
begin
ProgId:=StrToInt(copy(bf.DataString,1,1));
case ProgId of
1: begin
//請求IP回應
CastMyIP;
end;
2: begin
//接收
k := pos('*',bf.DataString);
DestIP := copy(bf.DataString,2,k-2); //取目標IP
if DestIP=LocalIP then //判斷信息是否是發(fā)給自己IP的
begin
msgTxt := copy(bf.DataString,2,length(bf.DataString)-1);
Memo2.Lines.Add(IP+':'+msgTxt);
end;
end;
//在列表中添加接收到的IP地址
3 : begin
for k:=0 to ListBox1.Items.Count-1 do
if ListBox1.Items[k]=IP then exit;
ListBox1.Items.Add(IP);
end;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
//發(fā)送信息 自定義信息格式 "2+IP+*+信息體"
IdIPMCastServer1.Send('2'+ListBox1.Items[ListBox1.ItemIndex]+'*'+Memo1.Text);
end;
procedure TForm1.CastMyIP;
begin
//在局域網(wǎng)中公告自己的IP地址
IdIPMCastServer1.Send('3');
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
LocalIP := GetLocalIP;
CastMyIP;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
bf,bmpBf:TMemoryStream;
StrBf:TStringStream;
begin
Image1.Picture.Bitmap.LoadFromFile('c:\111.bmp');
bmpBf := TMemoryStream.Create;
Image1.Picture.Bitmap.SaveToStream(bmpBf);
IdTCPClient1.Host := ListBox1.Items[ListBox1.ItemIndex];
if IdTCPClient1.Connected then IdTCPCLient1.Disconnect;
IdTCPClient1.Connect(1000);
IdTCPCLient1.WriteStream(bmpBf,true,true,bmpBf.Size);
idTCPClient1.Disconnect;
end;
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
bf :TMemoryStream;
begin
bf := TMemoryStream.Create;
AThread.Connection.ReadStream(bf,-1);
bf.Position := 0;
Image2.Picture.Bitmap.LoadFromStream(bf);
end;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -