C
chuyee
Unregistered / Unconfirmed
GUEST, unregistred user!
我想让两台PC若同时上网即可通信,使用了MAILSLOT,程序如下:
---server.pas---
procedure TForm1.FormCreate(Sender: TObject);
begin
slotname:='//chuyee/mailslot/chess5';
slothandle:=CreateMailslot(
pchar(SlotName), // MailSlot 识别名称
0, // 讯息长度的最大值,设为零表示不限
MAILSLOT_WAIT_FOREVER, // read time-out
nil); // 安全属性,先暂时采用预设值
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
NextSize: DWORD;
MessageCount: DWORD;
Result: BOOLean;
Buffer: pchar;
begin
if slothandle=INVALID_HANDLE_VALUE then exit;
result:=GetMailslotInfo(slothandle, nil,
NextSize, @MessageCount, nil);
if (not Result) or (NextSize = MAILSLOT_NO_MESSAGE)
then Exit;
while Result and (MessageCount <> 0) do
begin
// 资料的长度
Buffer := AllocMem(NextSize + 1);
try
// 读出资料
FileRead(slothandle, Buffer^, NextSize);
memo1.Lines.Add(string(buffer));
finally
FreeMem(Buffer, NextSize + 1);
end;
// 继续看看 MailSlot 中还有没有资料
Result := GetMailslotInfo(slothandle, nil,
NextSize, @MessageCount, nil);
end;
end;
---client.pas---
procedure TForm1.Button1Click(Sender: TObject);
var
str:string;
result:integer;
begin
if slotHandle = INVALID_HANDLE_VALUE then Exit;
str:=edit1.Text;
result:=FileWrite(slothandle, str, Length(str));
if result=-1 then beep;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
slotname:='//chuyee/mailslot/chess5';
slothandle:= CreateFile(pchar(SlotName),
GENERIC_WRITE, // Client 端对於 MailSlot 只能写入
FILE_SHARE_READ, // 设定为可供分享读取
Nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
end;
运行后,server可生成文件,client可写入文件,但server的MessageCount
却总是0。请各位帮忙,送上200分;也可不用MAILSLOT只要能解决在internet
上找到另一台机器即可。
---server.pas---
procedure TForm1.FormCreate(Sender: TObject);
begin
slotname:='//chuyee/mailslot/chess5';
slothandle:=CreateMailslot(
pchar(SlotName), // MailSlot 识别名称
0, // 讯息长度的最大值,设为零表示不限
MAILSLOT_WAIT_FOREVER, // read time-out
nil); // 安全属性,先暂时采用预设值
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
NextSize: DWORD;
MessageCount: DWORD;
Result: BOOLean;
Buffer: pchar;
begin
if slothandle=INVALID_HANDLE_VALUE then exit;
result:=GetMailslotInfo(slothandle, nil,
NextSize, @MessageCount, nil);
if (not Result) or (NextSize = MAILSLOT_NO_MESSAGE)
then Exit;
while Result and (MessageCount <> 0) do
begin
// 资料的长度
Buffer := AllocMem(NextSize + 1);
try
// 读出资料
FileRead(slothandle, Buffer^, NextSize);
memo1.Lines.Add(string(buffer));
finally
FreeMem(Buffer, NextSize + 1);
end;
// 继续看看 MailSlot 中还有没有资料
Result := GetMailslotInfo(slothandle, nil,
NextSize, @MessageCount, nil);
end;
end;
---client.pas---
procedure TForm1.Button1Click(Sender: TObject);
var
str:string;
result:integer;
begin
if slotHandle = INVALID_HANDLE_VALUE then Exit;
str:=edit1.Text;
result:=FileWrite(slothandle, str, Length(str));
if result=-1 then beep;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
slotname:='//chuyee/mailslot/chess5';
slothandle:= CreateFile(pchar(SlotName),
GENERIC_WRITE, // Client 端对於 MailSlot 只能写入
FILE_SHARE_READ, // 设定为可供分享读取
Nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
end;
运行后,server可生成文件,client可写入文件,但server的MessageCount
却总是0。请各位帮忙,送上200分;也可不用MAILSLOT只要能解决在internet
上找到另一台机器即可。