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.


#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);
    wstring page = client.GetResponseContent();

    wcout << page << endl;

    return 1;

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

    //wstring url = L"http://yahoo.com";
    wstring url = L"";

    char a;
    cin >> a;
    return 0;


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

#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);

        send(socketfd, buffer, strlen(buffer), 0);

        shutdown(socketfd, SD_BOTH);
2 Weeks
Discussion Span
Last Post by rproffitt

I added the HTTP header, and still the same , it seems the problem may be at the client part. I tried , it doesn't work.

        sprintf(buffer, "HTTP/1.1 200 OK\n", 16);
        sprintf(buffer, "Content-length: 46\n", 19);
        sprintf(buffer, "Content-Type: text/html\n\n", 25);
        sprintf(buffer, "<html><body><H1>Hello world</H1></body></html>",46);

Edited by Me


I did not review your server code but if the client works with websites, the thoughts I have is the HTTP which you changed and your server code. Try one of the small 40 or so line web servers before you begin to code from scratch.


Line 17 of your client code is your variable declaration yet you reference it in line 15 and 16. Pretty sure the variable needs to declared before use.


@Christopher_23. I think line 17 declares the variable in line. Some disagree with this practice so I just don't get the debate if that's good or bad. Notice "wstring"?

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.