CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2017
    Posts
    41

    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:

    Code:
    #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:
    Code:
    #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);
        }
    }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Getting empty string as a response from a web server app

    What happens if you use the browser to connect to your local server twice in a row? Can the browser connect the second time?

  3. #3
    Join Date
    Dec 2017
    Posts
    41

    Re: Getting empty string as a response from a web server app

    The browser connects every time

  4. #4
    Join Date
    Aug 2018
    Location
    Burbank, California
    Posts
    5

    Re: Getting empty string as a response from a web server app

    I am guessing you should probably check what happens when you use a different recv option than just zero.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured