c++ maps and struct sockaddr_in copying values and using sendto

916 Views Asked by At

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!!!!

0

There are 0 best solutions below