Advertisement
Guest User

Untitled

a guest
Oct 30th, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <SFML/Network.hpp>
  2. #include <iostream>
  3.  
  4. sf::UdpSocket socket;
  5. sf::Socket::Status status;
  6. bool quit = false;
  7. int port = 1500;
  8. unsigned short psender;
  9. std::string com;
  10. std::string msg;
  11. sf::IpAddress sender;
  12. sf::Mutex globalMutex;
  13.  
  14.  
  15. void receivePackets(void) {
  16.     std::cout << "Listener thread started\n";
  17.  
  18.     while (!quit) {
  19.         sf::Packet packet;
  20.  
  21.         socket.receive(packet, sender, psender);
  22.  
  23.         globalMutex.lock();
  24.         packet >> msg;
  25.         globalMutex.unlock();
  26.  
  27.         std::cout << msg;
  28.  
  29.     }
  30. }
  31.  
  32. int main() {
  33.  
  34.     status = socket.bind(port);
  35.     if (status != sf::Socket::Done) {
  36.         std::cout << "Something went wrong\n";
  37.         exit(1);
  38.     }
  39.  
  40.     std::cout << "Waiting for messages at port " << port << "...\n";
  41.  
  42.     sf::Thread* thread = new sf::Thread(&receivePackets);
  43.     thread->launch();
  44.  
  45.     std::cout << "Main thread still working\n\nTo stop listening type 'exit'\n";
  46.  
  47.     while (!quit) {
  48.         std::cin >> com;
  49.         if (com == "exit") quit = true;
  50.     }
  51.  
  52.     return 0;
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement