在 Delphi XE2 中,我使用 TTCPClient
组件与 RTSP 服务器通信。在反复试验未从服务器返回响应后,我将项目切换为通过端口 80(而不是 RTSP 的 554)发送 HTTP 请求,并尝试将请求发送到网站(具体为 www.google.com)。我仍然没有收到任何回复。
我在主窗体 (Form1
) 上有一个名为 Client
的 TTCPClient
组件,一个名为 TMemo
的控件Log
、一个名为 txtHost
的 TEdit
控件和一个 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
事件从未被调用,我正在连接的任何服务器都没有返回任何内容。
我在这里做错了什么?
引用资料
这些是我引用的一些链接:
- http://effbot.org/zone/socket-intro.htm
- http://www.ietf.org/rfc/rfc2326.txt
- http://folk.uio.no/meccano/reflector/smallclient.html
- http://www.samsungdforum.com/upload_files/files/guide/data/html/html_3/reference/rtsp_specification.html
我正在使用的相机是 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/