msanford
April 23rd, 2003, 12:52 PM
I am trying to request a html page and save it into a file. It works sometimes and other times it core dumps. Here is my complete source code:
#include <stdio.h>
#include <windows.h>
#include <winsock.h>
#define NETWORK_ERROR -1
#define FILE_ERROR -2
int GetPage(const char* webAddress, const char* page, const char* fileout);
int main(int a, char** b)
{
return GetPage("google.com", "/search?q=broncos", "hfile.htm");
}
int GetPage(const char* webAddress, const char* page, const char* fileout)
{
WORD sockVersion;
WSADATA wsaData;
int nret;
int retval=0;
char buf[1001];
FILE* fout;
if (strlen(fileout)==0 || (fout=fopen(fileout, "w"))==0)
return FILE_ERROR;
sockVersion = MAKEWORD(1, 1);
WSAStartup(sockVersion, &wsaData);
LPHOSTENT hostEntry;
hostEntry = gethostbyname(webAddress);
if (!hostEntry) {
nret = WSAGetLastError();
WSACleanup();
return NETWORK_ERROR;
}
SOCKET theSocket;
theSocket = socket(AF_INET, SOCK_STREAM,IPPROTO_TCP);
if (theSocket == INVALID_SOCKET)
{
WSACleanup();
return NETWORK_ERROR;
}
SOCKADDR_IN serverInfo;
serverInfo.sin_family = AF_INET;
serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
serverInfo.sin_port = htons(80);
nret = connect(theSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));
if (nret == SOCKET_ERROR)
{
WSACleanup();
return NETWORK_ERROR;
}
int rval;
sprintf(buf, "GET %s\n\n", page);
::send(theSocket, buf, strlen(buf), 0);
while ((rval=::recv(theSocket, buf, 1000, 0))>0)
{
if (rval==SOCKET_ERROR)
{
retval=NETWORK_ERROR;
break;
}
else
{
buf[rval]='\0';
fprintf(fout, buf);
}
}
closesocket(theSocket);
fclose(fout);
WSACleanup();
return retval;
}
However I know that the problem must be in the section where I a recieving the file:
int rval;
sprintf(buf, "GET %s\n\n", page);
::send(theSocket, buf, strlen(buf), 0);
while ((rval=::recv(theSocket, buf, 1000, 0))>0)
{
if (rval==SOCKET_ERROR)
{
retval=NETWORK_ERROR;
break;
}
else
{
buf[rval]='\0';
fprintf(fout, buf);
}
}
can anybody tell me what I am doing wrong here? Thanks for your help!!!
#include <stdio.h>
#include <windows.h>
#include <winsock.h>
#define NETWORK_ERROR -1
#define FILE_ERROR -2
int GetPage(const char* webAddress, const char* page, const char* fileout);
int main(int a, char** b)
{
return GetPage("google.com", "/search?q=broncos", "hfile.htm");
}
int GetPage(const char* webAddress, const char* page, const char* fileout)
{
WORD sockVersion;
WSADATA wsaData;
int nret;
int retval=0;
char buf[1001];
FILE* fout;
if (strlen(fileout)==0 || (fout=fopen(fileout, "w"))==0)
return FILE_ERROR;
sockVersion = MAKEWORD(1, 1);
WSAStartup(sockVersion, &wsaData);
LPHOSTENT hostEntry;
hostEntry = gethostbyname(webAddress);
if (!hostEntry) {
nret = WSAGetLastError();
WSACleanup();
return NETWORK_ERROR;
}
SOCKET theSocket;
theSocket = socket(AF_INET, SOCK_STREAM,IPPROTO_TCP);
if (theSocket == INVALID_SOCKET)
{
WSACleanup();
return NETWORK_ERROR;
}
SOCKADDR_IN serverInfo;
serverInfo.sin_family = AF_INET;
serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
serverInfo.sin_port = htons(80);
nret = connect(theSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));
if (nret == SOCKET_ERROR)
{
WSACleanup();
return NETWORK_ERROR;
}
int rval;
sprintf(buf, "GET %s\n\n", page);
::send(theSocket, buf, strlen(buf), 0);
while ((rval=::recv(theSocket, buf, 1000, 0))>0)
{
if (rval==SOCKET_ERROR)
{
retval=NETWORK_ERROR;
break;
}
else
{
buf[rval]='\0';
fprintf(fout, buf);
}
}
closesocket(theSocket);
fclose(fout);
WSACleanup();
return retval;
}
However I know that the problem must be in the section where I a recieving the file:
int rval;
sprintf(buf, "GET %s\n\n", page);
::send(theSocket, buf, strlen(buf), 0);
while ((rval=::recv(theSocket, buf, 1000, 0))>0)
{
if (rval==SOCKET_ERROR)
{
retval=NETWORK_ERROR;
break;
}
else
{
buf[rval]='\0';
fprintf(fout, buf);
}
}
can anybody tell me what I am doing wrong here? Thanks for your help!!!