0

I have the next code to make a connection to my website:

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include <Windows.h>
#include <WinSock2.h>
#include <WS2tcpip.h>
#include "Client.h"
#include <iostream>

#pragma comment(lib, "Ws2_32.lib")

void WSAStartup()
{
    // This will lead us all the way long...
    int Errors;
    WSADATA wsadata;
    Errors = WSAStartup(MAKEWORD(2, 2), &wsadata);
    if (Errors != 0)
    {
        std::cout << "E1, " << Errors << ".\n";
        getchar();
        exit(1);
    }
    //---------------------------------------
}


int main()
{
    WSAStartup();
    Client http_client;

    http_client.ZeroMem();
    http_client.GetInfo("www.xxx.xxx.xxx", "http"); // "http" is like writing 80. can be also integer parameter.
    http_client.CreateSocket();
    if (!http_client.Connect()) // If true, so it means there all went well.
    {
        std::cout << "Good for us! We are in.\n";
    }

    http_client.Recv();
    http_client.~Client();

    std::cin.get();
    return 0;
}

In the header "Client.h" I did all the "dirty staff"... I get the next output:

Good for us! We are in.

So apparently the problem is not the coding. I thought when I makes a connetion of "http" with a website, so the first thing I get in response is a DATA from the website containing the all html code of the index page, buy it doesnt.

I run this program while 'Wiresharking' to see what I get between the SYN till the FIN, here is the screenshot of what I got:

Screenshot of wireshark while runing my code

There isnt any DATA I get back as you can see... So how exactly should I work with sockets in cpp when I ask (or send a post request) html data? Links will be appreciated. thanks.

wrg wfg
  • 77
  • 6
  • Why don't you use WinHTTP instead of WinSock? HTTP is a higher level protocol, there is no point to use sockets. (Unless it is a learning exercise) – BCartolo Feb 08 '17 at 15:20
  • @BCartolo Because I learning how to use sockets and how the all staff works behind the scense at the low level. Can you hellp me? – wrg wfg Feb 08 '17 at 15:26
  • This looks ok to me: http://www.binarytides.com/winsock-socket-programming-tutorial/ But generally msdn is the place to learn windows specific API. If portability is an issue, you might want to use some wrapper. For example: http://stackoverflow.com/questions/10574857/simple-cross-platform-tcp-ip-api – BCartolo Feb 08 '17 at 15:31
  • Are you making an HTTP request? I see nothing between Connect and Recv. – Art Yerkes Feb 08 '17 at 15:32
  • 1
    Don't make explicit use of the destructor, `http_client.~Client()`, it makes your program undefined. You should probably start with one of [these](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Feb 08 '17 at 15:39
  • "I thought when I makes a connetion of "http" with a website, so the first thing I get in response is a DATA from the website containing the all html code of the index page" - no that's not how it works. The client needs to make a request before the server sends anything when using HTTP. Do use an HTTP(S) client library, it is no easy task to do it correctly yourself with plain sockets. – Mat Feb 08 '17 at 15:40
  • Not only don't you get anything back, you're also not requesting anything to be sent back. All you're doing is establishing a TCP connection and closing it. Do you have any idea about how HTTP works at all? – molbdnilo Feb 08 '17 at 15:48

0 Answers0