delphi - TCP 客户端未接收到来自 RTSP 服务器的响应

标签 delphi sockets delphi-xe2 rtsp tcp

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

我在主窗体 (Form1) 上有一个名为 ClientTTCPClient 组件,一个名为 TMemo 的控件Log、一个名为 txtHostTEdit 控件和一个 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 网站的论坛上提问。该代码确实适用于其他服务器连接。

最佳答案

这对我有用,这取决于你是否处于阻塞模式:

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.

编辑

这是一个带有 RTSP 服务器的真实示例(在本例中为 YouTube) 我使用了 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.

关于delphi - TCP 客户端未接收到来自 RTSP 服务器的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12886383/

相关文章:

delphi - 是否有任何工具/实用程序可以将 pascal 源文件中的 "string"转换为 "AnsiString"?

python - 套接字无法建立连接

c - 无法使用c中的套接字接收大型数组

arrays - 如何在没有数组变量的情况下传递值数组?

delphi - 如何检测 TWebBrowser 何时完成页面下载?

c++ - 带锁的多线程与单线程?

delphi - RemObjects SDK参数可以通过URI传递吗?

delphi - 即使 Windows 7 Flip 3D 已激活,如何使表单始终位于顶部

delphi - 组合多个 TBytes 数组的最佳方式

delphi - 自定义 Windows Vista 日历显示