怎样做个局域网里实现计算机之间相互通信的程序?(200分)

  • 主题发起人 主题发起人 ccqq
  • 开始时间 开始时间
C

ccqq

Unregistered / Unconfirmed
GUEST, unregistred user!
我想做一个在局域网里实现计算机之间相互通信的程序,类似于98下的winpoup,但winpoup
必须要先启动才行,能不能做到有消息来就自动激活程序,弹出信息窗口来,delphi能做到
吗,具体怎么做呢,或者有没有现成的软件或控件呢(不要用QQ)?给个思路吧!
 
做个常驻在内存中的监控程序对某一端口进行监控...
或者做成一个服务...
 
除非操作系统自己支持,否则是不可能做到“有消息来就自动激活程序”
这本身就是一个矛盾啊,程序未启动你怎么知道“有消息来”呢?
所以,你肯定要有个程序监听才行的。

局域网通信的Delphi源代码到处都有,找找吧。
 
如果你的机器都是WIN2000、WINXP,可以试试“NET SEND“发送网络消息
 
http://gujie.zhibo.net
 
用Delphi Udp控件可以实现,当来消息时会触发一个事件,可参看Demo,很简单。
 
请问楼上的兄台,Udp是在哪一个控件页里?
 
哈,找到了,不必麻烦你了,谢谢
 
Fastnet和Indy里面都有的
 
用他们和多层Access数据库可以做出很好的企业QQ
 
监视端口,如有消息这弹出
 
http://61.132.118.185:8080/dispdocnew.php?id=4228
我的一个小东西,有DEMO和原代码
 
其实好象winpopup可以做到,当对方机没有启动winpopup时,就弹出个“信使服务”的窗
口,请问大侠们,象这种“信使服务”怎么做出来呀?
还有,有没有win2000下的winpopup程序呢?
 
看了“udp”演示,不好意思有个很菜的问题,port在程序里到底有什么作用,在不同的
机器上需要特别指定吗?
 
IP+PORT,就确定了你的通讯对象
 
用Indy,10分钟告定
 
用邮件槽,下面是转贴。

procedure SendNetMessage(YourName, ComputerName,
MessageStr: String);
Var
Rc: LongBool;
MsHandle: Longint;
MsgText: String;
BytesWritten: Cardinal;
MailSlotName: String;
begin
MailSlotName := '//' + ComputerName + '/mailslot/messngr';
MsgText := YourName + Chr(0) + ComputerName + Chr(0) + MessageStr + Chr(0);
MsHandle := CreateFile(PChar(MailSlotName), GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, $FFFFFFFF);
if MsHandle = INVALID_HANDLE_VALUE then
RaiseLastWin32Error;

RC := WriteFile(MsHandle, Pointer(MsgText)^, Length(MsgText), BytesWritten, 0);

if not Rc then
RaiseLastWin32Error;

Rc := CloseHandle(MsHandle);
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
SendNetMessage('yang', 'yang', 'Hello');
end;

WinPopup
This here started with me getting fed up with the way WinPopup was working. I
always had this big visible block in my taskbar in Windows 95, and therefore I
often did not use my WinPopup - which the others often thought irritateing.
Also the fact that I was using non English letters in my login name, which made
the Danish versions (not the English versions) of WinPopup ignore sending
messages to me, even I had started WinPopup.

So what do a programmer do? He deside to make his own version of WinPopup! Yes,
but how?

I looked around in the WIN32 API for functions which were meeting the
functionabillity of WinPopup, and found mailslots. Mailslots is a conection
less service, which is made directly accessible in Windows 95 and NT. The
mailslots create a kind of virtual file on your machine (when your are the
owner of a mailslot), where other machines can write into.

The mailslots should be tried out, so I implemented the functionallity in a
program, and it all looked like the functionallity of WinPopup, but I was
still not any further because a mailslot have a name, and what name did
Microsoft choose to there system messages (Printer notification and WinPopup
messages) - I could not guess :(

I created a usable mailslot object, and made it avalible on the Internet so
other could user my work now when I coudn't (wasn't that nice of me?). After
a week - or so - someone wrote to me and asked if I knew how he could send a
message to WinPopup :( - and ofcource I cound not help him. But this guy
started searching himself, and a few days later he mailed me the "secret"
mailslot name "messngr".

Next we were looking for the format of a message - that was not so difficult
to find out of by sending messages. A recieved block of data is threaded as
folow three NULL terminated strings after each other :


Sender

Reciever

Message

So 'MARTIN'#0'DON'#0'Hi'#0 is a message from Martin to Don with the message Hi.
In the next I'll tell how to use mailslots (the Windows - WinPopup one as example),
using examples in a Pascal-like code. The code parts is not fully explaining
(if you want to use mailslots in a program you have a compiler and a helpfile
where you can find the exact syntax) - I'll just tell you about what you need
for making use of this function.
If you are Delphi 2.0 developer then you can go to my VCL page where I have a
fully functional VCL, with all the source code, for free download. For writing
to an existing mailslot (send a message over the network), all you have to do
is to thread the mailslot as a file on the target machine, whith this filename :


"//"+Server_Name+"/mailslot/messngr"
To write to this file, first open it like this :


Handle := CreateFile("//THOR/mailslot/messngr",
GENERIC_WRITE,
FILE_SHARE_READ,
NIL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
-1)
The handle returned can then be used in a call to write to the file like this :


WriteFile(Handle,
Data,
Size(Data),
BytesWritten,
NIL)
It is wise to close the handle after user with a call like this :


CloseHandle(Handle)
That was easy - wasn't it?

To make a mailslot avalible for other to use, you have to create a mailslot
usong the CreateMailslot function like this :


Handle := CreateMailSlot("//./mailslot/messngr",
0,
$FFFFFFFF,
NIL)
The handle returned from the CreateMailSlot function should then be used for accessing the mailslot. Note the value $FFFFFFFF should also be called "MAILSLOT_WAIT_FOREVER", but my Delphi 2.0 does not reconize the name. The value is also said in the WIN32 API helpfile to make a read from the mailslot go into a blocked waitstate until something is arriving into it, but I can not make it do so :(.

For checking up anything is in the mailslot call the GetMailSlotInfo function


GetMailSlotInfo(Handle,NIL, NextSize, @Waiting, NIL)
where NextSize and Waiting is both defined as DWord's.
NextSize will after a call contain the size of the next message
(for allocation of buffer) and Waiting will contain the number of messages waiting.

For reading from a mailslot, all which is needed to be done, is to call the ReadFile function with the handle recieved from the CreatMailSlot function, like this :


ReadFile(Handle,Buffer^,NextSize,ReadSize,NIL)
After end use of the mailslot I'll advise you to close the handle with the CloseHandle function (as far as I have understood, it is not strictly necessary) like this :


CloseHandle(FHandle)
Before continuing to the next problem, I'll like to thank Don Hass for telleing me about the "messngr" mailslot.
 
<用Indy,10分钟告定>

大哥啊,详细点啊!
 
我说错了,你可以用这个函数:
function NetMessageBufferSend(ServerName: PWideChar; MsgName: PWideChar;
FromName: PWideChar; Buf: PWideChar; BufLen: integer): Integer; stdcall;
external 'netapi32.dll';
 
后退
顶部