能不能给我也个最简单的IDTCPCLIENT和IDTCPSERVER的范例。我只要如此:
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
begin
showmessage(AThread.Connection.ReadLn('',-2));
end;procedure TForm1.Button1Click(Sender: TObject);
begin
if not IdTCPClient1.Connected then
IdTCPClient1.Connect();
IdTCPClient1.WriteLn('dd');
end;
在DELPHI调试环境下可以使用,但一变成EXE文件来运行时就没有反映,并在关闭程序时出错误。
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
begin
showmessage(AThread.Connection.ReadLn('',-2));
end;procedure TForm1.Button1Click(Sender: TObject);
begin
if not IdTCPClient1.Connected then
IdTCPClient1.Connect();
IdTCPClient1.WriteLn('dd');
end;
在DELPHI调试环境下可以使用,但一变成EXE文件来运行时就没有反映,并在关闭程序时出错误。
解决方案 »
- 远程连接Midas服务提示“拒绝访问”
- 通过Http的Post方式传递参数,接受方需要怎样获取?(急)
- 我手头有一个COM组件,但是怎么用呢?我一点概念都没有,我怎么创建它,使用它,完了释放它?(UP的也给分拉!)
- 很急!!希望大家帮帮忙!!
- 如何qreport手动加入一列.----------急-------------谢谢!!!
- 怎样在指定窗体中划可移动的直线?
- 限制输入问题
- 哪有关于DevExpress控件的编程教学资料下载?
- 急,,在线等待,,,小问题,,Socket.SendBuf/Socket.ReceiveBuf一次最大能发送/接收多少字
- 如何做浮动按钮
- 使用idTCpServer和IdTCPClient,当一个客户端连上来时,怎样知道它的ip地址?
- 如何IdTCPClient使用?
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPServer, StdCtrls,
shellAPI;const WM_NID=WM_USER+1000;type TCommBlock = record // 通讯结构
SenderName, // 发信方名字
Msg : string[100]; // 消息
end;type
TForm1 = class(TForm)
IdTCPServer: TIdTCPServer;
ChatLog: TMemo;
procedure IdTCPServerConnect(AThread: TIdPeerThread);
procedure IdTCPServerDisconnect(AThread: TIdPeerThread);
procedure IdTCPServerExecute(AThread: TIdPeerThread);
procedure FormCreate(Sender: TObject); procedure WMNID(var msg:TMessage);message WM_NID;
procedure FormClose(Sender: TObject; var Action: TCloseAction); private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
NotifyIcon:TNotifyIconData;implementation{$R *.dfm}procedure TForm1.IdTCPServerConnect(AThread: TIdPeerThread);
begin
AThread.Data:=TObject(AThread.Connection.LocalName);
ChatLog.Lines.Add(TimeToStr(Time)+' 来自计算机 "'+AThread.Connection.LocalName+'的呼叫');
end;procedure TForm1.IdTCPServerDisconnect(AThread: TIdPeerThread);
begin
ChatLog.Lines.Add(TimeToStr(Time)+' 断开的链接'+String(AThread.Data));
AThread.Data := nil;
end;procedure TForm1.IdTCPServerExecute(AThread: TIdPeerThread);
var
CommBlock : TCommBlock;
begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
AThread.Connection.ReadBuffer(CommBlock, SizeOf (CommBlock));
ChatLog.Lines.Add(CommBlock.SenderName+':'+CommBlock.Msg);
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
self.IdTCPServer.Active:=True;
//NotifyIcon为全局变量,在程序的开头已经定义了
with NotifyIcon do
begin
cbSize:=SizeOf(TNotifyIconData);
Wnd:=Handle; //指向当前窗体Form1的句柄
uID:=1;
uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;
uCallBackMessage:=WM_NID;
hIcon:=Application.Icon.Handle;
szTip:='张家恶少';
end;
//把设置好的变量NotifyIcon加入到系统中以便处理
Shell_NotifyIcon(NIM_ADD,@NotifyIcon);
end;procedure TForm1.WMNID(var msg:TMessage);//message WM_NID;
begin
case msg.LParam of
WM_LBUTTONUp:
Form1.Visible:=not Form1.Visible;
WM_RBUTTONUP:
ShowMessage('您点击的是右键');
End;
End;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Shell_NotifyIcon(NIM_DELETE,@NotifyIcon);
end;end.unit UChatClient;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
StdCtrls, Buttons;type
TCommBlock = record // 通讯结构
SenderName, // 发信方名字
Msg : string[100]; // 消息
end;type
TChatClientFrm = class(TForm)
IdTCPClient: TIdTCPClient;
ChatLog: TMemo;
RemoteIP: TEdit;
Label1: TLabel;
Label2: TLabel;
RemotePort: TEdit;
Label3: TLabel;
NickName: TEdit;
LocalPort: TEdit;
Label4: TLabel;
BitLogin: TBitBtn;
BitConnect: TBitBtn;
InputBox: TEdit;
BitSend: TBitBtn;
procedure BitConnectClick(Sender: TObject);
procedure BitSendClick(Sender: TObject);
procedure BitLoginClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end; TClientHandleThread = class(TThread)
private
CB: TCommBlock;
procedure HandleInput;
protected
procedure Execute; override;
end;var
ChatClientFrm: TChatClientFrm;
ClientHandleThread: TClientHandleThread; // variable (type see above)implementation{$R *.dfm}procedure TChatClientFrm.BitConnectClick(Sender: TObject);
begin
if trim(RemoteIP.Text)='' then
begin
ShowMessage('服务器IP不能为空');
Exit;
end; if trim(RemotePort.Text)='' then
begin
ShowMessage('服务器端口不能为空');
Exit;
end; IdTCPClient.Port:=StrToInt(RemotePort.Text);
IdTCPClient.Host:=RemoteIP.Text;
try
IdTCPClient.Connect; // in Indy < 8.1 leave the parameter away
ClientHandleThread := TClientHandleThread.Create(True);
ClientHandleThread.FreeOnTerminate:=True;
ClientHandleThread.Resume;
except
on E: Exception do MessageDlg ('链接错误:'+#13+E.Message, mtError, [mbOk], 0);
end;
BitSend.Enabled := true;
end;procedure TChatClientFrm.BitSendClick(Sender: TObject);
var
CommBlock : TCommBlock;
begin
CommBlock.SenderName := NickName.Text;
CommBlock.Msg := InputBox.Text;
IdTCPClient.WriteBuffer (CommBlock, SizeOf (CommBlock), true);
ChatLog.Lines.Add(CommBlock.SenderName+':'+CommBlock.Msg);
end;procedure TChatClientFrm.BitLoginClick(Sender: TObject);
begin
if trim(LocalPort.Text)='' then
begin
ShowMessage('本地端口不能为空');
Exit;
end; //IdTCPServer.DefaultPort:=StrToInt(LocalPort.Text);
//IdTCPServer.Active := true;
end;procedure TClientHandleThread.Execute;
begin
while not Terminated do
begin
if not ChatClientFrm.IdTCPClient.Connected then
Terminate
else
try
ChatClientFrm.IdTCPClient.ReadBuffer(CB, SizeOf (CB));
Synchronize(HandleInput);
except
end;
end;
end;procedure TClientHandleThread.HandleInput;
begin
ChatClientFrm.ChatLog.Lines.Add (CB.SenderName + ':' + CB.Msg);
end;
procedure TChatClientFrm.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
if IdTCPClient.Connected then
begin
ClientHandleThread.Terminate;
IdTCPClient.Disconnect;
end;
end;end.
//Delphi 7 网路高级编程里有,飞思科技