我正在實作一個非常簡單TIdCmdTCPServer的從我們也撰寫的手機應用程式接收資料的方法。單元應用程式以一個簡單的hello命令開始,但OnBeforeCommandHandler帶有垃圾。
AData等于'somerandomchars hello'
隨機字符是非 ASCII 字符。
我將 Delphi 2007 與捆綁的 Indy 10.1.5 組件一起使用。我試過這個:
- 也許我的程式造成了一些損壞,所以我做了一個非常簡單的程式,只有一個 Form 和一個
TIdCmdTCPServer. 結果相同。 - Delphi 2007 Indy 組件有問題嗎?我切換到德爾福 10.3。同樣的問題。
- 我的電腦是否以某種方式破壞了通信?我在物理和虛擬的其他 Windows 10 中運行了這個簡單的程式。遇到了同樣的問題。
- 也許手機應用程式發送了錯誤的資料。嘗試使用 Putty 直接發送命令。沒運氣。
- 不同的埠?相同的
我已經搜索過這個問題,但沒有找到一個頁面。事實上,它們都處于運行TIdCmdTCPServer平穩的狀態。
最奇怪的一點是,經過五六個命令后,資料讀取正常。在每個事件中,隨機字符都與前一個字符不同。單步執行 Indy 代碼后,我發現當它即將第一次讀取時,輸入緩沖區中已經有隨機字符。不同的“命令”需要不同數量的周期才能開始閱讀而沒有垃圾。在“你好”的情況下,八九次(并不總是相同)。
這是什么原因造成的?我剛剛啟動服務器:
IdCmdTCPServer1.Active := True;
它需要一個單獨的執行緒嗎?Indy 的幫助并沒有說明這一點。沒有從默認屬性更改任何屬性。
這是簡單應用程式的代碼:
unit test;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdCustomTCPServer, IdTCPServer,
IdCmdTCPServer, IdCommandHandlers, IdContext, IdServerIOHandler,
IdServerIOHandlerSocket, IdServerIOHandlerStack;
type
TForm1 = class(TForm)
cmdsMain: TIdCmdTCPServer;
ioshMain: TIdServerIOHandlerStack;
procedure FormShow(Sender: TObject);
procedure cmdsMainCommandHandlers0Command(ASender: TIdCommand);
procedure cmdsMainBeforeCommandHandler(ASender: TIdCmdTCPServer;
var AData: string; AContext: TIdContext);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.cmdsMainBeforeCommandHandler(ASender: TIdCmdTCPServer;
var AData: string; AContext: TIdContext);
begin
// here comes the garbagge
end;
procedure TForm1.cmdsMainCommandHandlers0Command(ASender: TIdCommand);
begin
//
end;
procedure TForm1.FormShow(Sender: TObject);
begin
cmdsMain.Active := True
end;
end.
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 299
ClientWidth = 635
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnShow = FormShow
PixelsPerInch = 96
TextHeight = 13
object cmdsMain: TIdCmdTCPServer
Bindings = <
item
IP = '0.0.0.0'
Port = 55
end>
DefaultPort = 55
IOHandler = ioshMain
CommandHandlers = <
item
CmdDelimiter = ' '
Command = 'hello'
Disconnect = False
Name = 'TIdCommandHandler0'
NormalReply.Code = '200'
ParamDelimiter = ' '
ParseParams = True
Tag = 0
OnCommand = cmdsMainCommandHandlers0Command
end>
ExceptionReply.Code = '500'
ExceptionReply.Text.Strings = (
'Unknown Internal Error')
Greeting.Code = '200'
Greeting.Text.Strings = (
'Welcome')
HelpReply.Code = '100'
HelpReply.Text.Strings = (
'Help follows')
MaxConnectionReply.Code = '300'
MaxConnectionReply.Text.Strings = (
'Too many connections. Try again later.')
ReplyTexts = <>
ReplyUnknownCommand.Code = '400'
ReplyUnknownCommand.Text.Strings = (
'Unknown Command')
OnBeforeCommandHandler = cmdsMainBeforeCommandHandler
Left = 312
Top = 152
end
object ioshMain: TIdServerIOHandlerStack
Left = 376
Top = 168
end
end
更新:我們修改了單元應用程式,只將命令發送到回顯服務器:tcpbin.com。回聲接收良好。我們使用 Putty 將單元模擬到同一臺服務器,以確保它生成任何垃圾。在兩者中,它都運行良好。
uj5u.com熱心網友回復:
您可以在多個 Indy 版本和多個服務器機器上重現該問題的事實告訴我,問題可能根本不在于服務器,而在于客戶端。例如,如果您在 telnet 模式而不是 raw 模式下使用 Putty。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/496408.html
