0

This is the code where the socket has been created and recieved data is stored in a text file

// Initialize Winsock.
    cout << "connecting1\n";
    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]);   // this lines writes to file
    }
}

here the data is returned mostly in proper format, but in some cases this happens :

{
  "type": "node",
  "id":

1000     // this 1000 is un-wanted

 1812432236,
  "lat": 20.2608987,
  "lon": 85.8379894
},

so my program fails to parse this node properly. While writing the query in url, the data is displayed in proper format. Also the garbage value gets inserted in the same place every time I run the program. Why could this be happening ?

4

2 回答 2

1

I got answer from openStreetMap help forum, which was more specific to the above question...

link to question I posted there

于 2013-01-30T09:22:31.650 回答
0

In the recv function, it calls for a character pointer for the buffer. You have buffer declared to be a vector of chars, which is not the same as char buf[1024*1024]. So I believe the vector is returning a string of the structure of the item when you print it to file. The vector contains 1024*1024 objects of type char, not a 1024*1024 size of contiguous memory to store characters. The 1024*1024 vector contains chars objects stored as separate elements, but not as a continuous allocation in memory.

I would change to std::vector buf(buf_size, '\0'); to char buf[1024*1024]; and then do a memset(buf, 0, 1024*1024) before you read the data using recv. I believe that will correct the issue (assuming you are not sending over a \0 over the socket as data, which would indicate the end of the string, in which case %s would stopping printing out the data at that point).

于 2013-01-30T07:10:33.757 回答