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.