Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // example of threaded receive / sender on either TCP or UDP
- //
- // sudo apt-get install libsfml-dev
- // to make master ::
- // g++-8 -Wconversion -std=c++11 -c client_socket.cpp -lpthread -lsfml-graphics -lsfml-window -lsfml-system -lsfml-network
- // g++-8 -std=c++11 client_socket.o -o sfml-app-m -lsfml-graphics -lsfml-window -lsfml-system -lsfml-network -lpthread
- // for slave :: comment out SFML_MASTER
- // g++-8 -Wconversion -std=c++11 -c client_socket.cpp -lpthread -lsfml-graphics -lsfml-window -lsfml-system -lsfml-network
- // g++-8 -std=c++11 client_socket.o -o sfml-app-s -lsfml-graphics -lsfml-window -lsfml-system -lsfml-network -lpthread
- //
- #include<SFML/Network.hpp>
- #include<iostream>
- #include<string>
- #include<thread>
- using namespace std;
- using namespace sf;
- /* uncomment if you want UDP
- #define ON_UDP
- */
- #ifndef SFML_BLOCKING
- #define SFML_BLOCKING false // no blocking socket
- #else
- #define SFML_BLOCKING true // blocking socket
- #endif
- #define SFML_MASTER // comment this out for slave
- #if defined(ON_UDP)
- void Send() //Send message.
- {
- while (true)
- {
- UdpSocket socket; //define UDP socket for transmit
- char message[250]; Packet packet;
- cin.getline(message, 250); //get keyboard message
- packet << message;
- #if defined(SFML_MASTER)
- socket.send(packet, "127.0.0.1", 2001);
- #else
- socket.send(packet, "127.0.0.1", 2002);
- #endif
- }
- }
- void Receive() // receive message
- {
- while (true)
- {
- #if defined(SFML_MASTER)
- UdpSocket socket; socket.bind(2002); //define UDP socket for receive
- #else
- UdpSocket socket; socket.bind(2001); //define UDP socket for receive
- #endif
- IpAddress sender; unsigned short port; string message; Packet packet; // Get a message from someone.
- socket.setBlocking(SFML_BLOCKING);
- socket.receive(packet, sender, port);
- packet >> message;
- cout << message << endl;
- }
- }
- //------- Protocol Udp ----------------+
- int main()
- {
- cout << "UDP messanger" << endl; cout << "--------" << endl;
- thread receive(Receive); thread send(Send); //Call 2 functions each in a separate thread
- receive.join(); send.join(); // Wait for the threads to finish working.
- size_t z; cin >> z; return 0;
- }
- #else /* on TCP */
- #include<SFML/Network.hpp>
- #include<iostream>
- #include<string>
- #include<thread>
- using namespace std;
- using namespace sf;
- void Send() //Send operation
- {
- while (true)
- {
- TcpSocket socket; //define transmit socket
- #if defined(SFML_MASTER)
- socket.connect("127.0.0.1", 2001); // CLIENT :: connect to ip
- #else
- socket.connect("127.0.0.1", 2002); // CLIENT :: connect to ip
- #endif
- char message[250]; Packet packet;
- cin.getline(message, 250); // get keyboard message
- packet << message;
- socket.send(packet);
- packet.clear();
- }
- }
- void Receive() //receive operation
- {
- while (true)
- {
- TcpSocket socket; // Define receive socket
- socket.setBlocking(SFML_BLOCKING);
- #if defined(SFML_MASTER)
- TcpListener listener; listener.listen(2002); // Server :: Create a listener socket, assign a port to it, and go into standby mode.
- #else
- TcpListener listener; listener.listen(2001); // Server :: Create a listener socket, assign a port to it, and go into standby mode.
- #endif
- listener.accept(socket); // Establish a connection with the subscriber, if he sent the request.
- string message; Packet packet; // Get a message from the caller.
- socket.receive(packet);
- packet >> message;
- cout << message << endl;
- }
- }
- //------- Protocol Tcp ----------------+
- int main()
- {
- cout << "TCP messager" << endl; cout << "---------------" << endl;
- thread receive(Receive); thread send(Send);
- receive.join(); send.join();
- size_t z; cin >> z; return 0;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement