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

Author Topic: Can someone cook up a small example of udp sockets with packets?  (Read 7460 times)

0 Members and 1 Guest are viewing this topic.

Jim70

  • Newbie
  • *
  • Posts: 23
    • View Profile
The documentation isn't very clear with this, so can someone cook up an example?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Can someone cook up a small example of udp sockets with packets?
« Reply #1 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.
Laurent Gomila - SFML developer

Jim70

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Can someone cook up a small example of udp sockets with packets?
« Reply #2 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);
      }
« Last Edit: September 22, 2015, 12:14:10 pm by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Can someone cook up a small example of udp sockets with packets?
« Reply #3 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.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Can someone cook up a small example of udp sockets with packets?
« Reply #4 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jim70

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Can someone cook up a small example of udp sockets with packets?
« Reply #5 on: September 22, 2015, 01:28:46 pm »
No, but what will I have to change to use udp?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Can someone cook up a small example of udp sockets with packets?
« Reply #6 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jim70

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Can someone cook up a small example of udp sockets with packets?
« Reply #7 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.
« Last Edit: September 22, 2015, 02:47:22 pm by eXpl0it3r »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Can someone cook up a small example of udp sockets with packets?
« Reply #8 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.
Laurent Gomila - SFML developer

Jim70

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Can someone cook up a small example of udp sockets with packets?
« Reply #9 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 :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Can someone cook up a small example of udp sockets with packets?
« Reply #10 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.
Laurent Gomila - SFML developer

Jim70

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Can someone cook up a small example of udp sockets with packets?
« Reply #11 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?

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Can someone cook up a small example of udp sockets with packets?
« Reply #12 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.
« Last Edit: September 22, 2015, 06:19:18 pm by Jesper Juhl »