Thanks for the replies.
The code has worked for multiple different webpages, but seems to have a problem with pages on cgi.ebay.co.uk
I have never used a network analyser before, I will take a look at Ethereal now.
Here's the code I have been using, which gets the header of a page using a HEAD request. (I have been using GET to get the page). If anyone can spot anything causing the problem that would be great.
(I have copied the code exactly from a working program so it should compile fine).
libws2_32.a is linked to the project.
Code:
#include <winsock.h>
#include <iostream>
int GetHTTP(std::string lpServerName, std::string lpFileName, std::string* file);
int main()
{
std::string page_string;
//int GetHTTP_return = GetHTTP("www.research.att.com", "/~bs/homepage.html" , &page_string);
int GetHTTP_return = GetHTTP("cgi.ebay.co.uk", "/windows-folder_W0QQitemZ280062863525QQihZ01"
"8QQcategoryZ80555QQrdZ1QQcmdZViewItem" , &page_string);
if (GetHTTP_return == 0)
{
std::cout << "Header received. \n\n" << page_string << std::endl;
}
else std::cout << "ERROR" << std::endl;
system ("PAUSE");
return 0;
}
int GetHTTP(std::string lpServerName, std::string lpFileName, std::string* file)
{
WORD wVersionRequested = MAKEWORD(1,1);
WSADATA wsaData;
int nRet;
nRet = WSAStartup(wVersionRequested, &wsaData);
if (nRet != 0)
{
WSACleanup();
return 1;
}
if (wsaData.wVersion != wVersionRequested)
{
WSACleanup();
return 2;
}
IN_ADDR iaHost;
LPHOSTENT lpHostEntry;
iaHost.s_addr = inet_addr(lpServerName.c_str());
if (iaHost.s_addr == INADDR_NONE)
{
lpHostEntry = gethostbyname(lpServerName.c_str());
}
else
{
lpHostEntry = gethostbyaddr((const char *)&iaHost,
sizeof(struct in_addr), AF_INET);
}
if (lpHostEntry == NULL)
{
return WSAGetLastError();
}
SOCKET Socket;
Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (Socket == INVALID_SOCKET)
{
return WSAGetLastError();
}
LPSERVENT lpServEnt;
SOCKADDR_IN saServer;
lpServEnt = getservbyname("http", "tcp");
if (lpServEnt == NULL)
saServer.sin_port = htons(80);
else
saServer.sin_port = lpServEnt->s_port;
saServer.sin_family = AF_INET;
saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
nRet = connect(Socket, (LPSOCKADDR)&saServer, sizeof(SOCKADDR_IN));
if (nRet == SOCKET_ERROR)
{
closesocket(Socket);
return WSAGetLastError();
}
std::string szBuffer_s;
szBuffer_s = "HEAD " + lpFileName + " HTTP/1.1 \r\nHost: " + lpServerName +
"\r\nConnection: close" "\r\n\r\n";
std::cout << "Client message sent. \n\n" << szBuffer_s << std::endl;
nRet = send(Socket, szBuffer_s.c_str(), szBuffer_s.length(), 0);
if (nRet == SOCKET_ERROR)
{
closesocket(Socket);
return WSAGetLastError();
}
char szBuffer[1024];
int array_int_copy = 0;
while(1)
{
nRet = recv(Socket, szBuffer, 1024, 0);
if (nRet == SOCKET_ERROR)
{
closesocket(Socket);
return WSAGetLastError();
}
if (nRet == 0)
break;
array_int_copy = 0;
while (array_int_copy != nRet)
{
*file += szBuffer[array_int_copy];
array_int_copy+=1;
}
}
closesocket(Socket);
WSACleanup();
return 0;
}