• ### Popular Now

• 11
• 9
• 12
• 9
• 11

This topic is 3571 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hello, im the author of Jolt3D! engine (jolt-3d.sf.net) im trying to create an alternative way to 'InternetReadFile()' func of WININET.DLL, to read HTML pages from C programs. My goal anyway is to get rid of the above dll & just use the wsock dll, but i hit a problem...The problem is that with the following winsock code im not getting correct results, or i dont know well the "GET" syntax. The (easy) code is:
	// address
LPHOSTENT	lpHostEntry;
lpHostEntry = gethostbyname(Servername);
else // It was a valid IP address string
if (lpHostEntry == NULL)
{
J_event("error A");
return;
}
// socket
SOCKET	Socket;
Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (Socket == INVALID_SOCKET)
{
J_event("error B");
return;
}
// port
LPSERVENT lpServEnt;
lpServEnt = getservbyname("http", "tcp");
if (lpServEnt == NULL)
saServer.sin_port = htons(80);
else
saServer.sin_port = lpServEnt->s_port;
// fill rest
saServer.sin_family = AF_INET;
// connect
if (nRet == SOCKET_ERROR)
{
J_event("error C");
return;
}
// build the HTTP request
char szBuffer[1024];
sprintf(szBuffer, "GET %s\n", Filename);
nRet = send(Socket, szBuffer, strlen(szBuffer), 0);
if (nRet == SOCKET_ERROR)
{
J_event("error D");
closesocket(Socket);
return;
}
// receive the file contents and print to local 'index.html'
FILE *f=fopen("index.html","wb");
while(1)
{
nRet = recv(Socket, szBuffer, sizeof(szBuffer), 0);
if (nRet == SOCKET_ERROR)
{
J_event("error E");
break;
}
else if (nRet == 0) // server closes connection ? (or just there'arent bytes to read)
break;
fwrite(szBuffer, nRet, 1, f); // write to file
}
closesocket(Socket);
fclose(f);


##### Share on other sites
You can try the HTTP-GET library, that does exactly that.

##### Share on other sites
nice library,
although it doesnt handle re-direction &
other things, is far better than my code :)
thanx

##### Share on other sites
Yes, it's somewhat minimal :-)

However, it should be not too hard to put re-direct parsing, cookies, and whatever else you need on top of what's there. The networking and request/response part works fairly well.

##### Share on other sites
...i suppose that is a library of yours (i saw the ~hplus directory :) )
really nice work ! Just one more question: is there a way to bypass
the header-like text before the actual html page ?
Im using a number of html pages from my c programs in real-time & doing parsing byte-2-byte, so i know (and need) the same byte-offsets for several of these pages; but with the header things (and offsets :) ) are changing...
I must start thinking where the <HTML> starts or is there an easier way ?
my thanx

##### Share on other sites
Quote:
 Original post by vlzvl...i suppose that is a library of yours (i saw the ~hplus directory :) )really nice work ! Just one more question: is there a way to bypassthe header-like text before the actual html page ?Im using a number of html pages from my c programs in real-time & doing parsing byte-2-byte, so i know (and need) the same byte-offsets for several of these pages; but with the header things (and offsets :) ) are changing...I must start thinking where the starts or is there an easier way ?my thanx

HTTP headers are fixed. You can send minimal subset, but it needs to conform to specification.

HTTP supports partial GET requests. They need to be supported by server. Some do not support it, and some deliberately disable it.

##### Share on other sites
The headers end after the character sequence "\r\n\r\n" (CR, LF, CR, LF). That character sequence cannot be part of the header. Thus, you can just look for that sequence, and when you find it, you know that the data starts with the very next byte. That may or may not be "<HTML>" by the way -- it could be "<html>," or "<?xml>," or "<!DOCTYPE>," or "," or some extra blanks inserted by whomever generated the page.