G
gigglegile
Unregistered / Unconfirmed
GUEST, unregistred user!
刚开始学udp,在网上下了一段代码,是用来聊天的,我在局域网上的两台电脑上测试,可是并不能通信,请问是什么原因???
代码如下:
Indy Udp聊天实例
unit frmMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Buttons, IdBaseComponent, IdComponent,
IdUDPBase, IdUDPClient, IdAntiFreezeBase, IdAntiFreeze, IdUDPServer,
IdSocketHandle;
type
TCommBlock = record // 通讯结构
SenderName, // 发信方名字
Msg : string[100]; // 消息
end;
TFrmChat = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
RemoteIP: TLabeledEdit;
RemotePort: TLabeledEdit;
NickName: TLabeledEdit;
LocalPort: TLabeledEdit;
InputBox: TEdit;
BitSend: TBitBtn;
IdUDPClient: TIdUDPClient;
ChatLog: TMemo;
IdUDPServer: TIdUDPServer;
BitSet: TBitBtn;
procedure BitSendClick(Sender: TObject);
procedure IdUDPServerUDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
procedure BitSetClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FrmChat: TFrmChat;
implementation
const
RECIEVETIMEOUT = 5000; // 毫秒
{$R *.dfm}
procedure TFrmChat.BitSendClick(Sender: TObject);
var
Comm:TCommBlock;
begin
Comm.SenderName:=NickName.Text;
Comm.Msg:=InputBox.Text;
ChatLog.Lines.Add(Comm.SenderName+':' + Comm.Msg);
IdUDPClient.SendBuffer(Comm,sizeof(Comm));
end;
procedure TFrmChat.IdUDPServerUDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
Comm : TCommBlock;
begin
AData.ReadBuffer(Comm,AData.Size);
ChatLog.Lines.Add(Comm.SenderName+':'+Comm.Msg);
end;
procedure TFrmChat.BitSetClick(Sender: TObject);
begin
IdUDPClient.Host := RemoteIP.Text;
IdUDPClient.Port := StrToInt(RemotePort.Text);
IdUDPClient.ReceiveTimeout := RECIEVETIMEOUT;
IdUDPServer.DefaultPort := StrToInt(LocalPort.Text);
IdUDPServer.Active := True;
BitSend.Enabled:=True;
end;
end.
代码如下:
Indy Udp聊天实例
unit frmMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Buttons, IdBaseComponent, IdComponent,
IdUDPBase, IdUDPClient, IdAntiFreezeBase, IdAntiFreeze, IdUDPServer,
IdSocketHandle;
type
TCommBlock = record // 通讯结构
SenderName, // 发信方名字
Msg : string[100]; // 消息
end;
TFrmChat = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
RemoteIP: TLabeledEdit;
RemotePort: TLabeledEdit;
NickName: TLabeledEdit;
LocalPort: TLabeledEdit;
InputBox: TEdit;
BitSend: TBitBtn;
IdUDPClient: TIdUDPClient;
ChatLog: TMemo;
IdUDPServer: TIdUDPServer;
BitSet: TBitBtn;
procedure BitSendClick(Sender: TObject);
procedure IdUDPServerUDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
procedure BitSetClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FrmChat: TFrmChat;
implementation
const
RECIEVETIMEOUT = 5000; // 毫秒
{$R *.dfm}
procedure TFrmChat.BitSendClick(Sender: TObject);
var
Comm:TCommBlock;
begin
Comm.SenderName:=NickName.Text;
Comm.Msg:=InputBox.Text;
ChatLog.Lines.Add(Comm.SenderName+':' + Comm.Msg);
IdUDPClient.SendBuffer(Comm,sizeof(Comm));
end;
procedure TFrmChat.IdUDPServerUDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
Comm : TCommBlock;
begin
AData.ReadBuffer(Comm,AData.Size);
ChatLog.Lines.Add(Comm.SenderName+':'+Comm.Msg);
end;
procedure TFrmChat.BitSetClick(Sender: TObject);
begin
IdUDPClient.Host := RemoteIP.Text;
IdUDPClient.Port := StrToInt(RemotePort.Text);
IdUDPClient.ReceiveTimeout := RECIEVETIMEOUT;
IdUDPServer.DefaultPort := StrToInt(LocalPort.Text);
IdUDPServer.Active := True;
BitSend.Enabled:=True;
end;
end.