-
July 29th, 2018, 07:13 AM
#1
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);
}
}
-
July 29th, 2018, 02:29 PM
#2
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?
-
July 30th, 2018, 07:59 AM
#3
Re: Getting empty string as a response from a web server app
The browser connects every time
-
August 11th, 2018, 06:03 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|