新
新手101010
Unregistered / Unconfirmed
GUEST, unregistred user!
服务器端收不到消息,大家看看代码是怎么回事
Server:
var
Form1: TForm1;
MT: TMyThread;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
MT := TMyThread.Create(False, Edit1.Text);
end;
{ TMyThread }
procedure TMyThread.AddMsg();
begin
form1.Memo1.Lines.Add(Msg);
end;
constructor TMyThread.Create(CreateSuspended: Boolean; const mailslotStr: string);
begin
inherited Create(CreateSuspended);
Mailslot := CreateMailslot(Pchar(format('//./Mailslot/%s', [mailslotStr])), 0, MAILSLOT_WAIT_FOREVER, NIL);
if Mailslot = INVALID_HANDLE_VALUE then
begin
ShowMessage('创建Mailslot失败');
Exit;
end;
end;
destructor TMyThread.Destroy;
begin
inherited;
end;
procedure TMyThread.Execute;
var
Buf: String;
ReCount: Cardinal;
begin
while not Terminated do
begin
setLength(Buf, 255);
ReadFile(Mailslot, Buf, 255, RECount, nil);
if RECount > 0 then
begin
Synchronize(AddMsg);
Buf := '';
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
if Assigned(MT) then
MT.Free;
end;
client:
var
Str: String;
hFile: THandle;
WriteCount: Cardinal;
begin
hFile := CreateFile(Pchar(Format('//./mailslot/%s', [Edit1.Text])), GENERIC_WRITE,
FILE_SHARE_READ, NIL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if hFile = INVALID_HANDLE_VALUE then
BEGIN
ShowMessage(format('连接服务器%s失败', [Edit1.Text]));
Exit;
end;
Str := Edit2.Text;
if not WriteFile(hFile, Str, Length(Str), WriteCount, 0) then
ShowMessage('发送数据出错');
CloseHandle(hFile);
end;
Server:
var
Form1: TForm1;
MT: TMyThread;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
MT := TMyThread.Create(False, Edit1.Text);
end;
{ TMyThread }
procedure TMyThread.AddMsg();
begin
form1.Memo1.Lines.Add(Msg);
end;
constructor TMyThread.Create(CreateSuspended: Boolean; const mailslotStr: string);
begin
inherited Create(CreateSuspended);
Mailslot := CreateMailslot(Pchar(format('//./Mailslot/%s', [mailslotStr])), 0, MAILSLOT_WAIT_FOREVER, NIL);
if Mailslot = INVALID_HANDLE_VALUE then
begin
ShowMessage('创建Mailslot失败');
Exit;
end;
end;
destructor TMyThread.Destroy;
begin
inherited;
end;
procedure TMyThread.Execute;
var
Buf: String;
ReCount: Cardinal;
begin
while not Terminated do
begin
setLength(Buf, 255);
ReadFile(Mailslot, Buf, 255, RECount, nil);
if RECount > 0 then
begin
Synchronize(AddMsg);
Buf := '';
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
if Assigned(MT) then
MT.Free;
end;
client:
var
Str: String;
hFile: THandle;
WriteCount: Cardinal;
begin
hFile := CreateFile(Pchar(Format('//./mailslot/%s', [Edit1.Text])), GENERIC_WRITE,
FILE_SHARE_READ, NIL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if hFile = INVALID_HANDLE_VALUE then
BEGIN
ShowMessage(format('连接服务器%s失败', [Edit1.Text]));
Exit;
end;
Str := Edit2.Text;
if not WriteFile(hFile, Str, Length(Str), WriteCount, 0) then
ShowMessage('发送数据出错');
CloseHandle(hFile);
end;