6

在 Delphi XE2 中,我使用该TTCPClient组件与 RTSP 服务器进行通信。经过反复试验,没有从服务器得到响应,我将项目切换为通过端口 80(而不是 RTSP 的 554)发送 HTTP 请求,并尝试向网站(特别是 www.google.com)发送请求。我仍然没有得到任何回应。

TTCPClient在主窗体 ( Form1) 上有一个名为的组件Client、一个名为 的TMemo控件Log、一个TEdit名为的控件txtHost和一个TBitBtn控件。以下是代码的相关部分:

连接到服务器

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  if Client.Active then Client.Disconnect;
  Client.RemoteHost:= txtHost.Text;
  Client.RemotePort:= '80'; // '554';
  Client.Connect;
end;

OnConnect 事件处理程序 (HTTP)

procedure TForm1.ClientConnect(Sender: TObject);
var
  S: String;
begin
  Client.Sendln('GET / HTTP/1.0');
  Client.SendLn('');
end;

OnConnect 事件处理程序 (RTSP)

procedure TForm1.ClientConnect(Sender: TObject);
var
  S: String;
begin
  Client.SendLn('OPTIONS * RTSP/1.0');
  Client.SendLn('CSeq:0');
  Client.SendLn('');
end;

OnReceive 事件处理程序

procedure TForm1.ClientReceive(Sender: TObject; Buf: PAnsiChar;
  var DataLen: Integer);
var
  S, R: String;
begin
  S:= Client.Receiveln;
  while S <> '' do begin
    R:= R+ S;
    S:= Client.Receiveln;
  end;
  Log.Lines.Append('> RECEIVED ' + R);
end;

OnError 事件处理程序

procedure TForm1.ClientError(Sender: TObject; SocketError: Integer);
begin
  Log.Lines.Append('> ERROR '+IntToStr(SocketError));
end;

OnReceive事件永远不会被调用,我连接的任何服务器都没有返回任何内容。

我在这里做错了什么?

参考

这些是我引用的一些链接:

我正在使用的相机是Grandstream GXV3601LL

更新

我得出的结论是问题出在 RTSP 服务器上,并在 Grandstream 网站的论坛上提出了问题。该代码确实适用于其他服务器连接。

4

2 回答 2

6

This works for me, it depends if you are in blocking mode or not:

unit Unit11;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Sockets, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, StdCtrls;

type
  TForm1 = class(TForm)
    IdTCPClient1: TIdTCPClient;
    TcpClient1: TTcpClient;
    Memo1: TMemo;
    procedure TcpClient1Connect(Sender: TObject);
    procedure TcpClient1Receive(Sender: TObject; Buf: PAnsiChar; var DataLen: Integer);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
 TcpClient1.BlockMode := bmBlocking;
 TcpClient1.RemoteHost := 'www.google.com';
 TcpClient1.RemotePort := '80';
 TcpClient1.Connect;
end;

procedure TForm1.TcpClient1Connect(Sender: TObject);

var s : string;

begin
 memo1.Lines.Add('connected');
 TcpClient1.Sendln('GET /');
 s := TcpClient1.Receiveln;
 memo1.Lines.Add(S);
end;

end.

EDIT

here is a real world example with a RTSP server (youtube in this case) I used Indy IdTcpClient

unit Unit11;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Sockets, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Client: TIdTCPClient;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);

var s : string;

begin
 Client.Host := 'v5.cache6.c.youtube.com';
 Client.Port := 554;
 Client.Connect;
 Client.IOHandler.Writeln('OPTIONS * RTSP/1.0');
 Client.IOHandler.Writeln('CSeq: 1');
 Client.IOHandler.Writeln('');

 s := Client.IOHandler.ReadLn;
 Memo1.Lines.Add(s);
 s := Client.IOHandler.ReadLn;
 Memo1.Lines.Add(s);
end;

end.
于 2012-10-14T21:06:20.400 回答
2

OnReceive未调用事件的原因TTCPClient是它不是异步组件,就像您试图处理它一样。该OnReceive事件的工作方式与旧事件TClientSocket.OnRead不同。该OnReceive事件ReceiveBuf()仅在方法内部调用(内部ReceiveLn()调用ReceiveBuf())。传递给OnReceive事件的数据与ReceiveBuf()方法在输出时返回的数据相同。您有一个 catch-22 情况 - 您在调用之前正在等待OnReceive事件ReceiveLn(),但OnReceive在您先调用之前不会被触发ReceiveLn()。如果要TTCPClient异步使用,则必须调用它ReceiveLn()定期方法,无论是在计时器还是工作线程中,而不是OnReceive事件内部。

TTCPClient组件是 Kylix 旧 CLX 框架的一部分。它不是 VCL 的一部分,甚至不是 FireMonkey,不应再使用。要么使用旧TClientSocket组件(已弃用但仍然可用),要么更改为另一个组件库,例如 Indy。

于 2012-10-15T21:28:48.803 回答