Hi!
I've got a problem using UDP Sockets to send data from one Thread to another.
A little background: I'm trying to program a game client and usually I'm using TCP Sockets for the communication. However, at this particular point in time, I need to send a request to all players and work with whatever comes back first.
I think the only way to do that with TCP Sockets is to start an extra thread for every player, am I correct? Because that seemed a bit excessive, I thought of using UDP Sockets to wait for connections and then take whatever Packet I receive first on the given port.
However, the sent packages never arrive.
Some code:
Player (Client) Side:
sf::Packet pack2;
//....
sf::SocketUDP udp_sock;
if(udp_sock.Send(pack2, server_ip, 3456)==sf::Socket::Done)
{
std::cout << std::endl << "sent UDP Button " << clickpos << " to " << server_ip << ":3456" << std::endl;
}
else
{
std::cout << std::endl << "Sending of UDP Button failed" << std::endl;
}
Server Side:
sf::SocketUDP udp_sock;
if(!udp_sock.Bind(3456))
{
std::cout << std::endl << "Error binding UDP Socket to port " << udp_sock.GetPort() << std::endl;
}
else
{
std::cout<<std::endl<<"Bound Socket to port 3456" <<std::endl;
}
//.....
sf::Packet pack;
sf::IPAddress addy;
unsigned short porty;
if(udp_sock.Receive(pack,addy,porty)==sf::Socket::Done)
{
std::cout << std::endl << "Received UDP Button From " << addy << ":" << port << " with " << pack.GetDataSize() << "B" <<std::endl;
}
else
{
std::cout << std::endl << "Receiving of UDP Button Failed" << std::endl;
}
I tried using TCP Sockets instead of UDP Sockets at those points (simply sending/receiving to/from player 1 only) and it worked fine.
As I said at the beginning, the the Client/Server side are currently simply two different threads (like a listening Server) for testing purposes.
If anybody has an idea why this doesn't work, it'd be greatly appreciated
I suppose the error could lie elsewhere in the code, but it's >15k lines as of now, so if there are some "standard" errors I could look for, it'd make my life much easier...
*edit*
Thinking that maybe I didn't receive anything because the Client Thread sent the packet before I was waiting for it in the Server Thread, I put the Send() command in an infinite loop, but still nothing arrived.
Thanks