Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Non-blocking UDP sockets intermittent errors help  (Read 3015 times)

0 Members and 1 Guest are viewing this topic.

_Fiction_

  • Newbie
  • *
  • Posts: 35
    • View Profile
Non-blocking UDP sockets intermittent errors help
« on: June 07, 2011, 11:49:30 pm »
I am writing a game for which I decided to use sfml's networking library to do the sending and receiving. the game runs just fine sometimes for around a minute and other times only a few seconds before failing. Ocassionally one of the sockets will get error status 3 forever, meaning something broke? The other half of the time both sides of the equation think they are sending and receiving just fine, but the sent data is no longer being received by the socket on one side or the other. Here is a sample of what I am doing:

( there are some member variables in the class that uses this loop. they do what their names suggest )

SERVER:

     
Code: [Select]
  unsigned short por = 5000;

float currentTime = m_clock.GetElapsedTime();

if ( previousTime == 0 || currentTime - previousTime > 1.0f/ 30 )
{
previousTime = currentTime;
if( !m_packetQueue.empty() )
{
sf::Packet p;
p = m_packetQueue.front();
m_packetQueue.pop();
while( !m_packetQueue.empty()  && p.GetDataSize() +  
                                  m_packetQueue.front().GetDataSize() <= 512 )
{
sf::Packet & temp = m_packetQueue.front();
p.Append( temp.GetData(), temp.GetDataSize() );
m_packetQueue.pop();
}
m_sendSocket.Send( p, m_address, por );
}
}

        sf::Packet receive;
unsigned short port = 5001;
sf::Socket::Status stat;

while( !( stat = m_receiveSocket.Receive( receive, m_address, port )))
{
receive >> m_input;
}


CLIENT

Code: [Select]


unsigned short port = 5000;
unsigned short port2 = 5001;
float currentTime = m_clock.GetElapsedTime();

if ( currentTime - previousTime > 1.0f/30 )
{
sf::Packet p;
p << m_view->GetInput();
m_sendSocket.Send( p, m_serverAddress, port2 );
previousTime = currentTime;
}

sf::Socket::Status status;
sf::Packet received;
sf::Packet temp;

while( !(status = m_receiveSocket.Receive( temp, m_serverAddress,  
                  port ) ) )
{
received.Append( temp.GetData(), temp.GetDataSize());
}
if ( received.GetDataSize() > 0 )
{ ProcessPacket( received );}


I hope nothing is wrong with the code, I had to edit it a bit to put it up here so I hope I didn't delete anything I didn't need to. Can anyone tell me why this intermittently fails?