1

I am using a simple fragment of winsocket code on my C++ project, trying to pull data from a file on a shared host (host-ed.me). My winsocket code is the following:

void write(string file, string text) {
   ofstream os;
   os.open(file, ofstream::app);
   text += "\n";
   os.write(text.c_str(), text.size());
   os.close();
}

bool request() {
   WSADATA wsa;
   SOCKET s;
   struct sockaddr_in server;
   char server_reply[4096];
   int recv_size;
   // initialize winsockets
   if (WSAStartup(MAKEWORD(2,2), &wsa) != 0) {
      write("log.txt", "winsockets initialization - error!");
      return false;
   }
   // open a socket
   if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) {
      write("log.txt", "socket opening - error!");
      return false;
   }
   // connect to server
   server.sin_addr.s_addr = inet_addr("46.105.118.169");
   server.sin_family = AF_INET;
   server.sin_port = htons( 80 );
   if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0) {
      write("log.txt", "server connection - error!");
      return false;
   }
   // send data
   string request;
   request += "GET /index.php HTTP/1.1\r\n";
   request += "Host: machines.host-ed.me\r\n";
   request += "Connection: close\r\n";
   request += "\r\n";
   if (send(s, request.c_str(), request.size(), 0) < 0) {
      write("log.txt", "data send - error!");
      return false;
   }
   // receive reply
   if ((recv_size = recv(s, server_reply, 4096, 0)) == SOCKET_ERROR) {
      write("log.txt", "data receive - error!");
      return false;
   }
   server_reply[recv_size] = '\0';
   write("log.txt", string(server_reply));   // gives strange characters
   // close socket
   closesocket(s);
   WSACleanup();
   return true;
}

What i mean by strange characters, look this reply:

HTTP/1.1 200 OK
Date: Tue, 09 Jan 2018 15:02:01 GMT
Server: Apache
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html

9
hello PHP
0

I mean those 9 and 0 before and after the content. My PHP code in the server looks like this:

<?php echo "hello PHP"; ?>

I do not know if it's a server issue, but if a change the text to "hello2 PHP", then it returns to my winsock code something like this:

a
hello2 PHP

I am clueless

vlzvl
  • 371
  • 4
  • 12

1 Answers1

1

What you're seeing is the HTTP 1.1 chunked transfer encoding, as is specified by the server in the header

Transfer-Encoding: chunked

Each chunk starts with the number of octets of the data it embeds expressed as a hexadecimal number in ASCII followed by optional parameters (chunk extension) and a terminating CRLF sequence, followed by the chunk data. The chunk is terminated by CRLF.

rustyx
  • 80,671
  • 25
  • 200
  • 267
  • thanx, so these numbers are simply the size as i get it. Can it change the request header (winsock ) so i do not get a chunked encoding? or if there's a header on php? – vlzvl Jan 09 '18 at 15:17
  • 1
    Yes, send an `HTTP/1.0` request instead of `HTTP/1.1` – rustyx Jan 09 '18 at 15:18
  • 1
    @user6096479 According to [this old answer](https://stackoverflow.com/a/23774393/440558) if you claim to be HTTP/1.1 compliant you *must* support chunked encoding. – Some programmer dude Jan 09 '18 at 15:20
  • thank you both and for the links !! it was that. I will accept when it allows me – vlzvl Jan 09 '18 at 15:22