• Advertisement
Sign in to follow this  

SFML Networking

This topic is 2260 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I'm trying to make an air-hockey game, which can be played over the net 1 vs 1, like the title says I'm using SFML.
I have never done network programming before an I have some issues.
First of all I use TCP, so will it be too slow, because I need to send data constantly(position, velocity).

I have a problem with my current code, I have two classes SendData and ReceivedData, for now I just send x and y position of the player, and I want the other running instance of my game(client), to receive that info...here's some relevant code:


SendData.cpp


void SendData::listenOnPort(unsigned short port)
{
if(!socket.Listen(port))
std::cout<<"Error: socket not listening on port: "<<port<<std::endl;
}

void SendData::acceptConnection()
{
sf::IPAddress ip;

if(socket.Accept(tcp,&ip) != sf::Socket::Done)
std::cout<<"Error: connection not accepted"<<std::endl;
}


void SendData::sendPackage(int pos_x, int pos_y)
{

sf::Packet pac;

x = pos_x;
y = pos_y;

pac << x << y;

if(tcp.Send(pac) != sf::Socket::Done)
{
std::cout<<"Error: packet not sent"<<std::endl;
}
else
std::cout<<"Packet is sent"<<std::endl;

}





ReceivedData.cpp


ReceivedData::ReceivedData()
{
unsigned short s = 4444;

sf::IPAddress ip("localhost");

socket.Connect(s,ip);

}


void ReceivedData::getPackage()
{
sf::Packet packet;

if(socket.Receive(packet) != sf::Socket::Done)
std::cout<<"Packet not received"<<std::endl;
else
std::cout<<"Packet is received"<<std::endl;

packet >> x >> y;

}




I use i something like this:

SendData server;
ReceivedData client;

//the program will send data

if(answer == "server")
{
server.listenOnPort(4444);

server.acceptConnection();
}



in the loop:


if(answer == "server")
{
server.sendPackage(player.x, player.y);
}

if(answer == "client")
{
client.getPackage();
}



The problem is that this will only sent the first position and not changing, for instance if my server player position is x = 10, y = 10, the client will receive that info, but when i change the position on my server side the client still receives x = 10, y = 10, it not updated.

I also tested it like this:



void SendData::sendPackage()
{

sf::Packet pac;

x = 5;
y = 6;

x++;
y++;

pac << x << y;

if(tcp.Send(pac) != sf::Socket::Done)
{
std::cout<<"Error: packet not sent"<<std::endl;
}
else
std::cout<<"Packet is sent"<<std::endl;

}



The server constantly receives x = 6, x = 7, why isn't it changing(it's in the loop)?

Share this post


Link to post
Share on other sites
Advertisement


void SendData::sendPackage()
{

sf::Packet pac;

x = 5;
y = 6;

x++;
y++;

pac << x << y;

if(tcp.Send(pac) != sf::Socket::Done)
{
std::cout<<"Error: packet not sent"<<std::endl;
}
else
std::cout<<"Packet is sent"<<std::endl;

}



The server constantly receives x = 6, x = 7, why isn't it changing(it's in the loop)?


Each time you call sendPackage(), you first assign 5 to x, and then you increment x by one. This means that each time you put x into the pac variable, it will have the value 6.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement