Winsock recv giving gibberish mixed with useful html

Course Queries Syllabus Queries . 2 years ago

  0   1   0   0   0 tuteeHUB earn credit +10 pts

5 Star Rating 5 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Write Your Comments or Explanations to Help Others



Tuteehub forum answer Answers (1)


profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago


I'm trying to get the html source of a webpage www.chemguide.co.uk (it has pages that aren't miles long) using winsock implemented in c++. Most of the data that comes through is good, but at certain points in the output there is a certain character (it looks like |¦ on the console and some sort of I on here) being repeated, I think in groups of 8, and there are some other strange characters as well.

Also, some of the document seems to be printed after the end of the page (the tag. Here's the code:

// Portprog.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"
#include 
#include 
#include 
#include 
#include 
#include 


#pragma comment(lib, "ws2_32.lib") //Winsock library

int getHTML(std::string *result)
{
    WSADATA wsa;
    SOCKET s;
    SOCKADDR_IN server;
    using std::string;
    using std::cout;
    using std::endl;

    cout << "Initialising Winsock...";
    if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
    {
        cout << "Failed. Error Code: " << WSAGetLastError();
        return 1;
    }
    cout << "Winsock initialised." << endl;

    if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
    {
        cout << "Could not create socket: " << WSAGetLastError() << endl;
        return 1;
    }
    cout << "Socket created." << endl;

    server.sin_addr.s_addr = inet_addr("217.27.240.124");
    server.sin_family = AF_INET;
    server.sin_port = htons(80); //host to network endian short

    //Connect to remote server
    if (connect(s, (SOCKADDR *)&server, sizeof(server)) < 0)
    {
        cout << "Connection failed." << endl;
        return 1;
    }
    cout << "Connected." << endl;

    //Send some data
    string srequest = "GET / HTTP/1.1\r\n";
    srequest += "Host: chemguide.co.uk\r\n";
    srequest += "Connection: close\r\n";
    srequest += "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n";
    srequest += "\r\n";

    char crequest[10000];
    int requestSize = srequest.length() + 1;
    strncpy_s(crequest,
                                                    
                                                    
0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community