dimanche 29 juillet 2018

Getting empty string as a response from a web server app

I have a web server app, and a client app that works like a browser. I get the desired response from the server app if I type my IP in the browser window, if I read the response to the client app I get an empty string. The client app works only if I read the URL of a website, but not with my IP.

Client:

#include "stdafx.h"
#include "WinHttpClient.h"
#include  <iostream>
#include <fstream>
#include  <algorithm>
#include <regex>
#include <string>
#include <vector>
#include <sstream>
#include <utility>

using namespace std;

int getResponse(wstring url) {
    WinHttpClient client(url);
    client.SendHttpRequest();
    wstring page = client.GetResponseContent();

    wcout << page << endl;

    return 1;
}

int _tmain(int /*argc*/, _TCHAR** /*argv*/)
{

    //wstring url = L"http://yahoo.com";
    wstring url = L"104.171.115.164";
    getResponse(url);

    char a;
    cin >> a;
    return 0;
}

Server:

#pragma comment(lib, "WS2_32.lib")
#define _CRT_SECURE_NO_DEPRECATE

#include <stdio.h>
#include <direct.h>
#include <winsock2.h>

long    nbytes;
static char buffer[8096 + 1];

int main(int argc, char **argv)
{
    WSADATA wsaData;
    SOCKET  listenfd, socketfd;
    static struct sockaddr_in cli_addr;  /* static = initialised to zeros */
    static struct sockaddr_in serv_addr; /* static = initialised to zeros */
    int hit;
    size_t  length;


    WSAStartup(MAKEWORD(2,2), &wsaData);

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(80);

    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    bind(listenfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
    listen(listenfd, 64);



    for (hit = 1; ; hit++)
    {
        length = sizeof(cli_addr);
        socketfd = accept(listenfd, (struct sockaddr *) &cli_addr, &length);

        recv(socketfd, buffer, 8096, 0);

        sprintf(buffer,"123456");
        send(socketfd, buffer, strlen(buffer), 0);

        shutdown(socketfd, SD_BOTH);
    }
}




Aucun commentaire:

Enregistrer un commentaire