Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Network.hpp>
- #include <iostream>
- sf::UdpSocket socket;
- sf::Socket::Status status;
- bool quit = false;
- int port = 1500;
- unsigned short psender;
- std::string com;
- std::string msg;
- sf::IpAddress sender;
- sf::Mutex globalMutex;
- void receivePackets(void) {
- std::cout << "Listener thread started\n";
- while (!quit) {
- sf::Packet packet;
- socket.receive(packet, sender, psender);
- globalMutex.lock();
- packet >> msg;
- globalMutex.unlock();
- std::cout << msg;
- }
- }
- int main() {
- status = socket.bind(port);
- if (status != sf::Socket::Done) {
- std::cout << "Something went wrong\n";
- exit(1);
- }
- std::cout << "Waiting for messages at port " << port << "...\n";
- sf::Thread* thread = new sf::Thread(&receivePackets);
- thread->launch();
- std::cout << "Main thread still working\n\nTo stop listening type 'exit'\n";
- while (!quit) {
- std::cin >> com;
- if (com == "exit") quit = true;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement