急!有关Indy UDP 的问题:为什么别人发给我的我收得到,我发给别人的别人收不到?

pfriend21 2003-08-30 10:19:54
我使用Delphi6的Demo,修改了IdUDPClient中的IP地址后他发给我的我能收到,我发给他的却不能收到,不知道为什么啊
IP从QQ上获取:我的218.85.251.75:4001
他的:61.183.235.161:10928
是不是因为他的端口太奇怪了?

******Client*******

object UDPAntiFreeze: TIdAntiFreeze
OnlyWhenIdle = False
Left = 268
Top = 16
end
object UDPClient: TIdUDPClient
Host = '218.6.48.133'
Port = 8090
ReceiveTimeout = 0
Left = 240
Top = 16
end
{***************************************************************
*
* Project : UDPClient
* Unit Name: UDPClientMain
* Purpose : UDP demo - client project
* Version : 1.0
* Date : Wed 25 Apr 2001 - 01:44:24
* Author : <unknown>
* History :
* Tested : Wed 25 Apr 2001 // Allen O'Neill <allen_oneill@hotmail.com>
*
****************************************************************}

unit UDPClientMain;

interface

uses
{$IFDEF Linux}
QGraphics, QControls, QForms, QDialogs, QStdCtrls,
{$ELSE}
windows, messages, graphics, controls, forms, dialogs, Winsock, 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.







object UDPServer: TIdUDPServer
Bindings = <>
DefaultPort = 8090
OnUDPRead = UDPServerUDPRead
Left = 240
Top = 16
end
object UDPAntiFreeze: TIdAntiFreeze
OnlyWhenIdle = False
Left = 268
Top = 16
end
******Server*******
{***************************************************************
*
* Project : UDPServer
* Unit Name: UDPServerMain
* Purpose : UDP demo - server project
* Version : 1.0
* Date : Wed 25 Apr 2001 - 01:43:04
* Author : <unknown>
* History :
* Tested : Wed 25 Apr 2001 // Allen O'Neill <allen_oneill@hotmail.com>
*
****************************************************************}

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.
...全文
80 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
pfriend21 2003-08-31
  • 打赏
  • 举报
回复
我换了个人试,换了个IP,他是拨号,端口用4000
一样不行
我发给他的他收不到,他发给我的我却收得到
sunhuiNO1 2003-08-30
  • 打赏
  • 举报
回复
对方在局域网内,需要他先给你发,然后你在给他发才有可能接受成功和发送成功
不过我建议你用TIdUDPServer来做,
mme 2003-08-30
  • 打赏
  • 举报
回复
哈哈哈哈,一看就知道他的端口是有问题的,他在局域网内部嘛!

所以要采取点技巧才可以实现跟他通讯的.用tcp就没那么麻烦了.

不过不管什么方法都得他先连接你才可以.
dext 2003-08-30
  • 打赏
  • 举报
回复
XP的防火墙也会导致此问题!

1,594

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 网络通信/分布式开发
社区管理员
  • 网络通信/分布式开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧