SFML community forums

Help => Network => Topic started by: Jim70 on September 22, 2015, 10:45:02 am

Title: Can someone cook up a small example of udp sockets with packets?
Post by: Jim70 on September 22, 2015, 10:45:02 am
The documentation isn't very clear with this, so can someone cook up an example?
Title: Re: Can someone cook up a small example of udp sockets with packets?
Post by: Laurent on September 22, 2015, 11:37:38 am
What don't you understand? It's really trivial, and not more complicated than with TCP sockets. There are examples in the documentation, tutorials and example applications.
Title: Re: Can someone cook up a small example of udp sockets with packets?
Post by: Jim70 on September 22, 2015, 11:45:30 am
What don't you understand? It's really trivial, and not more complicated than with TCP sockets. There are examples in the documentation, tutorials and example applications.

Then what do I need to change in this code?
       
        pre-window:
       
        sf::TcpSocket socket;
   char connectionType;

   cout << "Enter ip you wish to host/connect to." << endl;
   string tmp;

   cin >> tmp;

   sf::IpAddress ip(tmp);

   cout << "Port: ";
   int port;
   cin >> port;

   cout << "(s) for server and (c) for client: ";
   cin >> connectionType;

   if (connectionType == 's') {
      cout << "Waiting for someone to join..." << endl;
      sf::TcpListener listener;
      listener.listen(port);
      listener.accept(socket);
   }
   else {
      cout << "connecting..." << endl;
      socket.connect(ip, port);
   }
       
        ........

        game loop:
       
       
sf::Packet packet;

      if (prevPosition != rect1.getPosition() | prevRot != rot) {
         packet << rect1.getPosition().x << rect1.getPosition().y << rot;
         socket.send(packet);
      }

      socket.receive(packet);
      if (packet >> p2Position.x >> p2Position.y >> rot2) {
         rect2.setPosition(p2Position);
      }
Title: Re: Can someone cook up a small example of udp sockets with packets?
Post by: Laurent on September 22, 2015, 12:15:46 pm
So what exactly is wrong with this code? Please describe your problem precisely, we shouldn't have to ask all the details.

And these are TCP sockets, not UDP.
Title: Can someone cook up a small example of udp sockets with packets?
Post by: eXpl0it3r on September 22, 2015, 12:16:44 pm
And where's the UDP socket?

Also next time don't open two threads for the same topic.
Title: Re: Can someone cook up a small example of udp sockets with packets?
Post by: Jim70 on September 22, 2015, 01:28:46 pm
No, but what will I have to change to use udp?
Title: Re: Can someone cook up a small example of udp sockets with packets?
Post by: eXpl0it3r on September 22, 2015, 01:30:51 pm
Do you even understand what UDP is and how it works?
UDP is an unreliable protocol. You don't get any guarantees regarding to packet ordering or even receiving.
Title: Re: Can someone cook up a small example of udp sockets with packets?
Post by: Jim70 on September 22, 2015, 02:02:56 pm
Do you even understand what UDP is and how it works?
UDP is an unreliable protocol. You don't get any guarantees regarding to packet ordering or even receiving.

I understand the differences, tcp is more reliable but slower, but udp spits everything out as fast as possible.
witch makes it better for games (as far as ive heard) and I need minimum delay/lag.
There is a decent amount of good stuff on the interwebs about tcp and sockets, but not udp and sockets.
Title: Re: Can someone cook up a small example of udp sockets with packets?
Post by: Laurent on September 22, 2015, 02:05:53 pm
So you haven't even tried anything with sf::UdpSocket yet? I'd say start something first, the documentation, tutorials and examples should be more than enough to get you started. And if you get stuck on a specific point, come back and we'll be glad to solve your problem.
Title: Re: Can someone cook up a small example of udp sockets with packets?
Post by: Jim70 on September 22, 2015, 02:14:42 pm
So you haven't even tried anything with sf::UdpSocket yet? I'd say start something first, the documentation, tutorials and examples should be more than enough to get you started. And if you get stuck on a specific point, come back and we'll be glad to solve your problem.

*There is a decent amount of good stuff on the interwebs about tcp and PACKETS, but not udp and PACKETS.

Sorry for the confusion :)
Title: Re: Can someone cook up a small example of udp sockets with packets?
Post by: Laurent on September 22, 2015, 02:33:28 pm
http://www.sfml-dev.org/tutorials/2.3/network-packet.php#packets
http://www.sfml-dev.org/documentation/2.3.2/classsf_1_1UdpSocket.php#a48969a62c80d40fd74293a740798e435

Really, if you know how to do it with TCP, or with UDP and raw data, this is really trivial to adapt... So just write the code and give it a try, instead of writing forum posts :P

Anyway, you probbaly won't get any help until you show us some code and get stuck on a something specific. We won't write the code for you.
Title: Re: Can someone cook up a small example of udp sockets with packets?
Post by: Jim70 on September 22, 2015, 04:58:11 pm
http://www.sfml-dev.org/tutorials/2.3/network-packet.php#packets
http://www.sfml-dev.org/documentation/2.3.2/classsf_1_1UdpSocket.php#a48969a62c80d40fd74293a740798e435

Really, if you know how to do it with TCP, or with UDP and raw data, this is really trivial to adapt... So just write the code and give it a try, instead of writing forum posts :P

Anyway, you probbaly won't get any help until you show us some code and get stuck on a something specific. We won't write the code for you.

welp, ill give it a toss and see how it goes. But one last question, does tcp's lag scale with how much data you send? and at what percentage?
Title: Re: Can someone cook up a small example of udp sockets with packets?
Post by: Jesper Juhl on September 22, 2015, 05:44:29 pm
The lag you get with TCP is (mostly) due to having to retransmit when packets are lost. More lost packages - more delay.
UDP on the other hand just loses the package and you never get the data.