ServerMain.pas
上传用户:psxgmh
上传日期:2013-04-08
资源大小:15112k
文件大小:3k
- unit ServerMain;
- interface
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, IPC, ComCtrls, Spin;
- type
- TfrmServer = class(TForm)
- Label6: TLabel;
- edtSessionName: TEdit;
- btnListen: TButton;
- GroupBox1: TGroupBox;
- Label3: TLabel;
- Label4: TLabel;
- edtReceiveString: TEdit;
- sedtReceiveInteger: TSpinEdit;
- StatusBar: TStatusBar;
- Label5: TLabel;
- GroupBox2: TGroupBox;
- Label1: TLabel;
- Label2: TLabel;
- edtSendString: TEdit;
- sedtSendInteger: TSpinEdit;
- btnSend: TButton;
- procedure btnSendClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure btnListenClick(Sender: TObject);
- private
- FClientHwnd: HWND;
- IPCServer: TIPCServer; //定义IPCServer
- Procedure IPCServerAfterOpen(Sender : Tobject);
- procedure IPCServerConnect(AHwnd: HWND);
- procedure IPCServerDisconnect(AHwnd: HWND);
- procedure IPCServerClientData(MsgPointer: Pointer; AHwnd: HWND);
- procedure IPCServerAfterClose(Sender: TObject);
- public
- { Public declarations }
- end;
- type
- {定义一个发送和接收数据记录类型}
- PIPCData = ^TIPCData;
- TIPCData = record
- SendString: string[30];
- SendInteger: integer;
- end;
- var
- frmServer: TfrmServer;
- implementation
- {$R *.DFM}
- //当窗体被创建时
- procedure TfrmServer.FormCreate(Sender: TObject);
- begin
- //创建IPCServer
- IPCServer := TIPCServer.Create(self);
- IPCServer.OnAfterOpen := IPCServerAfterOpen;
- IPCServer.OnConnect := IPCServerConnect;
- IPCServer.OnDisconnect := IPCServerDisConnect;
- IPCServer.OnAfterClose := IPCServerAfterClose;
- IPCServer.OnClientData := IPCServerClientData;
- end;
- //监听连接
- procedure TfrmServer.btnListenClick(Sender: TObject);
- begin
- if not IPCServer.Active then
- IPCServer.SessionName := edtSessionName.Text;
- IPCServer.Open;
- end;
- //发送数据
- procedure TfrmServer.btnSendClick(Sender: TObject);
- var
- MsgData: TIPCData;
- begin
- if IPCServer.Active then
- begin
- MsgData.SendString := edtSendString.Text;
- MsgData.SendInteger := SedtSendInteger.Value;
- IPCServer.SendMsg(@MsgData, FClientHwnd, Sizeof(MsgData));
- end;
- end;
- //服务器连接事件
- procedure TfrmServer.IPCServerConnect(AHwnd: HWND);
- begin
- FClientHwnd := AHwnd;
- statusbar.Panels[0].Text := '连接状态:已连接';
- statusbar.Refresh;
- end;
- //服务器断开事件
- procedure TfrmServer.IPCServerDisconnect(AHwnd: HWND);
- begin
- statusbar.Panels[0].Text := '连接状态:未连接';
- statusbar.Refresh;
- end;
- //接收客户端数据
- procedure TfrmServer.IPCServerClientData(MsgPointer: Pointer; AHwnd: HWND);
- var
- AMsg: TIPCData;
- begin
- FClientHwnd := AHwnd;
- AMsg := TIPCData(MsgPointer^);
- edtReceiveString.Text := AMsg.SendString;
- sedtReceiveInteger.Value := AMsg.SendInteger;
- end;
- //服务器端被打开事件
- procedure TfrmServer.IPCServerAfterOpen(sender : TObject);
- begin
- statusbar.Panels[0].Text := '连接状态:处于监听状态....';
- statusbar.Refresh;
- FClientHwnd := 0;
- end;
- //服务器端被断开事件
- procedure TfrmServer.IPCServerAfterClose(Sender: TObject);
- begin
- statusbar.Panels[0].Text := '连接状态:未连接';
- statusbar.Refresh;
- FClientHwnd := 0;
- end;
- end.