F
FreeFish
Unregistered / Unconfirmed
GUEST, unregistred user!
我照资料用了TNMMsg和TNMMsgServ组件来编写一个聊天的程序,它的确能运行,
但似乎在用TNMMsg的PostIt方法互相发送消息的时候却不稳定,经常报错,请
问这是什么原因?如何修改?以下是代码,请各位大侠指点。非常感谢!
unit msgdem;
interface
uses
Windows, SysUtils, Classes, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, NMMSG, Psock, ComCtrls;
// NMMSG为控件当加入TNMMSG、TNMMSGSERV后Delphi自动加入
type
TForm1 = class(TForm)
Panel1: TPanel;
Memo1: TMemo;
Edit1: TEdit;
NMMsg1: TNMMsg;
NMMSGServ1: TNMMSGServ;
Label1: TLabel;
Edit2: TEdit;
Label2: TLabel;
Edit3: TEdit;
StatusBar1: TStatusBar;
Label3: TLabel;
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure NMMSGServ1MSG(Sender: TComponent; const sFrom, sMsg: String);
procedure NMMsg1MessageSent(Sender: TObject);
procedure NMMsg1Connect(Sender: TObject);
procedure NMMsg1ConnectionFailed(Sender: TObject);
procedure NMMsg1Disconnect(Sender: TObject);
procedure NMMsg1HostResolved(Sender: TComponent);
procedure NMMsg1Status(Sender: TComponent; Status: String);
procedure NMMsg1InvalidHost(var handled: Boolean);
procedure NMMSGServ1ClientContact(Sender: TObject);
procedure NMMSGServ1Status(Sender: TComponent; Status: String);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
//回车事件
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
If Key = #13 then
Begin
NMMsg1.Host := Edit3.Text;
NMMsg1.FromName := Edit2.Text;
NMMsg1.PostIt(Edit1.Text);
Edit1.Text:='';
End;
end;
//当服务器接受到消息时触发OnMSG事件,作如下处理
procedure TForm1.NMMSGServ1MSG(Sender: TComponent; const sFrom,
sMsg: String);
begin
Memo1.Lines.Add(sFrom+'::'+sMsg); //显示到来的消息
end;
procedure TForm1.NMMsg1MessageSent(Sender: TObject);
begin
StatusBar1.SimpleText:='Message sent!';
end;
procedure TForm1.NMMsg1Connect(Sender: TObject);
begin
StatusBar1.SimpleText := 'Connected';
end;
procedure TForm1.NMMsg1ConnectionFailed(Sender: TObject);
begin
ShowMessage('Connection Failed');
end;
procedure TForm1.NMMsg1Disconnect(Sender: TObject);
begin
StatusBar1.SimpleText := 'Disconnected';
end;
procedure TForm1.NMMsg1HostResolved(Sender: TComponent);
begin
StatusBar1.SimpleText := 'Host Resolved';
end;
procedure TForm1.NMMsg1Status(Sender: TComponent; Status: String);
begin
If StatusBar1 <> nil then
StatusBar1.SimpleText := status;
end;
procedure TForm1.NMMsg1InvalidHost(var handled: Boolean);
var
TmpStr: String;
begin
If InputQuery('Invalid Host!', 'Specify a new host:', TmpStr) then
Begin
NMMsg1.Host := TmpStr;
Handled := TRUE;
End;
end;
procedure TForm1.NMMSGServ1ClientContact(Sender: TObject);
//当有客户机连接到本服务器时作如下处理
begin
NMMsgServ1.ReportLevel := Status_Basic; //控制OnStatus事件和Status属性汇报的细节量为基本信息量
NMMsgServ1.TimeOut := 90000; //超时设置为90秒
StatusBar1.SimpleText := 'Client connected';
end;
procedure TForm1.NMMSGServ1Status(Sender: TComponent; Status: String);
begin
If StatusBar1 <> nil then
StatusBar1.SimpleText := status;
end;
end.
但似乎在用TNMMsg的PostIt方法互相发送消息的时候却不稳定,经常报错,请
问这是什么原因?如何修改?以下是代码,请各位大侠指点。非常感谢!
unit msgdem;
interface
uses
Windows, SysUtils, Classes, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, NMMSG, Psock, ComCtrls;
// NMMSG为控件当加入TNMMSG、TNMMSGSERV后Delphi自动加入
type
TForm1 = class(TForm)
Panel1: TPanel;
Memo1: TMemo;
Edit1: TEdit;
NMMsg1: TNMMsg;
NMMSGServ1: TNMMSGServ;
Label1: TLabel;
Edit2: TEdit;
Label2: TLabel;
Edit3: TEdit;
StatusBar1: TStatusBar;
Label3: TLabel;
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure NMMSGServ1MSG(Sender: TComponent; const sFrom, sMsg: String);
procedure NMMsg1MessageSent(Sender: TObject);
procedure NMMsg1Connect(Sender: TObject);
procedure NMMsg1ConnectionFailed(Sender: TObject);
procedure NMMsg1Disconnect(Sender: TObject);
procedure NMMsg1HostResolved(Sender: TComponent);
procedure NMMsg1Status(Sender: TComponent; Status: String);
procedure NMMsg1InvalidHost(var handled: Boolean);
procedure NMMSGServ1ClientContact(Sender: TObject);
procedure NMMSGServ1Status(Sender: TComponent; Status: String);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
//回车事件
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
If Key = #13 then
Begin
NMMsg1.Host := Edit3.Text;
NMMsg1.FromName := Edit2.Text;
NMMsg1.PostIt(Edit1.Text);
Edit1.Text:='';
End;
end;
//当服务器接受到消息时触发OnMSG事件,作如下处理
procedure TForm1.NMMSGServ1MSG(Sender: TComponent; const sFrom,
sMsg: String);
begin
Memo1.Lines.Add(sFrom+'::'+sMsg); //显示到来的消息
end;
procedure TForm1.NMMsg1MessageSent(Sender: TObject);
begin
StatusBar1.SimpleText:='Message sent!';
end;
procedure TForm1.NMMsg1Connect(Sender: TObject);
begin
StatusBar1.SimpleText := 'Connected';
end;
procedure TForm1.NMMsg1ConnectionFailed(Sender: TObject);
begin
ShowMessage('Connection Failed');
end;
procedure TForm1.NMMsg1Disconnect(Sender: TObject);
begin
StatusBar1.SimpleText := 'Disconnected';
end;
procedure TForm1.NMMsg1HostResolved(Sender: TComponent);
begin
StatusBar1.SimpleText := 'Host Resolved';
end;
procedure TForm1.NMMsg1Status(Sender: TComponent; Status: String);
begin
If StatusBar1 <> nil then
StatusBar1.SimpleText := status;
end;
procedure TForm1.NMMsg1InvalidHost(var handled: Boolean);
var
TmpStr: String;
begin
If InputQuery('Invalid Host!', 'Specify a new host:', TmpStr) then
Begin
NMMsg1.Host := TmpStr;
Handled := TRUE;
End;
end;
procedure TForm1.NMMSGServ1ClientContact(Sender: TObject);
//当有客户机连接到本服务器时作如下处理
begin
NMMsgServ1.ReportLevel := Status_Basic; //控制OnStatus事件和Status属性汇报的细节量为基本信息量
NMMsgServ1.TimeOut := 90000; //超时设置为90秒
StatusBar1.SimpleText := 'Client connected';
end;
procedure TForm1.NMMSGServ1Status(Sender: TComponent; Status: String);
begin
If StatusBar1 <> nil then
StatusBar1.SimpleText := status;
end;
end.