procedure TForm1.FormCreate(Sender: TObject);
begin
if ClientSocket1.Socket.Connected then
ClientSocket1.Socket.SendText('Connected');
end;
向服务器发送消息,有以上代码不能发送,跟踪运行时发现ClientSocket1.Socket.Connected 值为False,
把同样的代码拷到button的onclick事件中就能发送成功,不知何故。。
begin
if ClientSocket1.Socket.Connected then
ClientSocket1.Socket.SendText('Connected');
end;
向服务器发送消息,有以上代码不能发送,跟踪运行时发现ClientSocket1.Socket.Connected 值为False,
把同样的代码拷到button的onclick事件中就能发送成功,不知何故。。
ClientSocket1.Socket.SendText('Connected');
放到OnShow事件中试试
begin
showmessage('aaa');//加上这句
if ClientSocket1.Socket.Connected then
ClientSocket1.Socket.SendText('Connected');
end;
加上上面那句后就能发送成功,奇怪了。。
谁能解释解释啊,谢谢了。
ClientSocket1.Socket.SendText('Connected');
这样的代码不要放在主界面的oncreate事件里
showmessage('aaa');
相当于有延时,并且系统能够处理自己的消息。这句代码改成
Sleep(1000);
Application.ProcessMessages;可以有一样的效果。
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls, AppEvnts, ExtCtrls,IniFiles;type
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
btn1: TButton;
ApplicationEvents1: TApplicationEvents;
tmr1: TTimer;
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure FormShow(Sender: TObject);
procedure tmr1Timer(Sender: TObject);
procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure ReadIniFile;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
begin
if Socket.ReceiveText<>'Disconnect' then
Application.MessageBox(PChar(Socket.ReceiveText),'提示',MB_OK+MB_ICONINFORMATION+MB_TOPMOST)
else
begin
Application.MessageBox(PChar(程序将在三秒后关闭,请保存信息!'),
'提示',MB_OK+MB_ICONINFORMATION+MB_TOPMOST);
tmr1.Enabled:=True;
end;end;procedure TForm1.FormShow(Sender: TObject);
begin
tmr1.Enabled:=False;
end;procedure TForm1.tmr1Timer(Sender: TObject);
begin
Application.Terminate;
end;procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
Socket.Close;
ErrorCode:=0;
end;procedure TForm1.FormCreate(Sender: TObject);
var IP,HOST:string;
begin
ReadIniFile;
IP:=ClientSocket1.Socket.LocalAddress;
HOST:=ClientSocket1.Socket.LocalHost;
if ClientSocket1.Socket.Connected then
ClientSocket1.Socket.SendText('Connected'+'/'+IP+'/'+HOST+'/'+'User'+'/'+'DepartMent');
end;procedure TForm1.ReadIniFile;
var SocketIni:Tinifile;
begin
SocketIni:=TIniFile.Create(ExtractFileDir(Application.ExeName)+'\'+'Server.ini');
ClientSocket1.Active:=False;
ClientSocket1.Address:=SocketIni.ReadString('SERVER','Server IP','');
ClientSocket1.Active:=True;
ShowMessage('AAA');
end;end.
整个代码如上,
还有一个问题就是服务器发送信息时,收到的信息为空,就是这句:
Application.MessageBox(PChar(Socket.ReceiveText),'提示',MB_OK+MB_ICONINFORMATION+MB_TOPMOST)
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
begin
// if Socket.ReceiveText <>'Disconnect' then
Application.MessageBox(PChar(Socket.ReceiveText),'提示',MB_OK+MB_ICONINFORMATION+MB_TOPMOST)
// else
// begin
// Application.MessageBox(PChar(程序将在三秒后关闭,请保存信息!'),
// '提示',MB_OK+MB_ICONINFORMATION+MB_TOPMOST);
// tmr1.Enabled:=True;
// end; end;
这样就能接受信息,否则接收的信息为空
ClientSocket1.Socket.SendText('Connected');
这段代码放在BUTTON_ONCLICK事件里,
在FormCreate事件的最后一行调用BUTTON_ONCLICK试试
sleep(1000)..