⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 delphi写的以广播方式收发信息和图片_局域网聊天。
💻 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  //判断信息是否是发给自己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
  //发送信息 自定义信息格式 "2+IP+*+信息体"
  IdIPMCastServer1.Send('2'+ListBox1.Items[ListBox1.ItemIndex]+'*'+Memo1.Text);
end;

procedure TForm1.CastMyIP;
begin
  //在局域网中公告自己的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 + -