delphi7中两台机器怎么进行消息传递?用什么控件?

解决方案 »

  1.   

    昨天刚刚学会一招
    indy控件的udp服务器和客户端
      

  2.   

    用MSMQ吧,刚刚试用过的,很好用!unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ComCtrls, MSMQ_TLB, ExtCtrls;type
      TForm1 = class(TForm)
        StatusBar1: TStatusBar;
        Panel2: TPanel;
        GroupBox2: TGroupBox;
        GroupBox1: TGroupBox;
        Panel1: TPanel;
        SendMsgLabel: TEdit;
        Panel3: TPanel;
        RecvMsg: TButton;
        SendMsg: TButton;
        Panel4: TPanel;
        RecvMsgLabel: TEdit;
        Panel5: TPanel;
        Panel6: TPanel;
        Panel7: TPanel;
        Panel8: TPanel;
        SendCoumpterName: TEdit;
        SendQueueName: TEdit;
        RecvCoumpterName: TEdit;
        RecvQueueName: TEdit;
        SendRichEdit: TRichEdit;
        RecvRichEdit: TRichEdit;
        OpenDialog1: TOpenDialog;
        procedure SendMsgClick(Sender: TObject);
        procedure RecvMsgClick(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
    implementation
    uses ComObj;
    {$R *.dfm}procedure TForm1.SendMsgClick(Sender: TObject);
    var
      QueueName, Subject: string;
      Data: OleVariant;
      QueueInfo: IMSMQQueueInfo;
      Queue: IMSMQQueue;
      Msg: IMSMQMessage;
      tr: OLEVariant;
      O1, O2: OleVariant;
    begin
      QueueName := 'Direct=OS:'+ SendCoumpterName.Text + '\private$\'+ SendQueueName.Text;
      Subject := SendMsgLabel.Text + '[' + DateTimeToStr(Now) + ']';
      Data := SendRichEdit.Lines.Text;
      try
        QueueInfo := CreateCOMObject(CLASS_MSMQQueueInfo) as IMSMQQueueInfo;
        QueueInfo.FormatName := QueueName;
        try
          Queue := QueueInfo.Open(MQ_SEND_ACCESS, MQ_DENY_NONE);
        except
          QueueInfo.PathName := SendCoumpterName.Text + '\private$\'+ SendQueueName.Text;
          O1 := True;
          O2 := False;
          QueueInfo.Create(O1, O2);
          Queue := QueueInfo.Open(MQ_SEND_ACCESS, MQ_DENY_NONE);
        end;
        if not Queue.IsOpen then
          StatusBar1.SimpleText := '队列没有打开或队列不存在!'
        else begin
          try
            Msg := CreateCOMObject(CLASS_MSMQMessage) as IMSMQMessage;
            Msg.Label_ := Subject;
            Msg.Body := Data;
            tr := MQ_SINGLE_MESSAGE;
            Msg.Send(Queue, tr);
            StatusBar1.SimpleText := '数据传送成功!'
          finally
            Queue.Close;
          end;
        end;
      except
        on E: exception do ShowMessage (E.Message);
      end;
    end;procedure TForm1.RecvMsgClick(Sender: TObject);
    var
      QueueName: string;
      Data: OleVariant;
      QueueInfo: IMSMQQueueInfo;
      Queue: IMSMQQueue;
      Msg: IMSMQMessage;
      tr, wdq, wb, rt: OleVariant;
    begin
      QueueName:= 'Direct=OS:'+ RecvCoumpterName.Text + '\private$\'+ RecvQueueName.Text;
      try
        QueueInfo := CreateCOMObject(CLASS_MSMQQueueInfo) as IMSMQQueueInfo;
        QueueInfo.FormatName := QueueName;
        Queue := QueueInfo.Open(MQ_RECEIVE_ACCESS, MQ_DENY_NONE);
        if not Queue.IsOpen then
            StatusBar1.SimpleText := '队列打不开或不存在!'
        else begin
          try
            tr := MQ_SINGLE_MESSAGE;
            wdq := False;
            wb := True;
            rt := 1000;
            Msg := Queue.Receive(tr, wdq, wb, rt);
            if Msg <> nil then
            begin
              Data := Msg.Body;
              StatusBar1.SimpleText := '接收成功!';
              RecvRichEdit.Lines.Text := Data;
              RecvMsgLabel.Text := Msg.Label_;
            end
            else
              StatusBar1.SimpleText := '您所打开的队列中没有消息'
          finally
            Queue.Close;
          end;
        end;
      except
        on E: Exception do ShowMessage(E.Message);
      end;
    end;
    end.
      

  3.   

    delphi7 中可以用 Tcpsever 和Tcpclient 控件.
    或者用Udpsockets控件。
    以上控件均在internet组件板。
    或者用indy的一套控件