Click to See Complete Forum and Search --> : Socket Help


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!!!

msanford
April 23rd, 2003, 05:20 PM
I finally got it to work correctly all the time by changing fprintf to fputs. I dont understand what is the difference between the two and why would it cause a core dump? Is it possible that printf if asynchrounus and the data in buffer is being changed faster that it can out put it? Hmmmm one to ponder I suppose....

muthuis
April 24th, 2003, 01:10 AM
Why did you not have a formatting string inside fprintf?

fprintf(fout, "%s",buf);

Are there any specific reasons?

msanford
April 24th, 2003, 12:59 PM
does it make a difference?

isnt it the same thing if I do:


char* str1="Hello World "
char* str2="Hello World %s"
char* str3="How are you?\n"

fprintf(fout, "%s%s", str1, str3);
fprintf(fout, str2, str3);
fprintf(fout, "Hello World how are you?\n");


In any case I guess I learned my lesson, to use fputs.

muthuis
April 24th, 2003, 09:06 PM
The example u quoted is fine. But if you really look at your while loop for recv, won't the buf variable get rewritten everytime you do a recv.

In that case you are left with no formatting characters.

Even if you have that character left without being replaced, you need the third parameter to pass the value for that formatting character.