这是服务器
unit PipnamedThread;
interface
uses
Classes,Windows,Sysutils,stdctrls;
const
MSGBUF=1024;
FILBUF=4096;
PIPE='//./Pipe/';
type
TPipType=(dtSendFile,dtClose,dtSendMsg,dtOpen);
TPipnamedThread = class(TThread)
private
{ Private declarations }
FMemo:TMemo;
FPip:THandle;
Fhandle:THandle;
Fdelaytime:integer;
Fname:string;
szName
Char;
Msg:string;
procedure ErrMsg(Msg:string);
procedure LogMsg;
protected
procedure Execute; override;
public
property name:string read Fname;
property delaytime:integer write Fdelaytime;
constructor CreatePip(handle:THandle;name:string;Memo:TMemo);
destructor Destroy;override;
end;
{ TPipServer = class(TThread)
private
protected
public
end; }
implementation
{ Important: Methods and properties of objects in VCL can only be used in a
method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TPipnamedThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ TPipnamedThread }
procedure TPipnamedThread.LogMsg;
begin
FMemo.Lines.Add(Msg);
end;
procedure TPipnamedThread.ErrMsg(Msg:string);
begin
MessageBox(Fhandle,PChar(Msg),'错误',MB_ICONERROR);
end;
constructor TPipnamedThread.CreatePip(handle:THandle;name:string;Memo:TMemo);
begin
inherited Create(true);
Freeonterminate:=true;
szName:=PChar(PIPE+name);
Fname:=StrPas(szName);
Fhandle:=handle;
FMemo:=Memo;
Fdelaytime:=1000;
end;
procedure TPipnamedThread.Execute;
var
Buffer
Char;
byteread
WORD;
pasStr:string;
begin
FPip:=CreateNamedPipe(PChar(Fname),PIPE_ACCESS_DUPLEX,PIPE_TYPE_MESSAGE and
PIPE_READMODE_BYTE,1,0,0,1000,nil);
if FPip=INVALID_HANDLE_VALUE then
begin
ErrMsg(Format('创建命名管道错误,错误代码是:%d',
[GetLastError()]));
exit;
end;
Msg:='创建命名管道成功!';
Synchronize(LogMsg);
if ConnectNamedPipe(FPip,nil)=false then
begin
ErrMsg(Format('连接命名管道失败,错误代码是:%d',
[GetLastError()]));
exit;
end;
GetMem(Buffer,MSGBUF);
try
while (FPip<>INVALID_HANDLE_VALUE) and (not Terminated) do
begin
FillChar(Buffer^,MSGBUF,0);
if ReadFile(FPip,Buffer^,FILBUF,byteread,nil)<>false then
begin
pasStr:=copy(Buffer,1,MSGBUF);
Msg:='收到消息为:'+pasStr;
Synchronize(LogMsg);
end;
end;
if DisconnectNamedPipe(FPip)=false then
begin
ErrMsg(Format('断开命名管道失败,错误代码是:%d',
[GetLastError()]));
end;
finally
FreeMem(Buffer);
end;
end;
destructor TPipnamedThread.Destroy;
begin
CloseHandle(FPip);
Msg:='关闭命名管道成功!';
Synchronize(LogMsg);
inherited destroy;
end;
end.
procedure TForm1.Button2Click(Sender: TObject);
var
szName
Char;
err:string;
begin
GetMem(szName,80);
wsprintf(szName,PChar(Format('//%s/Pipe/%s',[Edit2.text,edit1.text])));
if WaitNamedPipe(szName,NMPWAIT_WAIT_FOREVER)=false then
begin
err:=Format('等待命名管道失败,错误代码是:%d',[GetLastError()]);
MessageBox(handle,pchar(err),'错误',MB_ICONERROR);
exit;
end;
AHandle:=CreateFile(szName,
GENERIC_READ or GENERIC_WRITE,0,
nil,OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if AHandle=INVALID_HANDLE_VALUE then
begin
err:=Format('连接命名管道失败,错误代码是:%d',[GetLastError()]);
MessageBox(handle,pchar(err),'错误',MB_ICONERROR);
exit;
end;
FreeMem(szName);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
szWrited
Char;
writed
WORD;
err:string;
begin
if (AHandle=INVALID_HANDLE_VALUE) then
exit;
GetMem(szWrited,256);
FillChar(szWrited^,256,0);
StrCopy(szWrited,PChar(Memo1.text));
if WriteFile(AHandle,szWrited^,256,writed,nil)=false then
begin
err:=Format('发送数据出错,错误代码是:%d',[GetLastError()]);
MessageBox(Handle,PChar(err),'错误',MB_ICONERROR);
exit;
end;
FreeMem(szWrited);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
closehandle(AHandle);
end;
procedure TForm1.Memo1Enter(Sender: TObject);
begin
Memo1.BorderStyle:=bsSingle;
end;
procedure TForm1.Memo1Exit(Sender: TObject);
begin
Memo1.BorderStyle:=bsNone;
end;
end.
后面的是客护短