I get error that packet ain't sent. With TCP this code worked.Things I've tried:
1)change port ::AnyPort
2)Use IP from hamachi with my friend
3)put debugging couts. After the first "while msg!=stop" loop it changes port and IP to 0 and 0000000000(Or so would seem)
4)Removing it from thread partially, *exclamation mark* partially fixed the problem, it worked 1 out of 10 times!
Help...I'll answer all questions quick.
sf::IpAddress hostIp = sf::IpAddress::getLocalAddress();
sf::UdpSocket udpSocket;
unsigned short port = 51234;
string recMsg ;
string msg;
void sendData(string msg);
sf::Thread secondThread(&sendData, msg);
int main() {
udpSocket.bind(port);
secondThread.launch();
while (msg != "stop") {
if (udpSocket.receive(packet2, hostIp, port) != sf::Socket::Done)
cout << "~~error receiving data" << endl;
else {
cout << "--data received" << endl;
packet2 >> recMsg;
if (recMsg != "")
cout << "Other user: " << recMsg << endl;
}
}
}
void sendData(string msg) {
while (msg != "stop") {
getline(cin, msg);
packet << msg;
if (udpSocket.send(packet, hostIp, port) != sf::Socket::Done)
std::cout <<"~~Could not send data"<<endl;
}