In Delphi XE2, I'm using the TTCPClient component to communicate with an RTSP server. After trial and error not getting a response back from the server, I switched the project to send HTTP requests via port 80 (instead of 554 for RTSP) and tried to send a request to a website (www.google.com specifically). I'm still not getting any response.
I have a TTCPClient component on the main form (Form1) called Client, a TMemo control called Log, a TEdit control called txtHost, and a TBitBtn control. Here's the relevant parts of the code:
Connecting to Server
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
if Client.Active then Client.Disconnect;
Client.RemoteHost:= txtHost.Text;
Client.RemotePort:= '80'; // '554';
Client.Connect;
end;
OnConnect Event Handler (HTTP)
procedure TForm1.ClientConnect(Sender: TObject);
var
S: String;
begin
Client.Sendln('GET / HTTP/1.0');
Client.SendLn('');
end;
OnConnect Event Handler (RTSP)
procedure TForm1.ClientConnect(Sender: TObject);
var
S: String;
begin
Client.SendLn('OPTIONS * RTSP/1.0');
Client.SendLn('CSeq:0');
Client.SendLn('');
end;
OnReceive Event Handler
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 Event Handler
procedure TForm1.ClientError(Sender: TObject; SocketError: Integer);
begin
Log.Lines.Append('> ERROR '+IntToStr(SocketError));
end;
The OnReceive event is never called, nothing is coming back from any Server I'm connecting to.
What am I doing wrong here?
References
These are some links which I'm referencing to:
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
The camera I'm working with is Grandstream GXV3601LL
UPDATE
I've concluded that the issue is with the RTSP server, and have asked a question on the forums on Grandstream's website. The code does work with other server connections.
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.
The reason the OnReceive event is not called is because TTCPClient is NOT an asynchronous component, like you are trying to treat it. The OnReceive event DOES NOT work the same way as the old TClientSocket.OnRead event. The OnReceive event is called inside of the ReceiveBuf() method only (ReceiveLn() calls ReceiveBuf() internally). The data that is passed to the OnReceive event is the same data that the ReceiveBuf() method returns on output. You have a catch-22 situation - you are waiting for the OnReceive event before calling ReceiveLn(), but OnReceive will not be triggered until you call ReceiveLn() first. If you want to use TTCPClient asynchronously, you will have to call its ReceiveLn() method periodically, either in a timer or worker thread, NOT inside the OnReceive event.
The TTCPClient component is part of the old CLX framework for Kylix. It is not part of the VCL, or even FireMonkey, and should not be used anymore. Either use the old TClientSocket component (which is deprecated but still available), or change to another component library, such as Indy.
Related
provide an example in DELPHI 5 if is possible
lets say we have the follow code.A simple tclientsocket communicates with a tserver socket.
Everything works fine if requests from tclientsocket are coming after the process of data on event tserversocket1onReadClient.
But How i will bypass the problem when on the middle of process i am getting a new Request from socketclient1 and i havent finish yet my process?
do i have to Implement it in ServerType: stThreadBlocking
Do i have to Create a thread to Do the process?
is it a better way do this simple.
My tclientsocket string messages will not be larger than 255 chars.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ScktComp;
type
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
Button1: TButton;
ServerSocket1: TServerSocket;
procedure Button1Click(Sender: TObject);
procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure ServerSocket1ClientError(Sender: TObject;Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;var ErrorCode: Integer);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
with Serversocket1 do
begin
Active:=false;
ServerType:=stNonBlocking;
port:=5052;
Active:=true;
end;
with ClientSocket1 do
begin
active:=false;
port:=5052;
Address:='127.0.0.1';
host:='127.0.0.1';
active:=true;
end;
end;
procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
errorcode:=0;
end;
procedure TForm1.ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
errorcode:=0;
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;Socket: TCustomWinSocket);
var mydata:String;
begin
mydata:=socket.ReceiveText;
///Proceccing my data now
//Line 1
//Line 2
//Line 3
//Line 4 <---- ie. when i am proccessing line 4 a new Request from clientsocket1 arrives
//Line 5
end;
end.
The scenario you describe cannot happen. When the ServerSocket1ClientRead event handler is executing, it will not be called in a re-entrant fashion. Well, unless you were to call Application.ProcessMessages. So, don't do that!
I am having small issue. After I make a connection between the server and client. I would close the client, and then the server, but the server hangs and sends me a "program has crashed" I think the problem I am having is that the server doesn't recognize a client has disconnected, and still thinks the client is active. Here is the source code:
client:
unit client;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, Winsock, IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient;
type
TForm1 = class(TForm)
IdTCPClient1: TIdTCPClient;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function GetIpFromDns(HostName: string): string;
type
tAddr = array[0..100] of PInAddr;
pAddr = ^tAddr;
var
I: Integer;
WSA: TWSAData;
PHE: PHostEnt;
P: pAddr;
begin
Result := HostName;
WSAStartUp($101, WSA);
try
PHE := GetHostByName(pChar(HostName));
if (PHE <> nil) then
begin
P := pAddr(PHE^.h_addr_list);
I := 0;
while (P^[i] <> nil) do
begin
Result := (inet_nToa(P^[i]^));
Inc(I);
end;
end;
except
end;
WSACleanUp;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
IdTCPClient1.Host := GetIpFromDns('example.no-ip.org');
IdTCPClient1.Port := 9000;
IdTCPClient1.Connect;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if IdTCPclient1.Connected = True then
IdTCPClient1.Disconnect
else
end;
end.
Server:
unit server;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, IdBaseComponent, IdComponent, IdTCPServer;
type
TForm1 = class(TForm)
IdTCPServer1: TIdTCPServer;
procedure IdTCPServer1Connect(AThread: TIdPeerThread);
procedure IdTCPServer1Disconnect(AThread: TIdPeerThread);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
showmessage('client connected');
end;
procedure TForm1.IdTCPServer1Disconnect(AThread: TIdPeerThread);
begin
showmessage('client disconnected');
end;
end.
it may not look like I set up a listening port for indy, but I did in the object inspector page. For some reason if I put IdTCPServer.DefaultPort and Active in the form create it throws more errors.
I also tried IdTCPClient1.DisconnectSocket but no luck there either.
Do I need to create something on the server side to check the connections periodically? if so, what would be best way to do that?
I am running TEmbeddedwb and I got a javascript timeout error while navigating on that TEmbeddedwb .
(I do not have this error while running in my internet explorer !)
The browser asks me if I want to stop the execution of the script.
I put the TEmbeddedwb propertioes to
silent = true
dialogoBox.disableAll = true
But I still have this popup comming out !
1) why do I have this error (tested on 2pcs) while there is no error while navigating on Internet explorer
2) how to disable / hide this popup ?
regards
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleCtrls, SHDocVw_EWB, EwbCore, EmbeddedWB;
type
TForm1 = class(TForm)
iemain: TEmbeddedWB;
procedure iemainScriptError(Sender: TObject; ErrorLine, ErrorCharacter,
ErrorCode, ErrorMessage, ErrorUrl: String;
var ScriptErrorAction: TScriptErrorAction);
procedure FormCreate(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.iemainScriptError(Sender: TObject; ErrorLine,
ErrorCharacter, ErrorCode, ErrorMessage, ErrorUrl: String;
var ScriptErrorAction: TScriptErrorAction);
begin
MessageDlg('hello', mtWarning, [mbOK], 0);
if ErrorCode='123' then ScriptErrorAction := eaContinue;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
iemain.Navigate('http://www.expedia.fr/Hotels');
end;
end.
How to handle JavaScript error in TEmbeddedWB ?
Write a handler for the OnScriptError event and return one of the available TScriptErrorAction values in the ScriptErrorAction output parameter. To ignore the script error and continue use e.g.:
procedure TForm1.EmbeddedWB1ScriptError(Sender: TObject; ErrorLine,
ErrorCharacter, ErrorCode, ErrorMessage, ErrorUrl: string;
var ScriptErrorAction: TScriptErrorAction);
begin
if ErrorCode = 123 then
ScriptErrorAction := eaContinue;
end;
I have a project which does financial reports and I want to let user to be able to get this reports through the internet
I tried using TIdHTTPServer which is an Indy component to make my application to work as an HTTP Server and to let it to be able
receive request -> process the request -> send back the result of the request process
using a special port.
now my problem is that I'm getting a lot of Access Violation errors and random exceptions
it looks like about threads problem or I don't know because if I process the same request without using the TIdHTTPServer I don't get any problem
i'm using the OnCommandGet Event to process the request and send the result back to user inside the context stream.
what I need is a demonstration on how to use it with TADODataSet and TADOConnection
for example I need the user to be able to send a request and the TIdHTTPServer takes the request (for example call a stored procedure using to ADODataSet and take the result as XML file and send it back to the user)
please help....thank you.
one possibility how a Server could work ...
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,IDContext, IdBaseComponent, IdComponent, IdCustomTCPServer, IdTCPServer, StdCtrls, DB, ADODB;
type
TForm3 = class(TForm)
IdTCPServer1: TIdTCPServer;
Memo1: TMemo;
Button1: TButton;
DummyConnection: TADOConnection;
procedure Button1Click(Sender: TObject);
procedure IdTCPServer1Execute(AContext: TIdContext);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form3: TForm3;
implementation
uses ComObj,AdoInt,ActiveX;
{$R *.dfm}
function SendStream(AContext: TIdContext; AStream: TStream): Boolean;
begin
Result := False;
try
AContext.Connection.IOHandler.Write(AStream.Size); // sending length of Stream first
AContext.Connection.IOHandler.WriteBufferOpen;
AContext.Connection.IOHandler.Write(AStream, AStream.Size);
AContext.Connection.IOHandler.WriteBufferFlush;
finally
AContext.Connection.IOHandler.WriteBufferClose;
end;
Result := True;
end;
procedure TForm3.Button1Click(Sender: TObject);
begin
IdTCPServer1.Active := true;
end;
{ Clientside function
Function RecordsetFromXMLStream(Stream:TStream): _Recordset;
var
RS: Variant;
begin
RS := CreateOleObject('ADODB.Recordset');
RS.Open(TStreamAdapter.Create(Stream) as IUnknown);
Result := IUnknown(RS) as _Recordset;
end;
}
Procedure RecordsetToXMLStream(const Recordset: _Recordset;Stream:TStream);
var
RS: Variant;
begin
if Recordset = nil then Exit;
RS := CreateOleObject('ADODB.Recordset');
RS := Recordset;
RS.Save(TStreamAdapter.Create(stream) as IUnknown, adPersistXML);
Stream.Position := 0;
end;
Procedure GetQueryStream(Const s,ConStr:String;ms:TMemoryStream);
var
AC:TAdoConnection;
ads:TAdodataset;
begin
AC:=TAdoConnection.Create(nil);
try
ads:=TAdodataset.Create(nil);
try
ads.Connection := AC;
AC.ConnectionString := ConStr;
ads.CommandText := s;
ads.Open;
RecordsetToXMLStream(ads.Recordset,ms);
finally
ads.Free
end;
finally
AC.Free
end;
end;
procedure TForm3.IdTCPServer1Execute(AContext: TIdContext);
var
cmd:String;
ms:TMemoryStream;
begin
CoInitialize(nil);
AContext.Connection.IOHandler.Readln(cmd);
ms:=TMemoryStream.Create;
try
GetQueryStream('Select * from Adressen',DummyConnection.ConnectionString,ms);
ms.Position := 0;
SendStream(AContext,ms);
AContext.Connection.Socket.CloseGracefully;
finally
ms.Free;
CoUninitialize;
end;
end;
end.
Trying to learn how to make server-client apps and stuff like. I trying to draw circles(on mouse click) in all clients so this is how i trying to do that. But it's not working - no errors but form is empty. What i need to fix?
Client code
unit Client;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, {Figure, Ball,} IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient, ScktComp;
type
TForm1 = class(TForm)
Timer1: TTimer;
Button1: TButton;
ClientSocket: TClientSocket;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
public
{ Public declarations }
end;
var
Form1: TForm1;
f:boolean;
p:MyPoint;
s:MyPoint;
z:TCanvas;
obj: MyFigure;
pX, pY:Integer;
myBuf: array[1..32] of Integer;
dataBuf: array[1..32] of Integer;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Timer1.Enabled:=false;
Timer1.Interval:=5;
z:=Form1.Canvas;//TCanvas.Create;
Button1.Caption:='Пуск';
f:=false;
ClientSocket.Port:=1234;
ClientSocket.Active:= False;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if not f then
begin
Timer1.Enabled:=true;
Button1.Caption:='Стоп';
f:=not f;
end
else
begin
Timer1.Enabled:=false;
Button1.Caption:='Пуск';
ClientSocket.Active:= True;
f:=not f;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
//z.Lock;
//z.Brush.Color:=ClWhite;
//z.FillRect(Canvas.ClipRect);
//obj.Draw(z);
if ClientSocket.Active then
ClientSocket.Socket.ReceiveBuf(dataBuf, 32);
z.Brush.Color:=ClRed;
z.Ellipse(dataBuf[1] + 10, dataBuf[2] + 10,dataBuf[1] - 10, dataBuf[2] - 10);
//z.Unlock;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ClientSocket.Active := false;
end;
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
myBuf[1]:=X;
myBuf[2]:=Y;
if ClientSocket.Active then
ClientSocket.Socket.SendBuf(myBuf, 32);
end;
end.
Server
unit ServerProject;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp;
type
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientWrite(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
sBufer : array [1..32] of Integer;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
ServerSocket1.Port:=1234;
ServerSocket1.Active := True;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ServerSocket1.Active := false;
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
i:integer;
begin
for i := 0 to ServerSocket1.Socket.ActiveConnections-1 do
begin
with ServerSocket1.Socket.Connections[i] do
begin
ReceiveBuf(sBufer, 32);
end;
end;
end;
procedure TForm1.ServerSocket1ClientWrite(Sender: TObject;
Socket: TCustomWinSocket);
var
i:integer;
begin
for i := 0 to ServerSocket1.Socket.ActiveConnections-1 do
begin
with ServerSocket1.Socket.Connections[i] do
begin
SendBuf(sBufer, 32);
end;
end;
end;
end.
Your painting code is in the wrong place and is painting to the wrong thing. In Windows programs you are meant to paint in response to a WM_PAINT message. You are not doing so. What's more, you have to paint on a device context that is provided by a call to BeginPaint.
The VCL wraps all those details up for you, but you still need to follow the rules. In your case I recommend that you add a TPaintBox component to your form. Then implement an OnPaint event handler for the paint box. Finally, whenever you wish to repaint the paint box, for example on a timer, call the Invalidate method of the paint box.
I suspect that you want your each new ellipse to be drawn in addition to the earlier drawn ellipses. In which case you are probably best served by drawing them to an off-screen bitmap first and then, when you come to paint to the paint box, draw that bitmap on the paint box. The point is that a window needs to be able to re-paint itself in its entirety. When you paint to a screen device, what you painted is lost the next time that window needs to be painted. So it's the responsibility of the application to be able to paint its entire self at any point, if it is asked.
More generally I urge you to stop using global variables. They will cause you no end of trouble. Prefer local variables wherever possible. If you need state to persist between different method calls, use member variables. The guiding principle is to use the narrowest scope possible.
Your current design uses a timer to poll for new data. That's a very poor approach. The most efficient and effective approach is to use synchronous blocking communication. Indy takes that approach. Windows sockets components instead tend to be used in an asynchronous mode. Irrespective of the relative merits of these two approaches, you should not be polling on a timer. If you do use asynchronous communication, then respond to new data by handling an event rather than polling.
Your program is currently trying to mix together GDI painting, and network communication. I suggest that you attempt to get on top of these concepts one at a time. Learn how to paint without the distraction of communication. Then when you have cracked painting, try to bring in the communication aspect.