gpt4 book ai didi

delphi - 如何在Delphi中向HLDS服务器发送UDP数据包?

转载 作者:行者123 更新时间:2023-12-01 22:39:11 26 4
gpt4 key购买 nike

我正在尝试将 A2A_PING UDP 数据包发送到 HLDS 服务器,如 here 中所述。使用德尔福XE4。但是,我没有得到任何回应。当我使用 Packet Sender 进行测试时,我在数据包发送器中得到了预期的响应。

我想做的是创建一个服务器监视器以在其 UDP 端口上发送 ping。因此,我的应用程序将始终使用其本地 IP 在服务器上本地运行。服务器的UDP端口是27015。

我尝试发送的 UDP 数据包是:

Hex: FF FF FF FF 69

String equivalent: ÿÿÿÿi

这是我迄今为止尝试过的:

unit uFrmMain;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ShellAPI, IdUDPServer, IdBaseComponent, IdComponent,
IdUDPBase, IdUDPClient, IdSocketHandle, IdGlobal;

type
TfrmMain = class(TForm)
ListBox1: TListBox;
btnSendMessage: TButton;
btnInitialize: TButton;
procedure btnSendMessageClick(Sender: TObject);
procedure IdUDPServer1UDPRead(AThread: TIdUDPListenerThread; const AData: TIdBytes; ABinding: TIdSocketHandle);
procedure btnInitializeClick(Sender: TObject);
private
function String2Hex(const Buffer: Ansistring): string;
procedure Initialize;
public
{ Public declarations }
end;

const
Host = '172.30.0.96';
TargetPort = 27015;
LocalListenPort = 47001;
MessageToSend = 'ÿÿÿÿi'; // need to send: FF FF FF FF 69

var
frmMain: TfrmMain;
udpServer: TIdUDPServer;
udpClient: TIdUDPClient;

implementation

{$R *.dfm}

procedure TfrmMain.Initialize;
var
binding: TIdSocketHandle;
begin
// Setup UDP Server
udpServer := TIdUDPServer.Create(frmMain);
udpServer.Active := false;
binding := udpServer.Bindings.Add;
Binding.IP := Host; // local host ip
binding.Port := LocalListenPort; // Listen for incoming messages on 47001
udpServer.OnUDPRead := IdUDPServer1UDPRead;
udpServer.Active := true;

// Setup UDP client
udpClient := TIdUDPClient.Create(frmMain);
udpClient.Host := Host; // Local host ip
udpClient.Port := TargetPort; // Send messages to 27015
udpClient.Active := true;
end;

procedure TfrmMain.btnInitializeClick(Sender: TObject);
begin
Initialize;
end;

procedure TfrmMain.btnSendMessageClick(Sender: TObject);
begin
udpClient.SendBuffer(Host, TargetPort, ToBytes(MessageToSend));
end;

procedure TfrmMain.IdUDPServer1UDPRead(AThread: TIdUDPListenerThread; const AData: TIdBytes; ABinding: TIdSocketHandle);
begin
ListBox1.Items.Add(BytesToString(AData));
end;

function TfrmMain.String2Hex(const Buffer: Ansistring): string;
begin
SetLength(result, 2*Length(Buffer));
BinToHex(@Buffer[1], PWideChar(@result[1]), Length(Buffer));
end;

end.

更新根据雷米的建议,我也尝试过:

unit uFrmMain;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ShellAPI, IdUDPServer, IdBaseComponent, IdComponent,
IdUDPBase, IdUDPClient, IdSocketHandle, IdGlobal;

type
TfrmMain = class(TForm)
ListBox1: TListBox;
btnSendMessage: TButton;
btnInitialize: TButton;
procedure btnSendMessageClick(Sender: TObject);
procedure IdUDPServer1UDPRead(AThread: TIdUDPListenerThread; const AData: TIdBytes; ABinding: TIdSocketHandle);
procedure btnInitializeClick(Sender: TObject);
private
function String2Hex(const Buffer: Ansistring): string;
procedure Initialize;
public
{ Public declarations }
end;

const
// Host = '172.30.0.96';
Host = '192.168.190.1';
TargetPort = 27015;
LocalListenPort = 47001;

var
frmMain: TfrmMain;
udpServer: TIdUDPServer;
// udpClient: TIdUDPClient;
binding: TIdSocketHandle;

implementation

{$R *.dfm}

procedure TfrmMain.Initialize;
var
bytes_received: integer;
begin
// Setup UDP Server
udpServer := TIdUDPServer.Create(frmMain);
udpServer.Active := false;
udpServer.DefaultPort := 0;
binding := udpServer.Bindings.Add;
Binding.IP := Host; // local host ip
binding.Port := LocalListenPort; // Listen for incoming messages on 47001
udpServer.OnUDPRead := IdUDPServer1UDPRead;
udpServer.Active := true;

// Setup UDP client
// udpClient := TIdUDPClient.Create(frmMain);
// udpClient.Host := Host; // Local host ip
// udpClient.Port := TargetPort; // Send messages to 27015
// udpClient.BoundIP := Host;
// udpClient.BoundPort := LocalListenPort;
// udpClient.ReceiveTimeout := 2000;
// udpClient.Active := true;
end;

procedure TfrmMain.btnInitializeClick(Sender: TObject);
begin
Initialize;
end;

procedure TfrmMain.btnSendMessageClick(Sender: TObject);
var
MessageToSend: TIdBytes;
begin
SetLength(MessageToSend, 5);
MessageToSend[0] := $FF;
MessageToSend[1] := $FF;
MessageToSend[2] := $FF;
MessageToSend[3] := $FF;
MessageToSend[4] := $69;
// udpClient.SendBuffer(Host, TargetPort, RawToBytes(MessageToSend, SizeOf(MessageToSend)));
binding.SendTo(Host, TargetPort, RawToBytes(MessageToSend, SizeOf(MessageToSend)));
end;

procedure TfrmMain.IdUDPServer1UDPRead(AThread: TIdUDPListenerThread; const AData: TIdBytes; ABinding: TIdSocketHandle);
begin
ListBox1.Items.Add(ToHex(AData));
end;

end.

我一定错过了一些非常明显的东西,因为数据包发送器工作得很好。任何帮助将非常感激。

最佳答案

您不应在消息中使用字符串(尤其是如果您使用 Delphi 2009+,则应使用 Unicode 字符串)。您使用的协议(protocol)本质上是二进制的,而不是文本的。您需要使用原始字节进行操作,例如:

const
MessageToSend: array[0..4] of Byte = ($FF, $FF, $FF, $FF, $69);

procedure TfrmMain.btnSendMessageClick(Sender: TObject);
begin
udpClient.SendBuffer(Host, TargetPort, RawToBytes(MessageToSend, SizeOf(MessageToSend));
end;

{
Alternatively:

procedure TfrmMain.btnSendMessageClick(Sender: TObject);
var
MessageToSend: TIdBytes;
begin
SetLength(MessageToSend, 5);
MessageToSend[0] := $FF;
MessageToSend[1] := $FF;
MessageToSend[2] := $FF;
MessageToSend[3] := $FF;
MessageToSend[4] := $69;
udpClient.SendBuffer(Host, TargetPort, MessageToSend);
end;
}

procedure TfrmMain.IdUDPServer1UDPRead(AThread: TIdUDPListenerThread; const AData: TIdBytes; ABinding: TIdSocketHandle);
begin
ListBox1.Items.Add(ToHex(AData));
end;

关于delphi - 如何在Delphi中向HLDS服务器发送UDP数据包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47159299/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com