局域网内两台机子互发消息(10分)

  • 主题发起人 主题发起人 anni9977
  • 开始时间 开始时间
A

anni9977

Unregistered / Unconfirmed
GUEST, unregistred user!
我想让局域网内的两台机子能相互通讯(互发字符串),但不想通过TCP/IP,听说好像能用UDP,哪位高手用过请帮帮忙好吗?
 
使用IdUDPClient与IDUDPServer,例子如下:
服务端:
unit UDPServerMain;

interface

uses
{$IFDEF Linux}
QGraphics, QControls, QForms, QDialogs, QStdCtrls,
{$ELSE}
windows, messages, graphics, controls, forms, dialogs, IdWinsock, stdctrls,
{$ENDIF}
SysUtils, Classes, IdBaseComponent, IdAntiFreezeBase, IdAntiFreeze,
IdComponent, IdUDPBase, IdUDPClient, IdStack, IdUDPServer, IdSocketHandle;


type
TUDPMainForm = class(TForm)
SourceGroupBox: TGroupBox;
HostNameLabel: TLabel;
HostAddressLabel: TLabel;
HostName: TLabel;
HostAddress: TLabel;
UDPServer: TIdUDPServer;
UDPAntiFreeze: TIdAntiFreeze;
PortLabel: TLabel;
Port: TLabel;
BufferSizeLabel: TLabel;
BufferSize: TLabel;
UDPMemo: TMemo;
procedure FormCreate(Sender: TObject);
procedure UDPServerUDPRead(Sender: TObject; AData: TStream; ABinding: TIdSocketHandle);
private
{ Private declarations }
public
{ Public declarations }
end;

var
UDPMainForm: TUDPMainForm;

implementation

const
HOSTNAMELENGTH = 80;

{$IFDEF MSWINDOWS}{$R *.dfm}{$ELSE}{$R *.xfm}{$ENDIF}

procedure TUDPMainForm.FormCreate(Sender: TObject);
begin
HostName.Caption := UDPServer.LocalName;
HostAddress.Caption := GStack.LocalAddress;
Port.Caption := IntToStr(UDPServer.DefaultPort);
BufferSize.Caption := IntToStr(UDPServer.BufferSize);
UDPServer.Active := True;
end;

procedure TUDPMainForm.UDPServerUDPRead(Sender: TObject; AData: TStream; ABinding: TIdSocketHandle);
var
DataStringStream: TStringStream;
s: String;
begin
DataStringStream := TStringStream.Create('');
try
DataStringStream.CopyFrom(AData, AData.Size);
UDPMemo.Lines.Add('Received "' + DataStringStream.DataString + '" from ' + ABinding.PeerIP + ' on port ' + IntToStr(ABinding.PeerPort));
s := 'Replied from ' + UDPServer.LocalName + ' to "' + DataStringStream.DataString + '"';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, s[1], Length(s));
finally
DataStringStream.Free;
end;
end;


end.
客户端:
unit UDPClientMain;

interface

uses
{$IFDEF Linux}
QGraphics, QControls, QForms, QDialogs, QStdCtrls,
{$ELSE}
windows, messages, graphics, controls, forms, dialogs, IdWinsock, stdctrls,
{$ENDIF}
SysUtils, Classes, IdBaseComponent, IdAntiFreezeBase, IdAntiFreeze,
IdComponent, IdUDPBase, IdUDPClient, IdStack;

type
TUDPMainForm = class(TForm)
SourceGroupBox: TGroupBox;
HostNameLabel: TLabel;
HostAddressLabel: TLabel;
HostName: TLabel;
HostAddress: TLabel;
UDPAntiFreeze: TIdAntiFreeze;
PortLabel: TLabel;
Port: TLabel;
DestinationLabel: TLabel;
DestinationAddress: TLabel;
BufferSizeLabel: TLabel;
BufferSize: TLabel;
UDPMemo: TMemo;
SendButton: TButton;
UDPClient: TIdUDPClient;
procedure FormCreate(Sender: TObject);
procedure SendButtonClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
UDPMainForm: TUDPMainForm;

implementation

const
HOSTNAMELENGTH = 80;
RECIEVETIMEOUT = 5000; // milliseconds

{$IFDEF MSWINDOWS}{$R *.dfm}{$ELSE}{$R *.xfm}{$ENDIF}

procedure TUDPMainForm.FormCreate(Sender: TObject);
begin
Randomize; // remove if you want reproducible results.
HostName.Caption := UDPClient.LocalName;
HostAddress.Caption := GStack.LocalAddress;
Port.Caption := IntToStr(UDPClient.Port);
DestinationAddress.Caption := UDPClient.Host;
BufferSize.Caption := IntToStr(UDPClient.BufferSize);
UDPClient.ReceiveTimeout := RECIEVETIMEOUT;
end;

procedure TUDPMainForm.SendButtonClick(Sender: TObject);
var
MessageID: Integer;
ThisMessage: String;
ReceivedString: String;
begin
MessageID := Random(MAXINT);
ThisMessage := 'Message: ' + IntToStr(MessageID);
UDPMemo.Lines.Add('Sending ' + ThisMessage);
UDPClient.Send(ThisMessage);
ReceivedString := UDPClient.ReceiveString();
if ReceivedString = '' then
UDPMemo.Lines.Add('No response received from the server after ' + IntToStr(UDPClient.ReceiveTimeout) + ' millseconds.')
else
UDPMemo.Lines.Add('Received: ' + ReceivedString)
end;

end.
 
呵呵,刚贴了一个自己写的UDP类,还有DEMO看看吧

http://www.delphibbs.com/keylife/iblog_show.asp?xid=5863
 
请问东兰梦舞,你这是在DELPHI几中用的,我在DELPHI5中怎么找不到这两个控件呀,能告诉我在DELPHI5中怎么用呀,谢谢了!
 
用的是Indy,在
http://www.indyproject.org/
上可以下载,D6/D7自带的也是它。
 
你可以看看有关TCP/IP的书或资料,你说的UDP也是传输层中的一个协议,和TCP是在同一层工作的,TCP/UDP都是通过网络层的IP协议分组来传送报文的,TCP报文通常都被分解为若干个IP分组来传输,而一个UDP报文是不加分解就用一个IP分组来传输,即一个IP分组运输一个UDP报文。UDP和IP一样是一种数据报协议,它是无连接的,不具备确认功能,UDP的报文头也非常简单,只能通过检验和来检查UDP报文的正确性,但就因为这样系统开销非常少,所以传输快速和及时。
UDP还能把报文广播到网络(或子网)内的主机,如果广播到另一个网络(或子网)需要网关的特殊支持。
可以使用广播地址来作为UDP的目的IP地址,
如:
128.112.2.255 255.255.255.0 (B类,子网网络广播地址)
131.40.255.255 255.255.0.0 (B类,网络广播地址)
255.255.255.255 (本以太网内或子网)

 
如果是win2000的话,也可用net send ip message
 
lz999能说一下具体怎么用吗?我是刚开始编程的,什么都不会,请你有点耐心好吗?最好给拷上代码,谢了!
 
udp 是发到所有的机器上,
还是用tcp/ip吧,
 
我就是想发到局域网内的所有机子上
 
faint!!!!
谁说UDP是发到所有机器?????????好好看看《计算家网络》

发到广播地址才是所有机器的。
 
你现在的情况就是对网络的基础知识了解得不够,包括各种协议的概念、功能和结构。
希望你先不要急着去编程,这样的基础写不出什么东西的。
 
参考
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2438519
 
我看还是用TCP/IP吧!
 
接受答案了.
 
后退
顶部