I think this probably belongs here, since it seems to be an error with my C++, not my interpretation of winsock. However, feel free to move it if I'm wrong.
Okay, when I run this program, it returns the webpage okay, but I've been having problems once it's finished; it exits with this debug error: Run-Time Check Failure #2 - Stack around the variable 'stuff' was corrupted.
Here's the improtant code (the rest is just a house for it)
greeting.cpp
On a related note, how would I get stuff from subdirectories? I can only seem to get something from the front page of the website (ie. www.binrev.com). Cheers in advance!Code:#include "greeting.h" //this logs the data into a text file void WriteToFile(char *stuff) { ofstream current_file("greeting_log.html", ios::app); //open the file current_file << stuff << "\n"; current_file.close(); } void ReportError(int errorCode, const char *whichFunc) { char errorMsg[92]; //character array to hold the message ZeroMemory(errorMsg, 92); //automatically null-terminate the string //copy the phrase, whichFunc string, and int errorCode into the buffer sprintf(errorMsg, "Call to %s returned error %d!", (char *)whichFunc, errorCode); WriteToFile(errorMsg); } int greeting() { char website[60]; cout << "What website do you want? "; cin >> website; //variables WORD sockVersion; //holds the version of winsock in use WSADATA wsaData; //queries the version of winsock required int nret; //stores error stuff char info[100]; char stuff[400]; sprintf(info, "<!--"); WriteToFile(info); sockVersion = MAKEWORD(1,1); //we need winsock version 1.1 nret = WSAStartup(sockVersion, &wsaData); //initialise winsock sprintf(info, "sockVersion = %ld", nret); WriteToFile(info); SOCKET theSocket; //my single socket theSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //initialises the socket if(theSocket == NETWORK_ERROR) { nret = WSAGetLastError(); //mroe detailed error info ReportError(nret, "connect()"); //reports error with custom function WSACleanup(); //clean up winsock return NETWORK_ERROR; //return -1 } sprintf(info, "Socket = %ld", theSocket); WriteToFile(info); //host information (well, location) LPHOSTENT hostEntry; hostEntry = gethostbyname(website); sprintf(info, "hostEntry = %s", hostEntry); WriteToFile(info); //fill in socket addressing information SOCKADDR_IN serverInfo; serverInfo.sin_family = AF_INET; //uses TCP/IP protocol suite serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list); //works out all addresses of target serverInfo.sin_port = htons(80); //port to connect through sprintf(info, "server info filled in"); WriteToFile(info); nret = connect(theSocket,(LPSOCKADDR)&serverInfo, sizeof(sockaddr)); if(nret == SOCKET_ERROR) { nret = WSAGetLastError(); //mroe detailed error info ReportError(nret, "connect()"); //reports error with custom function WSACleanup(); //clean up winsock return NETWORK_ERROR; //return -1 } sprintf(info, "Connect = %ld", nret); WriteToFile(info); strcpy(stuff, "GET / \r\n"); nret = send(theSocket, stuff, sizeof(stuff) , 0); sprintf(info, "send = %ld", nret); WriteToFile(info); while(nret != 0) { strset(stuff, ' '); nret = recv(theSocket, stuff, sizeof(stuff), 0); WriteToFile(stuff); } MessageBox(0, "done!", "Finished", 0); sprintf(info, "-->"); WriteToFile(info); closesocket(theSocket); WSACleanup(); return(0); }



LinkBack URL
About LinkBacks




)