mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Sep 2003
Posts: 4,230
D
DaveC Offline OP
Hoopy frood
OP Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
ok im sure its not that hard, but its elluding me as to anything that is always the same.

Im socket reading a webpage, and i want to detect when the page has finished being sent. is there any tell tail sign of the end of send?

PS: i dont want to wait for the socket to close, because i dont want it to close, i want to use it again.

Joined: Aug 2004
Posts: 7,252
R
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
In my experience the last line of any single webpage consits of </html>

In the few sockets that I do use, I use that as a check to close the socket unless there's been something earlier (ie: a match) that has closed it.

Joined: Sep 2005
Posts: 2,881
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
Couldnt you just send "Connection: close", catch the socket closing and then reopen the socket in the on sockclose event? There will be next to no loss in speed.

This seems the best way to me. RusselB's method might work but it would fail on badly made webpages.

Joined: Feb 2004
Posts: 2,019
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2004
Posts: 2,019
You can check the value of the Content-Length header, and work something out with that.

For instance, you could sockmark the connection with the size of the headers + value from the content length header, together they tell you the entire data you will receive. Once ($sock($sockname).mark == $sock($sockname).rcvd) you'll know you're at the end of the data.

For subsequent receiving of data, you'll just increment the value of the sockmark with the new content length and size of headers (which you can easily catch by incrementing the value with $sockbr in the headers phase), and then do the same comparison again with .rcvd.

Personally, I'd probably go for the on sockclose event, but if you absolutely want to keep it open...

Looking for </html> could be a workable solution, however if you're reading a text file, an XML document, or other data, this will be of no use. Although you did mention that its to read a webpage, I do think it's an important point to raise for the general case.

Even if we are dealing with HTML here, it's not certain that you will actually receive the </html> in one piece, depending on the formatting of the site. Heck, some sites don't even have a closing html tag, if I look at the source of my Gmail inbox, there's no </html>, only an opening <html> at the beginning.

However, if you know the formatting is decent, and it looks like you'll be able to catch </html> just fine, then this looks like the best way when keeping the socket open.


Gone.
Joined: Feb 2004
Posts: 2,019
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2004
Posts: 2,019
Quote:
In the few sockets that I do use, I use that as a check to close the socket unless there's been something earlier (ie: a match) that has closed it.


There's really no need to be looking for </html> to close your socket manually, just include the "Connection: close" header and the server will close the connection automatically after all data has been sent. Actually, in many cases the server already does this regardless if you specified the header or not, though you must specify it to be certain.

You'll save some processing time for not having to parse each incoming line to check for </html>.


Gone.

Link Copied to Clipboard