0

okay, so I have a prob where when I try to get a struct sockaddr_in from my map, and use a tmp var to then send the stuct sockaddr_in through a socket using the method sendto.

map<string,struct sockaddr_in> userToAddrStrct;

after I call recvfrom i put the struct sockaddr into my map with a name as key. then when I try to get that address again in another request i do.

string getUserOfCurrAddr()
{ 
    //recAddr is the (struct sockaddr_in) i initially use with recvfrom() method to receive address
    struct sockaddr_in* address = (struct sockaddr_in*)&recAddr;
    string aTmp = "";     
    map<string,struct sockaddr_in>::iterator i;
    for(i=userToAddrStrct.begin(); i != userToAddrStrct.end(); i++) {
        cout << "before checkEQ call\n";
        if(checkAddrEq(i->second,*address) == 0) {
            aTmp = i->first;
        }
    }
    return aTmp;
}
string username = getUserOfCurrAddr();
map<string, struct sockaddr_in>::iterator sockIt = userToAddrStrct.find(username);

and then I try to print out the contents in this map and I get a seg fault 11. here is the code I used to print.

for(map<string,struct sockaddr_in>::iterator isu = userToAddrStrct.begin(); isu != userToAddrStrct.end(); isu++) {
        cout << "User: " << isu->first << " with address: " << stringAddr(isu->second) << " with port #: " << isu->second.sin_port <<"\n"; 
    }

Any help would be very appreciated. I am bad with c++ and may have a bad reference.

thanks to all!!!!

poutyboi
  • 149
  • 5
  • 20
  • You get a seg fault where exactly? Have you used a debugger? – John Zwinck Nov 09 '14 at 13:36
  • no i haven't I just know I by using print statements it adds to the map when it shouldn't. so when i print size for map it says 121424856313667 which is way big and it seg faults because thats an addrss probably – poutyboi Nov 09 '14 at 13:40
  • Where is the code that populates `recAddr` and `userToAddrStrct`? – Remy Lebeau Nov 09 '14 at 16:47
  • Also, `getUserOfCurrAddr()` should exit as soon as it finds a match, and maybe also return the iterator so you don't have to call `find()` to search the `map` again. And when outputting `isu->second.sin_port`, you need to use `ntohs()`. – Remy Lebeau Nov 09 '14 at 16:50

0 Answers0