0
#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";}
3
Contributors
9
Replies
33
Views
3 Years
Discussion Span
Last Post by zuki88
0

That's not a question, just a statement of what you want to do. No one but you knows why you posted that code. If you want help then you have to tell us what you want help with.

Edited by Ancient Dragon

0

like i said.i want to strcmp response.

if (strcmp(response, "ok") == 0) 
{
echo "word was found in webpage";}

but this dosent work.i want to know if its another way to compare.

0

Do you know what strcmp does? strcmp is a function that accepts char pointers. You don't have char pointers. You have C++ strings. You can compare a C++ string like this: if (response == "ok")

echo "word was found in webpage";

This doesn't look like C++ code. echo is not a C++ keyword and you don't have a function named echo and even if you did the syntax here is all wrong. Did you perhaps mean something likecout << "word was found in webpage";?

0

sory my bad echo its cout
yes i already try this

if (response == "ok")

but it dosent work

Edited by zuki88

0

Yes it does. If the string response is "ok", that will do whatever is inside the if loop. It works exactly as expected. What makes you think the string contains "ok"?

Learn to debug. Here's a hint; cout << "response is: " << response << endl;

Edited by Moschops

0

it makes me think that string response contain ok,its because i insert in to a
index.php

<h1>ok</h1>

Edited by zuki88

0

How does index.php have anything to do with your c++ program?

One other point about comparing strings -- the std::string == operator is case-sensitive, that is "ok" is not the same as "Ok" or "OK". So if response contains "Ok" then the comparison will fail. You need to make sure both have the same case by converting all the characters in response to lower case before doing the comparison.

0

yes i know what it means case sensitive.and i know for that std::string compare too. But problem is that i get

HTTP/1.1 200 OK
Date: Sun, 29 Sep 2013 14:37:59 GM
Server: Apache
X-Powered-By: PHP/5.2.17
Content-Length: 201
Connection: close
Content-Type: text/html

<html>
<body>
<h1>ok</h1>
</body>
</html>

this as a output.is there someway to extract

<h1>ok</h1>

into a string

Edited by zuki88

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.