Quote:Original post by dphoenixWhat if you call that function 2 or 3 times? I bet it returns the same pointer each time, meaning if you store the pointers in a vector, you'll end up with all entries in the vector being pointers to the same character, meaning they'll seem to be the same string.
This is the function to get myIP:const char* Messenger::getMyIP(){ hostent* localInfo = gethostbyname(""); // get the local host name struct in_addr addr; memcpy(&addr,localInfo->h_addr_list[0],sizeof(struct in_addr)); const char* localIP = inet_ntoa(addr); return localIP;}
I've confirmed that that part works.
Quote:Original post by dphoenixIf m.sender is a const char*, it's just storing the pointer, not the contents. What is host?
Also, whenever another player sends me a message, the message automatically has their IP address attached by the TCP listener that gets the message:string mymessage(buffer);size_t found = mymessage.find("#");if((found != string::npos) && (found > 0))//found # delimiter{ Message m; int pos = int(found); string codeSub = mymessage.substr(0, pos); m.code = codeSub; m.message = mymessage.substr(pos+1); m.sender = host; messageList.push_back(m); //cout<<"Message in queue."<<endl;}
The preceding code is in the TCP listener, when it gets a message from another player. Each message has a code and the content, all delineated by the # sign. So that's what m.code and m.message are. Each message also has a space for the sender address, which is initially uninitialized when its sent (this was before I knew how to get my own IP address). m.sender is also a const char*,
Also, the test for fount > 0 and the cast of found to an it is redundant (std::string::find will always return npos or >= 0, and substr() takes size_t parameters rather than ints I think (Don't have VC to check though)).
Quote:Original post by dphoenix[smile]class Message{ public: Message(); Message(string c, string m); public: string code; string message; const char* sender;};Message::Message(){}Message::Message(string c, string m):code(c),message(m){}
That's what it looks like. I hope I didn't post too much irrelevant code, but I want to make sure I'm providing enough info because I am absolutely at my wit's end and ready to cry right now lol...
Basically, whenever you store a const char* you're storing the pointer, not the contents. If you're re-using a buffer (Like the sprintf() example in my last post), then you're just storing the address of the original buffer, not a copy of it.