// clientsockthread.cpp (uses Winsock calls only)
#include <stdafx.h>
#include "blocksock.h"
#include "utility.h"
#define MAXBUF 50000
CString g_strIPClient;
CString g_strProxy = "ITGPROXY";
BOOL g_bUseProxy = FALSE;
UINT ClientSocketThreadProc(LPVOID pParam)
{
// sends a blind request, followed by a request for a specific URL
CHttpBlockingSocket sClient;
char* buffer = new char[MAXBUF];
int nBytesReceived = 0;
// We're doing a blind GET, but we must provide server name if we're using a proxy.
// A blind GET is supposed to retrieve the server's default HTML document.
// Some servers don't have a default document but return a document name in the Location header.
char request[] = "GET %s%s%s HTTP/1.0/r/n";
char headers[] =
"User-Agent: Mozilla/1.22 (Windows; U; 32bit)/r/n"
"Accept: */*/r/n"
"Accept: image/gif/r/n"
"Accept: image/x-xbitmap/r/n"
"Accept: image/jpeg/r/n"
// following line tests server's ability to not send the URL
// "If-Modified-Since: Wed, 11 Sep 1996 20:23:04 GMT/r/n"
"/r/n"; // need this
CSockAddr saServer, saPeer, saTest, saClient;
try {
sClient.Create();
if(!g_strIPClient.IsEmpty()) {
// won't work if network is assigning us our IP address
// good only for intranets where client computer has several IP addresses
saClient = CSockAddr(g_strIPClient);
sClient.Bind(saClient);
}
if(g_bUseProxy) {
saServer = CBlockingSocket::GetHostByName(g_strProxy, 80);
}
else {
if(g_strServerIP.IsEmpty()) {
saServer = CBlockingSocket::GetHostByName(g_strServerName, g_nPort);
}
else {
saServer = CSockAddr(g_strServerIP, g_nPort);
}
}
sClient.Connect(saServer);
sClient.GetSockAddr(saTest);
TRACE("SOCK CLIENT: GetSockAddr = %s, %d/n", saTest.DottedDecimal(), saTest.Port());
if(g_bUseProxy) {
wsprintf(buffer, request, "http://" , (const char*) g_strServerName, g_strFile);
}
else {
wsprintf(buffer, request, "", "", g_strFile);
}
sClient.Write(buffer, strlen(buffer), 10);
sClient.Write(headers, strlen(headers), 10);
// read all the server's response headers
do {
nBytesReceived = sClient.ReadHttpHeaderLine(buffer, MAXBUF, 10);
TRACE("SOCK CLIENT: %s", buffer);
} while(strcmp(buffer, "/r/n"));
// read the server's file
nBytesReceived = sClient.ReadHttpResponse(buffer, MAXBUF, 10);
TRACE("SOCK CLIENT: bytes received = %d/n", nBytesReceived);
if(nBytesReceived == 0) {
AfxMessageBox("No response recevied. Bad URL?");
}
else {
buffer[nBytesReceived] = '/0';
::MessageBox(::GetTopWindow(::GetDesktopWindow()), buffer, "WINSOCK CLIENT", MB_OK);
}
// could do another request on sClient by calling Close, then Create, etc.
}
catch(CBlockingSocketException* e) {
LogBlockingSocketException(pParam, "CLIENT:", e);
e->Delete();
}
sClient.Close();
delete [] buffer;
return 0;
}
//***************************************
// clienturlthread.cpp (uses CInternetSession::OpenURL)
#include <stdafx.h>
#include "utility.h"
#define MAXBUF 50000
CString g_strURL = "http://";
UINT ClientUrlThreadProc(LPVOID pParam)
{
char* buffer = new char[MAXBUF];
UINT nBytesRead = 0;
CInternetSession session; // can't get status callbacks for OpenURL
CStdioFile* pFile1 = NULL; // could call ReadString to get 1 line
try {
pFile1 = session.OpenURL(g_strURL, 0, INTERNET_FLAG_TRANSFER_BINARY |
INTERNET_FLAG_KEEP_CONNECTION);
// if OpenURL fails, we won't get past here
nBytesRead = pFile1->Read(buffer, MAXBUF - 1);
buffer[nBytesRead] = '/0'; // necessary for message box
char temp[100];
if(pFile1->Read(temp, 100) != 0) { // makes caching work if read complete
AfxMessageBox("File overran buffer -- not cached");
}
::MessageBox(::GetTopWindow(::GetDesktopWindow()), buffer, "URL CLIENT", MB_OK);
}
catch(CInternetException* e) {
LogInternetException(pParam, e);
e->Delete();
}
if(pFile1) delete pFile1;
delete [] buffer;
return 0;
}