NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum

#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <stdlib.h>
#include <winsock2.h>
#define SIZE 10000

using namespace std;

const std::string host  = "81.19.81.199"; // IP of overpass.osm.rambler.ru
const int port          = 80;

//(lower lat, usually lower lon, higher lat, usually higher lon).
const std::string query = "GET /cgi/interpreter?data=[out:json];(node(18.51507,73.87276,18.53597,73.90142);way(18.51507,73.87276,18.53597,73.90142);node(w)->.x;);out; HTTP/1.1\r\n"
                          "Host: overpass.osm.rambler.ru\r\n"
                          "User-Agent: SteveC\r\n"
                          "Accept: */*\r\n"
                          "Connection: close\r\n"
                          "\r\n";

int main(int argc, char* argv[]) {

    FILE *fp = fopen ("d://test.txt", "r+");

    // Initialize Winsock.
    WSADATA wsadata;
    int iResult = WSAStartup (MAKEWORD(2,2), &wsadata );
    if (iResult !=NO_ERROR )
        printf("\nmyERROR at WSAStartup()\n");

    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == -1) {
        perror("error opening socket"); return -1;
    }

    struct sockaddr_in sin;
    sin.sin_port = htons(port);
    sin.sin_addr.s_addr = inet_addr(host.c_str());
    sin.sin_family = AF_INET;

    if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
        perror("error connecting to host"); return -1;
    }

    const int query_len = query.length() + 1; // trailing '\0'
    if (send(sock, query.c_str(), query_len, 0) != query_len) {
        perror("error sending query"); return -1;
    }

    const int buf_size = 1024 * 1024;
    while (true) {
        std::vector<char> buf(buf_size, '\0');
        const int recv_len = recv(sock, &buf[0], buf_size - 1, 0);

        if (recv_len == -1) {
            perror("error receiving response"); return -1;
        } else if (recv_len == 0) {
            std::cout << std::endl; break;
        } else {
            std::cout << &buf[0];
            fprintf(fp, "%s", &buf[0]);
        }
    }

    return 0;
}

In this program some extra garbage values are inserted like :

{
  "type": "node",
  "id": 245647473,
  "lat": 18.5289245,
  "lon": 73.9019677,
  "tags": {
    "source": "AND"
  }
},
{

1000                         // garbage...

"type": "node",
  "id": 245647482,
  "lat": 18.5301753,
  "lon": 73.8676588,
  "tags": {
    "source": "AND"
  }
},

While executing the query in browser gives no problem. Earlier I thought there is some problem while writing to file, but the output by /std::cout << &buf[0];/ also gives the same result. Also the garbage value gets inserted in the same place every time I run the program. Why does it happen ?

asked 30 Jan '13, 06:35

Anubha's gravatar image

Anubha
31336
accept rate: 0%

edited 30 Jan '13, 06:41


Ah, that's my example code from this question with a little Winsock adjustments.

The number you see is not junk but the response is sent with chunked transfer encoding which you have to process first. This is just one of several possible transfer encodings which you SHOULD support in your program. As already explained in my answer to the other question you should better use a third-party library for HTTP traffic if you want a simple solution.

permanent link

answered 30 Jan '13, 06:43

scai's gravatar image

scai ♦
33.3k21309459
accept rate: 23%

Thank you scai, its a real comfort to know those values are not junk, now that I know this I will support them in my parser happily. As for third-party library, for now JSON parser code I wrote works okay and renders pretty roads(with some nodes missing due to this encoding).In this month I coded the 3d car that is to travel these roads. Hope to complete the road stuff in feb. Your help so far is highly appreciated.

(30 Jan '13, 09:20) Anubha

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×483
×29
×5

question asked: 30 Jan '13, 06:35

question was seen: 4,411 times

last updated: 30 Jan '13, 09:20

NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum