wizard2000
asked on
TClientSocket help
We are trying to write a program which uses a TClientSocket object to send a text string to an HTTP server and receive the response.
It seems so far that the socket is opened and data is sent to the server (apache logs confirm this). However, no data seems to be recevied and processed on the OnRead event. This does not even seem to be triggered.
Please note that this is Delphi 5.
Code below:
procedure TForm1.BitBtn1Click(Sender : TObject);
begin
Form1.ClientSocket1.Active := True;
//clientsocket1.Open;
clientsocket1.Socket.SendT ext('GET http://www.getminted.com/Hello HTTP/1.1' + #13 + 'Host: www.getminted.com' + #13 + 'Connection: close' + #13 + #13 );
clientsocket1.socket.
FWaiting := true;
Application.MessageBox('Se ndText','' , MB_OKCANCEL + MB_DEFBUTTON1) ;
while FWaiting do
Application.processmessage s;
end;
procedure TForm1.ClientSocket1Read(S ender: TObject; Socket: TCustomWinSocket);
var
Buffer : array [0..4095] of char;
BytesReceived : integer;
MemoryStream : TMemoryStream;
begin
Application.MessageBox('Cl ientSocket 1Read','', MB_OKCANCEL + MB_DEFBUTTON1) ;
while FWaiting do
begin
MemoryStream := TMemoryStream.Create;
try
// Sleep(200);
while True do
begin
BytesReceived := Socket.ReceiveBuf(Buffer,S izeOf(Buff er));
if (BytesReceived <= 0) then
Break
else
begin
edit1.text := Buffer;
MemoryStream.Write(Buffer, BytesRecei ved);
end;
end;
FWaiting := False;
MemoryStream.Position := 0;
// XMLResponse.LoadFromStream (MemoryStr eam);
finally
MemoryStream.Free;
end;
end;
end;
It seems so far that the socket is opened and data is sent to the server (apache logs confirm this). However, no data seems to be recevied and processed on the OnRead event. This does not even seem to be triggered.
Please note that this is Delphi 5.
Code below:
procedure TForm1.BitBtn1Click(Sender
begin
Form1.ClientSocket1.Active
//clientsocket1.Open;
clientsocket1.Socket.SendT
clientsocket1.socket.
FWaiting := true;
Application.MessageBox('Se
while FWaiting do
Application.processmessage
end;
procedure TForm1.ClientSocket1Read(S
var
Buffer : array [0..4095] of char;
BytesReceived : integer;
MemoryStream : TMemoryStream;
begin
Application.MessageBox('Cl
while FWaiting do
begin
MemoryStream := TMemoryStream.Create;
try
// Sleep(200);
while True do
begin
BytesReceived := Socket.ReceiveBuf(Buffer,S
if (BytesReceived <= 0) then
Break
else
begin
edit1.text := Buffer;
MemoryStream.Write(Buffer,
end;
end;
FWaiting := False;
MemoryStream.Position := 0;
// XMLResponse.LoadFromStream
finally
MemoryStream.Free;
end;
end;
end;
For what it's worth, I think I've always seen web browsers use both #13 carriage return and #10 line feed for terminators, i.e.
clientsocket1.Socket.SendT ext('GET http://www.getminted.com/Hello HTTP/1.1' + #13#10 + 'Host: www.getminted.com' + #13#10 + 'Connection: close' + #13#10#13#10 );
That might be the problem.
Rob
clientsocket1.Socket.SendT
That might be the problem.
Rob
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yep but the thing is, if you do it from a telnet prompt (on a unix machine at least) you get data back. IE just using
GET /Hello HTTP/1.0
Host: www.getminted.com
(note 2 returns to terminate the data)
This returns 'stuff' - thing is im not getting the onread event triggered which as far as im concerned it should be. The additional stuff is not needed to get a response from the server so shouldent be need to trigger the onread as far as i can see.
Why is it not triggering?!?! Driving me nuts... BTW I started using 13 10 as the term chars but it makes no difference.
And, strangest of all, apache is showing the request in the logs. As an error, but a raw socket shouldent be aware of that.
GET /Hello HTTP/1.0
Host: www.getminted.com
(note 2 returns to terminate the data)
This returns 'stuff' - thing is im not getting the onread event triggered which as far as im concerned it should be. The additional stuff is not needed to get a response from the server so shouldent be need to trigger the onread as far as i can see.
Why is it not triggering?!?! Driving me nuts... BTW I started using 13 10 as the term chars but it makes no difference.
And, strangest of all, apache is showing the request in the logs. As an error, but a raw socket shouldent be aware of that.
ASKER
Unfortunatly ethereal just crashes as soon as its started...
right... you don't send the whole www... after the GET everytime, just the URL from the root point, in this case "/Hello"
in any case... shouldn't you get a response back saying at least error or something?
in any case... shouldn't you get a response back saying at least error or something?
ASKER
Exactly! Damn event should be triggered... I have a messagebox alert first thing in the onread event but it never gets called, program just gets stuck in the application.processmessage s bit.
Normally im a unix / perl coder so this is a bit out of my experience... but do you guys concur this should be right?
Normally im a unix / perl coder so this is a bit out of my experience... but do you guys concur this should be right?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
erm, i accepted an answer days ago... why was this changed?
I recommend the following:
1) Get a copy of Ethereal (see http://www.ethereal.com ) and install it on the machine on which you're running your Delphi test application.
2) Run Ethereal, capturing your own network traffic, with the filter "port 80".
3) Use a web browser (like IE) to fetch the URL you're trying to get with your Delphi application.
4) Read the Ethereal log to see precisely how the GET is formatted by IE, then duplicate that precisely in your application.
In the process, you'll both figure out what your application is doing wrong, and you'll learn to use a very powerful diagnostic tool (one you're going to want sooner or later anyhow).
Rob---