Hey guys, I am making a simple game using SFML. Currently I've just got 2 rectangle shape players that move about. I'm trying to send UDP packets that contain 2 floats that store a client's X and Y positions to a server, which then forwards that very same packet to the other client. I've done a fair bit of debugging, and as far as I can tell, the values in the packet are correct up until the point that they are actually received by a client where, when extracted, the values are 0 and 0. I'm not really sure how to go about fixing this, as I am fairly new to SFML and network programming. I'll post the code that I think is relevant below
server main
//Used to send Player positions between clients
struct playerData
{
sf::Vector2f Position;
};
//Used to store ClientIDs, as well ass their addresses and the ports the client sockets are bound to
struct clientData
{
sf::IpAddress address;
unsigned short port;
sf::Uint16 ID;
};
int main()
{
clientData clients[2];
sf::UdpSocket socket;
sf::Uint16 SocketID = 0;
sf::Packet packet;
sf::Packet receivedPacket;
packet << SocketID;
bool keepRunning = true;
sf::IpAddress sender;
unsigned short port;
unsigned short ClientPort;
//bind the listener to a port;
if(socket.bind(4444) == sf::Socket::Done)
{
//Wait for 2 clients to connect
for(int i = 0; i < 2; i++)
{
//When a client connects, it should send a packet containing the port it is bound to
//Server stores this in the ClientData struct, as well as the address it received from
//and the Client's ID
if(socket.receive(packet, sender, port) == sf::Socket::Done)
{
packet >> ClientPort;
clients[i].address = sender;
clients[i].port = ClientPort;
clients[i].ID = SocketID;
SocketID += 1;
packet.clear();
}
}
//Send back the Client's ID
for(int i = 0; i < 2; i++)
{
packet << clients[i].ID;
socket.send(packet, clients[i].address, clients[i].port);
packet.clear();
}
while(keepRunning == true)
{
//Receive a packet containing a client's position in the game world
//Then send that same packet to the other client
if(socket.receive(packet, clients[1].address, clients[1].port) == sf::Socket::Done)
{
socket.send(packet, clients[0].address, clients[0].port);
cout<<"Sent info to player 2"<<endl;
packet.clear();
}
if(socket.receive(packet, clients[0].address, clients[0].port) == sf::Socket::Done)
{
socket.send(packet, clients[1].address, clients[1].port);
cout<<"Sent info to player 1"<<endl;
packet.clear();
}
}
Client class functions:
//Binds the client to a free port, and then stores that port to be sent to the server to be stored
void client::Bind()
{
bind(myPort);
myPort = getLocalPort();
}
//sends the client port to server, and in return gets its ID
Uint16 client::getID()
{
packet << myPort;
std::cout<<myPort<<std::endl;
send(packet, serverAddress, port);
while(gotID == false)
{
std::cout<<"waiting..."<<std::endl;
if(receive(receivedPacket, serverAddress, port) == sf::Socket::Done)
{
receivedPacket>>receivedID;
gotID = true;
}
}
setBlocking(false);
return receivedID;
}
void client::SendPlayerData(Packet packet)
{
send(packet, serverAddress, port);
}
bool client::ReceivePlayerData(Packet packet)
{
if(receive(packet, serverAddress, port) == sf::Socket::Done)
return true;
}
Client main:
sf::Uint32 OtherX;
sf::Uint32 OtherY;
//used tp send player's position to server
sf::Uint32 Xposition;
sf::Uint32 Yposition;
int main()
{
client Client;
player Players[2];
Players[0].setFillColor(sf::Color::Red);
Players[1].setFillColor(sf::Color::Blue);
Players[0].setPosition(200.f, 400.f);
Players[1].setPosition(300.f, 300.f);
sf::Packet packet;
sf::Packet Receivedpacket;
Client.Bind();
Client.getID();
cout<<"gotID"<< Client.receivedID<<endl;
sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
// run the program as long as the window is open
while (window.isOpen())
{
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
}
//make sure only this client's player can be controlled by this keyboard
Players[Client.receivedID].Movement(Client.receivedID);
cout<<"My ID: "<< Client.receivedID<<endl;
//Ready this player's data to be sent to the server
Xposition = Players[Client.receivedID].getPosition().x;
Yposition = Players[Client.receivedID].getPosition().y;
packet << Xposition <<Yposition;
Client.SendPlayerData(packet);
packet.clear();
//Receive information about other player
if(Client.ReceivePlayerData(Receivedpacket))
{
Receivedpacket >> OtherX >> OtherY;
}
if(Client.receivedID == 1)
Players[0].setPosition(OtherX, OtherY);
else if(Client.receivedID == 0)
Players[1].setPosition(OtherX, OtherY);
Receivedpacket.clear();
}
// clear the window with black color
window.clear(sf::Color::Black);
// draw everything here...
for(int i = 0; i < 2; i++)
window.draw(Players[i]);
// end the current frame
window.display();
}
return 0;
}
Like I said, I'm pretty to new to networking and SFML, so any help at all here is really appreciated
Thank you!
P.S. I apologise for the wall of code