这是我们的代码,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.
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.
解决方案 »
- Delphi打包程序 -----------!
- 如何获得本机在公网的ip地址(连接方式为 adsl + hub)
- 以前用 Dev ExpressPageControl for D6的控件现在想用D7的怎么办呢?
- 一个菜鸟关于treeview的一些幼稚问题
- 各位过来人帮我 !!!!!
- 奇怪!我在D6中用dxBar4.2做了一个窗体模板,用其继承一个新窗体后运行dxBar中的按钮都没了!
- 在form2窗体上的buttonclick中:form1.caption:='newcaption'怎么不执行,其中...
- 调用BDE本地数据库如何设置params,使得user和pwd提问不出现?
- 要命的问题,sqlserver6.5的ODBC配制问题,请高手进来
- 数据保存的一个怪现象
- length(array of char)是setlegnth的长度,还是其中字符串的长度?
- 在一个Excel文件中,怎样只写其中的一列,而其他的列不改变。
...
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;
...//连接完成
这些代码我也知道我的程序和这些也差不多,在本地机调试的时候也已经成功了,我就是不知道为什么把client和server放到局域网的两台机器上就不成功,在这种情况下要如何设置port和address,具体的见我的问题,一台机器的ip 192.168.15.189 另一台 192.168.15.179