#include <iostream>
#include <string>
#include <stdlib.h>
#include <winsock.h>//dont forget to add wsock32.lib to linker dependencies
using namespace std;
#define BUFFERSIZE 1024
void die_with_error(char *errorMessage);
void die_with_wserror(char *errorMessage);
int main(int argc, char *argv[])
{
string request;
string response;
int resp_leng;
char buffer[BUFFERSIZE];
struct sockaddr_in serveraddr;
int sock;
WSADATA wsaData;
char *ipaddress = "127.0.0.1;
int port = 80;
request+="GET /index.php HTTP/1.0\r\n";
request+="Host: localhost\r\n";
request+="\r\n";
//init winsock
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
die_with_wserror("WSAStartup() failed");
//open socket
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
die_with_wserror("socket() failed");
//connect
memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = inet_addr(ipaddress);
serveraddr.sin_port = htons((unsigned short) port);
if (connect(sock, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0)
die_with_wserror("connect() failed");
//send request
if (send(sock, request.c_str(), request.length(), 0) != request.length())
die_with_wserror("send() sent a different number of bytes than expected");
//get response
response = "";
resp_leng= BUFFERSIZE;
char z;
while (resp_leng == BUFFERSIZE)
{
resp_leng= recv(sock, (char*)&buffer, BUFFERSIZE, 0);
if (resp_leng>0)
response+= string(buffer).substr(0,resp_leng);
//note: download lag is not handled in this code
}
//display response
cout << response << endl;
//disconnect
closesocket(sock);
//cleanup
WSACleanup();
return 0;
}
void die_with_error(char *errorMessage)
{
cerr << errorMessage << endl;
exit(1);
}
void die_with_wserror(char *errorMessage)
{
cerr << errorMessage << ": " << WSAGetLastError() << endl;
exit(1);
}
ok i got this code to open and read from website.i want to do strcmp
like
if (strcmp(response, "ok") == 0)
{
echo "word was found in webpage";}