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

Author Topic: UDP Receive loop problem  (Read 2098 times)

0 Members and 2 Guests are viewing this topic.

tluszcz

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
UDP Receive loop problem
« on: October 29, 2015, 10:49:13 pm »
Hello everyone :) I've barely started to learn how things in Network library are working and while trying to make a simple send-receive application I've encountered a problem.

This is the code I'm dealing with:
#include <SFML/Network.hpp>
#include <iostream>

sf::UdpSocket socket;

std::string msg;

int main() {
    if (socket.bind(1500) != sf::Socket::Done) exit (1);
    std::cout << "Waiting for message...\n";
    sf::IpAddress sender;
    unsigned short psender;
    while (1==1) {
        sf::Packet packet;
        socket.receive(packet, sender, psender);
        if (packet >> msg) {
            std::cout << msg;
        }
    }

    return 0;
}

Everything was fine when there was no loop, but I want to receive more than one packet :) I've seen the example chat application and it's using thread to manage sending and receiving. Should I do the same and it will work? Or is there something in my code that I don't see :(

EDIT: So I've tried to edit my code, done the receiver in another thread and in the main loop I just wait to type "exit". The messages are received properly, but they print after I press enter or write something. I get a feeling that I'm missing something, but looking the example (https://github.com/SFML/SFML/wiki/Source:-Network-Chat-Example) there's nothing I can think of :( Here's the code as well: http://pastebin.com/FBvGNVyZ

EDIT 2: Could it be because there's no std::endl at the end of std::cout while sending message and it has something to do with buffer? I can't check it right now and I'm dying inside a little :(

EDIT 3: Yes, it was it. Just needed to add the eol and it's working properly now :D
« Last Edit: October 31, 2015, 04:59:17 pm by tluszcz »