0

I'm making a low level peer to peer application, when a peer needs to know what file is with what peer, it asks the tracker/server(which is maintaining a register). The tracker then replies with the address of that peer. In my program, the tracker side is sending the peer information without fault, but at the receiving side that information is not being received. I have wasted hours on end trying to sort this out and I'm out of my depth trying to solve this. The code for the method that asks the tracker/server is

void ask(string file){

    my_addr.sin_family = AF_INET;
    my_addr.sin_port = MY_PORT;
    my_addr.sin_addr.s_addr=inet_addr(IP.c_str());

    tracker_addr.sin_family = AF_INET;
    tracker_addr.sin_port = TRACKER_PORT;
    tracker_addr.sin_addr.s_addr=inet_addr(IP.c_str());

    char* msg = new char();
    int sendSock = socket(AF_INET, SOCK_DGRAM, 0);
    //send filename
    cout << "Asking for file: " << file << endl;
    sendto(sendSock, file.c_str(), strlen(file.c_str()), 0, (sockaddr *) &tracker_addr, sizeof(tracker_addr));
    int x = bind(recvSock,(sockaddr *)& my_addr, sizeof(my_addr));if(x < 0) perror("bind");
    //recieve info, 0 if no found
    recvfrom(recvSock, msg, sizeof(msg), 0, (sockaddr *) &tracker_addr, (socklen_t *) &tracker_addr);
    cout << "Checks" << endl;
    int portToConnect = atoi(msg);

    if(portToConnect != 0)
        cout << "File is found with peer on port: " << portToConnect << endl;
    else
        cout << "No such file found" << endl;
}

take a look and see if you can help. Thank-you in anticipation :)

EDIT: Since recvfrom is a blocking call, my program waits infinitely for a message from the tracker/server, even though the message has been sent.

Adnan Khan
  • 387
  • 2
  • 12
  • `sizeof(msg)` does not work work the way you think it does. – Captain Obvlious Nov 19 '15 at 19:28
  • I've used strlen(msg); aswell. What else can you suggest? And what is wrong with sizeof(msg); in this particular context? – Adnan Khan Nov 19 '15 at 19:35
  • 1
    Check the return value of all your functions, and use `perror` to print the error string. It will tell you many useful things. – dbush Nov 19 '15 at 19:42
  • `atoi(msg);`. In that call `msg` needs to be a valid string but `char* msg = new char();` only allocates one `char`. How can `msg` have enough space to fit any non-empty string in it? – kaylum Nov 19 '15 at 19:50
  • Yeah, tried that. Everything goes good until I reach the recvfrom(); method. It waits there infinitely. The perror() for recvfrom(); doesn't get to work. :'( @dbush – Adnan Khan Nov 19 '15 at 19:53
  • @kaylum that would be a problem if I had received anything in recvfrom(); The problem is that I'm not receiving anything in recvfrom(); The program waits for a message that it doesn't receive. It's waiting infinitely – Adnan Khan Nov 19 '15 at 19:58
  • In that case, run `netstat -an` from the shell to ensure you're bound to the proper port, and run Wireshark to ensure that the packet is coming in to the address/port you expect. – dbush Nov 19 '15 at 20:01
  • 1
    @AdnanKhan You didn't actually tell us that. Would be good to add that info into your question proper as you would agree it's pretty relevant. And technically, since your `recvfrom` has been given invalid parameters your program has Undefined Behaviour and hanging is a possible (but not probable) outcome. So fix all those bugs already pointed out first to remove all such doubts. – kaylum Nov 19 '15 at 20:01
  • Yes, that part is pretty relevant. But how has my `recvfrom` function been given invalid parameters? Can you please explain? @kaylum – Adnan Khan Nov 19 '15 at 20:20
  • 1
    Refer to the first comment. `sizeof (msg)` is 4 or 8. But `msg` is only a 1 byte buffer. So you are telling `recfrom` that it can write past valid memory. – kaylum Nov 19 '15 at 20:24
  • I tried `char msg[100] = "";` but that doesn't work either. Secondly, that would be a problem if `recvfrom()` would write anything in the buffer, and if it did, printing out would lead to a garbage value. But thats not the case here, the problem is, that `recvfrom()` is not receiving anything. @kaylum – Adnan Khan Nov 19 '15 at 20:33
  • 1
    How do you know it didn't write anything? The point of Undefined Behaviour is that you can't predict what will happen. It could have written to invalid memory and then hung the process. Unlikely but possible. Please search for Undefined Behaviour if you want to learn more - the topic is asked a couple of times a day. The point is really just to fix all known bugs first to eliminate any undefined possibilities. – kaylum Nov 19 '15 at 20:47
  • `recvfrom(recvSock, msg, sizeof(msg), 0, (sockaddr *) &tracker_addr, (socklen_t *) &tracker_addr);` -- The final parameter should be a pointer to a variable containing the length of the sockaddr buffer. Something like `socklen_t tracker_addr_len = sizeof(tracker_addr); recvfrom(recvSock, msg, sizeof(msg), 0, (sockaddr *) &tracker_addr, &tracker_addr_len);` (assuming you fix the `sizeof(msg)` problems mentioned earlier). – keithmo Nov 19 '15 at 23:46

0 Answers0