I would like to know how any of this is incorrect or is "buggy"...
Code:
void dSend (int D2JSP, char* request) {
if (send(D2JSP, request, strlen(request), 0) == SOCKET_ERROR) {
cout<<"Failed to send request to D2JSP... " << WSAGetLastError() << endl;
shutdown(D2JSP, 2); closesocket(D2JSP); WSACleanup(); exit(0);
}
cout<< request << endl;
}
int main()
{
WSADATA WsaDat;
SOCKET D2JSP;
sockaddr_in D2;
hostent* dHost;
char *dIP, *request, dbuf[512];
int dRecv;
unsigned short dline = 0, cwrite = 0;
request = "GET /index.php?showforum=168 HTTP/1.1\r\nHost: forums.d2jsp.org\r\n\r\n";
if (WSAStartup(MAKEWORD(2, 0), &WsaDat) != 0) {
cout<<"WSAStartup failed to initalize...\n"; return 0;
}
D2JSP = socket(AF_INET, SOCK_STREAM, 0);
if (D2JSP == INVALID_SOCKET) {
cout<<"Failed to make D2JSP socket... " << WSAGetLastError() << endl;
WSACleanup(); return 0;
}
dHost = gethostbyname("forums.d2jsp.org");
dIP = inet_ntoa (*(in_addr*) dHost->h_addr);
cout<<"D2JSP IP: " << dIP << endl;
D2.sin_family = AF_INET;
D2.sin_addr.s_addr = inet_addr (dIP);
D2.sin_port = htons (80);
if (connect(D2JSP, (sockaddr*) &D2, sizeof(D2)) == INVALID_SOCKET) {
cout<<"Failed to connect to D2JSP socket... " << WSAGetLastError() << endl;
shutdown(D2JSP, 2); closesocket(D2JSP); WSACleanup(); return 0;
}
dSend(D2JSP, request);
ofstream dparse ("d2jsp.txt");
do {
dRecv = recv(D2JSP, dbuf, 512, 0);
if (dRecv == SOCKET_ERROR) {
cout<<"Failed to recieve data through D2JSP... " << WSAGetLastError() << endl;
break;
}
dbuf[dRecv] = '\0';
if (!dparse.is_open()) { cout<<"Failed to open d2jsp.txt...\n"; break; }
else if (dparse.is_open()) dparse << dbuf;
}
} while (dRecv > 0);
dparse.close();
shutdown(D2JSP, 2); closesocket(D2JSP); WSACleanup();
return 0;
}
For the changes that was made thanks to the other guys, it has the same behavior but dont think i dont appericate the clean up but i have the simple winsock function and i keep gettin cut lines.