我把IdTCPClient IdTCPServer两个放在同一窗体中.希望能实现发送和接收两种用途.网络通讯,已经可以发送消息.但是在发文件之前,我希望弹出对话框,确定对方是否接收.
对方已经可以接收到对话框,但是我自己接收不到是否接收的消息.   请大家看看我写的代码,是不是哪里出了问题.(本机上面可以通过)unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, IdTCPServer, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient;type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Edit1: TEdit;
    Label1: TLabel;
    Edit2: TEdit;
    Label2: TLabel;
    Button1: TButton;
    Memo1: TMemo;
    Button2: TButton;
    Button3: TButton;
    StatusBar1: TStatusBar;
    OpenDialog1: TOpenDialog;
    IdTCPClient1: TIdTCPClient;
    IdTCPServer1: TIdTCPServer;
    procedure FormCreate(Sender: TObject);
    procedure IdTCPClient1Connected(Sender: TObject);
    procedure IdTCPClient1Disconnected(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure IdTCPServer1Execute(AThread: TIdPeerThread);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
IdTCPServer1.Active:=true;
end;procedure TForm1.IdTCPClient1Connected(Sender: TObject);
begin
statusbar1.Panels[0].Text:='已经连接成功';
button1.Caption:='断开';
end;procedure TForm1.IdTCPClient1Disconnected(Sender: TObject);
begin
statusbar1.Panels[0].Text:='已经断开连接';
button1.Caption:='连接';
end;procedure TForm1.Button1Click(Sender: TObject);
begin
if Button1.Caption='连接' then
  begin
  IdTCPClient1.Host:=edit1.Text;
  IdTCPClient1.Port:=StrToInt(edit2.Text);
  IdTCPClient1.Connect(1000);
  end
else if Button1.Caption='断开' then
  begin
  IdTCPClient1.Disconnect;
  end;
end;procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
  cmd:string;
  filebegin:Integer;
begin
AThread.Start;
cmd:=AThread.Connection.ReadLn();
if cmd='BEGINFILESTART' then  //首先查看客户端是否发送文件传送标志
  begin
  filebegin:=application.MessageBox('对方发送了文件'+Chr(13)+Chr(13)+'是否接收?','提示',4);
  if filebegin=IDYES then
    begin
    IdTCPClient1.WriteLn('BEGINFILEOK');
    end
  else
    begin
    IdTCPClient1.WriteLn('BEGINFILENO');
    end;
  end
else if cmd='BEGINFILEOK' then   //如果对方点击了'是'
  begin
  application.MessageBox('对方接受了文件!','提示',0)
  end
else if cmd='BEGINFILENO' then   //如果对方点击了'否' 
  begin
  application.MessageBox('对方已经拒绝了!','提示',0)
  end
else   //并不是发送文件
  begin
  listbox1.Items.Add('(消息):'+cmd);
  end;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
IdTCPClient1.Disconnect;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
  with IdTCPClient1 do
    begin
    if Connected then
      begin
      WriteLn(memo1.Lines.Text);
      end
    else
      begin
      application.MessageBox('还没有连接','提示',0)
      end;
    end;
end;procedure TForm1.Button3Click(Sender: TObject);
begin
  with IdTCPClient1 do
    begin
    if Connected then
      begin
      opendialog1.Execute;
      WriteLn('BEGINFILESTART');
      end
    else
      begin
      application.MessageBox('还没有连接','提示',0)
      end;
    end;
end;end.

解决方案 »

  1.   

    问题处在下列代码中
    procedure   TForm1.IdTCPServer1Execute(AThread:   TIdPeerThread); 
    var 
        cmd:string; 
        filebegin:Integer; 
    begin 
    AThread.Start; 
    cmd:=AThread.Connection.ReadLn(); 
    if   cmd='BEGINFILESTART'   then     //首先查看客户端是否发送文件传送标志 
        begin 
        filebegin:=application.MessageBox('对方发送了文件'+Chr(13)+Chr(13)+'是否接收?','提示',4); 
        if   filebegin=IDYES   then 
            begin 
              IdTCPClient1.WriteLn('BEGINFILEOK'); 
            end 
        else 
            begin 
              IdTCPClient1.WriteLn('BEGINFILENO'); 
            end; 
        end 
      else  <<---这里else语句块是对应那里呢(问题1) 
         if   cmd='BEGINFILEOK'   then       //如果对方点击了'是' 
         begin 
          application.MessageBox('对方接受了文件!','提示',0) 
         end 
        else   
          if   cmd='BEGINFILENO'   then       //如果对方点击了'否'   
          begin 
            application.MessageBox('对方已经拒绝了!','提示',0) 
          end 
    else       //并不是发送文件 
        begin 
        listbox1.Items.Add('(消息):'+cmd); 
        end; 
    end;问题2:
    从你的代码看是自己发送标志自己接收标志,而不是接收对方的返回给你的信息
      

  2.   

    不知道这么改是否符合你的要求,你自己试下
    AThread.Start; 
    cmd:=AThread.Connection.ReadLn(); 
    if   cmd='BEGINFILESTART'   then
    begin
    filebegin := application.MessageBox('对方发送了文件'+Chr(13)+Chr(13)+'是否接收?','提示',4);
    if (filebegin = IDYES) and (cmd='BEGINFILEOK') then
    begin
    IdTCPClient1.WriteLn('BEGINFILEOK');
    application.MessageBox('对方接受了文件!','提示',0);
    end
    else
    begin
    IdTCPClient1.WriteLn('BEGINFILENO');
    application.MessageBox('对方已经拒绝了!','提示',0);
    end
    end
    else
    begin
    listbox1.Items.Add('(消息):'+cmd);
    end;
      

  3.   

    放在一个窗体里也可以实现,看看这个,或许对你有用http://www.2ccc.com/article.asp?articleid=3868