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

Author Topic: Problem with UDP Sockets  (Read 6469 times)

0 Members and 1 Guest are viewing this topic.

Lorizean

  • Newbie
  • *
  • Posts: 14
    • View Profile
Problem with UDP Sockets
« on: January 24, 2011, 06:25:12 pm »
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:
Code: [Select]

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:
Code: [Select]

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

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
Problem with UDP Sockets
« Reply #1 on: January 24, 2011, 11:38:47 pm »
You can do this using TCP sockets and a Socket Selector. It's all on the networking tutorial on the SFML site.

Lorizean

  • Newbie
  • *
  • Posts: 14
    • View Profile
Problem with UDP Sockets
« Reply #2 on: January 24, 2011, 11:46:47 pm »
You're right, I didn't think of that, very new to SFML. Thanks!
Still, would be nice to know why it doesn't work with the UDP Sockets.

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
Problem with UDP Sockets
« Reply #3 on: January 24, 2011, 11:52:11 pm »
Sorry, can't help you with that. Never needed to use them before. :wink:

D

  • Newbie
  • *
  • Posts: 12
    • View Profile
Problem with UDP Sockets
« Reply #4 on: January 25, 2011, 01:11:20 am »
A thing that caught my eye was that you declare an integer called porty, and use it without initializing it. Dunno if that's the problem though.

Lorizean

  • Newbie
  • *
  • Posts: 14
    • View Profile
Problem with UDP Sockets
« Reply #5 on: January 25, 2011, 01:21:24 am »
I thought that wouldn't be a problem, because as far as I understand, the port that is called in SocketUDP::Receive is simply filled with the port of the incoming connection.
Is that correct?

D

  • Newbie
  • *
  • Posts: 12
    • View Profile
Problem with UDP Sockets
« Reply #6 on: January 25, 2011, 10:00:57 am »
No, I think that's not correct. According to the API you need to provide the port on which the remote computer sent the data.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problem with UDP Sockets
« Reply #7 on: January 25, 2011, 10:24:13 am »
No, Lorizean is right, the last two parameters are filled by the function.

Where exactly does the program fails? Is it when receiving? When extracting from the packet?

Have you tried the "sockets" sample in UDP mode?
Laurent Gomila - SFML developer

Lorizean

  • Newbie
  • *
  • Posts: 14
    • View Profile
Problem with UDP Sockets
« Reply #8 on: January 25, 2011, 01:21:55 pm »
Generally, the Receiving and Sending using UDP Sockets works. I added a test send/receive to the beginning of the Threads and it all worked fine.

The Problem seems to be that nothing is received at all, i.e. no packet ever arrives, the function never goes past the Receive(), since it's in blocking mode. It doesn't matter how often I send the packet.
I've got it working using Selectors now, but as I said, would still be nice to know why it didn't work using UDP.
Normally I would assume it's an error somewhere completely unrelated in the program logic (like maybe filling/receiving the packets wrongly, binding wrong ports etc.), but since it works when I simply exchange UDP with TCP and leave everything else exactly the same, that does not seem to be the case.
Unless there arises a problem if I keep a TCP and a UDP Socket open at the same time? (using different ports)