这是我们的代码,port211希望对你有帮助
unit UConnections;interfaceuses
  SysUtils, Classes, DB, DBClient, MConnect, SConnect,QDialogs,UCommonModule;type
  TConnections = class(TDataModule)
    cnnSysSet: TSocketConnection;
    cnnCust: TSocketConnection;
    cnnLogin: TSocketConnection;
    cnnSupply: TSocketConnection;
  private
    { Private declarations }
  public
    IP:string; //应用服务器的IP地址
    procedure ConnectToServer(b:Boolean); //连接和断开应用服务器
    procedure ReadIPFromFile;             //从文件中读取服务器的IP地址,并保存在IP内
  end;var
  Connections: TConnections;
const
  FileName='IP.txt';       //存放服务器IP地址的文件名
  DefaultIP='10.13.21.110'; //缺省IP地址implementation{$R *.dfm}{ TConnections }procedure TConnections.ConnectToServer(b: Boolean);
begin
     if b=TRUE then
     begin
          //从文件中读取IP地址
          ReadIPFromFile;
          //设置服务器的IP地址
          cnnLogin.Host:=IP;
          cnnSysSet.Host:=IP;
          cnnCust.Host:=IP;
          cnnSupply.Host:=IP;          cnnLogin.Connected:=TRUE;
          cnnSysSet.Connected:=TRUE;
          cnnCust.Connected:=TRUE;
          cnnSupply.Connected:=TRUE;
          {if Assigned(FormSplash) then FormSplash.ProgressBar.StepBy(1);
          cnnCust.Connected:=TRUE;
          if Assigned(FormSplash) then FormSplash.ProgressBar.StepBy(1);
          cnnLogin.Connected:=TRUE;
          if Assigned(FormSplash) then FormSplash.ProgressBar.StepBy(1);
          cnnOrder.Connected:=TRUE;
          if Assigned(FormSplash) then FormSplash.ProgressBar.StepBy(1);
          cnnStore.Connected:=TRUE;
          if Assigned(FormSplash) then FormSplash.ProgressBar.StepBy(1);
          cnnTrans.Connected:=TRUE;
          if Assigned(FormSplash) then FormSplash.ProgressBar.StepBy(1);
          cnnFund.Connected:=TRUE;
          if Assigned(FormSplash) then FormSplash.ProgressBar.StepBy(1);
          cnnAnalysis.Connected:=TRUE;
          if Assigned(FormSplash) then FormSplash.ProgressBar.StepBy(1);
          cnnSubSale.Connected:=TRUE;
          if Assigned(FormSplash) then FormSplash.ProgressBar.StepBy(1);}     end
     else
     begin
          //断开与服务器的连接
          //Connections.cnnLogin.AppServer.LogUserOnLine(curUserName,'','',FALSE);
          cnnLogin.Connected:=FALSE;
          cnnSysSet.Connected:=FALSE;
          cnnCust.Connected:=FALSE;
          cnnSupply.Connected:=FALSE;    end;
end;procedure TConnections.ReadIPFromFile;
var
     F:TextFile;
     FSearchRec:TSearchRec;
     FindResult:Integer;
begin
     //在当前目录下查找IP地址文件
     FindResult:=FindFirst(GetCurrentDir()+'\'+FileName,faAnyFile,FSearchRec);
     //FindFirst函数返回0,表示成功找到IP地址文件
     if FindResult<>0 then
     begin
          //如果在当前目录下没有找到指定的文件
          //则创建该文件,并把缺省的IP地址写入文件的第一行
          IP:=DefaultIP; //设置缺省的服务器地址                             
          AssignFile(F,GetCurrentDir()+'\'+FileName); //设置新文件的文件名
          try
             Rewrite(F); //创建文件
             try
                WriteLn(F,IP); //写文件
             finally
                CloseFile(F);  //关闭文件
             end;
          except
             on EInOutError do
                ShowMessage('文件不可写');
          end;
     end
     else
     begin
          //如果在当前目录下找到了指定的文件
          AssignFile(F,GetCurrentDir()+'\'+FileName);
          try
             Reset(F);  //打开文件
             try
                ReadLn(F,IP); //读出文件第一行
             finally
                CloseFile(F);
             end;
          except
             on EInOutError do
               ShowMessage('读文件产生错误');
          end;
          IP:=Trim(IP);
     end;
end;
end.

解决方案 »

  1.   

    这是我的一段代码,就是进行联接的:
    ...
    MainForm.ServerSocket1.Active:=True;  //打开服务器端socket
    ...
    ...
    MainForm.ClientSocket1.Address:=ServerIP; //设置ip地址
    MainForm.ClientSocket1.Port:=ServerPort; //设置port
    ...
    if MainForm.ClientSocket1.Active=False then
        MainForm.ClientSocket1.Open;
    while True do    //等待连接
      begin
        if MainForm.ClientSocket1.Active then Break
        else Application.ProcessMessages;
      end;
    ...//连接完成
      

  2.   

    谢谢,请问具体应用的时候如何实现,还要不要其他的控件,仅仅是Connections.connecttoserver就行了吗?然后如何和我的程序相关联?
      

  3.   

    to cowboy1999(牛仔) 
    这些代码我也知道我的程序和这些也差不多,在本地机调试的时候也已经成功了,我就是不知道为什么把client和server放到局域网的两台机器上就不成功,在这种情况下要如何设置port和address,具体的见我的问题,一台机器的ip 192.168.15.189 另一台 192.168.15.179
      

  4.   

    Server不用设地址,Client将HostAddress设为Server所在机器的IP就行了,Port要求Server和Client相同。