Advertisement
aircampro

tcp/udp sender client and server

Jun 13th, 2021
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.54 KB | None | 0 0
  1. // example of threaded receive / sender on either TCP or UDP
  2. //
  3. // sudo apt-get install libsfml-dev
  4. // to make master ::
  5. // g++-8 -Wconversion -std=c++11 -c client_socket.cpp -lpthread -lsfml-graphics -lsfml-window -lsfml-system -lsfml-network
  6. // g++-8 -std=c++11 client_socket.o -o sfml-app-m -lsfml-graphics -lsfml-window -lsfml-system -lsfml-network -lpthread
  7. // for slave :: comment out SFML_MASTER
  8. // g++-8 -Wconversion -std=c++11 -c client_socket.cpp -lpthread -lsfml-graphics -lsfml-window -lsfml-system -lsfml-network
  9. // g++-8 -std=c++11 client_socket.o -o sfml-app-s -lsfml-graphics -lsfml-window -lsfml-system -lsfml-network -lpthread
  10. //
  11. #include<SFML/Network.hpp>
  12. #include<iostream>
  13. #include<string>
  14. #include<thread>
  15.  
  16. using namespace std;
  17. using namespace sf;
  18.  
  19. /* uncomment if you want UDP
  20. #define ON_UDP
  21. */
  22.  
  23. #ifndef SFML_BLOCKING
  24. #define SFML_BLOCKING false                                             // no blocking socket
  25. #else
  26. #define SFML_BLOCKING true                                              // blocking socket
  27. #endif
  28.  
  29. #define SFML_MASTER                                                     // comment this out for slave
  30.  
  31. #if defined(ON_UDP)
  32. void Send()                                                             //Send message.
  33. {
  34.     while (true)
  35.     {
  36.         UdpSocket socket;                                               //define UDP socket for transmit
  37.         char message[250]; Packet packet;
  38.  
  39.         cin.getline(message, 250);                                      //get keyboard message
  40.         packet << message;
  41. #if defined(SFML_MASTER)
  42.         socket.send(packet, "127.0.0.1", 2001);
  43. #else
  44.         socket.send(packet, "127.0.0.1", 2002);
  45. #endif
  46.  
  47.     }
  48. }
  49. void Receive()                                                          // receive message
  50. {
  51.     while (true)
  52.     {
  53. #if defined(SFML_MASTER)
  54.         UdpSocket socket; socket.bind(2002);                            //define UDP socket for receive
  55. #else
  56.         UdpSocket socket; socket.bind(2001);                            //define UDP socket for receive
  57. #endif
  58.         IpAddress sender; unsigned short port; string message; Packet packet;       // Get a message from someone.
  59.         socket.setBlocking(SFML_BLOCKING);
  60.         socket.receive(packet, sender, port);
  61.         packet >> message;
  62.         cout << message << endl;
  63.     }
  64. }
  65. //------- Protocol Udp  ----------------+
  66. int main()
  67. {
  68.     cout << "UDP messanger" << endl; cout << "--------" << endl;
  69.  
  70.     thread receive(Receive); thread send(Send);                         //Call 2 functions each in a separate thread
  71.  
  72.  
  73.     receive.join(); send.join();                                        // Wait for the threads to finish working.
  74.     size_t z; cin >> z; return 0;
  75. }
  76.  
  77. #else /* on TCP */
  78.  
  79. #include<SFML/Network.hpp>
  80. #include<iostream>
  81. #include<string>
  82. #include<thread>
  83.  
  84. using namespace std;
  85. using namespace sf;
  86.  
  87. void Send()                                                             //Send operation
  88. {
  89.     while (true)
  90.     {
  91.  
  92.         TcpSocket socket;                                               //define transmit socket
  93. #if defined(SFML_MASTER)
  94.         socket.connect("127.0.0.1", 2001);                              // CLIENT :: connect to ip
  95. #else
  96.         socket.connect("127.0.0.1", 2002);                              // CLIENT :: connect to ip
  97. #endif
  98.         char message[250]; Packet packet;
  99.  
  100.         cin.getline(message, 250);                                      //  get keyboard message
  101.         packet << message;
  102.         socket.send(packet);
  103.         packet.clear();
  104.     }
  105. }
  106. void Receive()                                                          //receive operation
  107. {
  108.     while (true)
  109.     {
  110.         TcpSocket socket;                                               // Define receive socket
  111.         socket.setBlocking(SFML_BLOCKING);
  112. #if defined(SFML_MASTER)
  113.         TcpListener listener; listener.listen(2002);                    // Server :: Create a listener socket, assign a port to it, and go into standby mode.
  114. #else
  115.         TcpListener listener; listener.listen(2001);                    // Server :: Create a listener socket, assign a port to it, and go into standby mode.
  116. #endif
  117.         listener.accept(socket);                                        // Establish a connection with the subscriber, if he sent the request.
  118.         string message; Packet packet;                                  // Get a message from the caller.
  119.  
  120.         socket.receive(packet);
  121.         packet >> message;
  122.         cout << message << endl;
  123.     }
  124. }
  125. //------- Protocol Tcp  ----------------+
  126. int main()
  127. {
  128.     cout << "TCP messager" << endl; cout << "---------------" << endl;
  129.     thread receive(Receive); thread send(Send);
  130.  
  131.     receive.join(); send.join();
  132.     size_t z; cin >> z; return 0;
  133. }
  134. #endif
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement