Advertisement
Godsend72

sfml network

Apr 18th, 2016
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <SFML/Network.hpp>
  3. #include <iostream>
  4. #include <thread>
  5. #include <mutex>
  6.  
  7. const unsigned short port = 5310;
  8.  
  9. sf::TcpSocket socket1;
  10. sf::TcpListener listenertcp;
  11. sf::Packet sendingpacket;
  12. sf::Packet receivingpacket;
  13.  
  14.  
  15. std::mutex mutex1;
  16. bool quit = false;
  17.  
  18. void listener()
  19. {
  20. listenertcp.listen(port);
  21. while(!quit)
  22. {
  23. if(listenertcp.accept(socket1) == sf::Socket::Done)
  24. {
  25. mutex1.lock();
  26. std::cout << "Client just connected " << socket1.getRemoteAddress() << std::endl; //appilcation might need to tell this client to connect to the remote ip
  27. mutex1.unlock();
  28. }
  29. }
  30. }
  31.  
  32. void receive(sf::Packet* receivepacket)
  33. {
  34. std::string outputr;
  35. while(!quit)
  36. {
  37. if(socket1.receive(*receivepacket) == sf::Socket::Done)
  38. {
  39. *receivepacket >> outputr;
  40. mutex1.lock();
  41. std::cout << outputr << std::endl;
  42. mutex1.unlock();
  43. }
  44. }
  45. }
  46.  
  47. int main()
  48. {
  49. std::string input;
  50. std::cout << "Enter /server or /client" << std::endl;
  51. std::cin >> input;
  52. if(input == "/server")
  53. {
  54. std::cout << "Server is listening on port " << port << std::endl;
  55. std::thread listen(listener);
  56. std::thread messagereceive(receive, &receivingpacket);
  57.  
  58. listen.join();
  59. messagereceive.join();
  60.  
  61. }
  62. else if(input == "/client")
  63. {
  64. std::cout << "input /connect to connect to a client." << std::endl;
  65. std::cout << "input /quit to quit the application." << std::endl;
  66. std::cout << "input /myip to get your ip address." << std::endl;
  67.  
  68. while(!quit)
  69. {
  70. std::string userinput;
  71. std::cin >> userinput;
  72.  
  73. if(userinput == "/connect")
  74. {
  75. std::string ip;
  76. std::cout << "Please input the ip address you'd like to connect to" << std::endl;
  77. std::cin >> ip;
  78. sf::Socket::Status status = socket1.connect(ip, port);
  79. if(status != sf::Socket::Done)
  80. {
  81. std::cout << "invalid ip address" << std::endl;
  82. }
  83. else
  84. {
  85. std::cout << "connected to client at " << ip << "address" << std::endl;
  86. }
  87. }
  88. else if(userinput == "/myip")
  89. {
  90. sf::IpAddress myip;
  91. std::string myipstring;
  92. myip = sf::IpAddress::getPublicAddress();
  93. myipstring = myip.toString();
  94. std::cout << myipstring << std::endl;
  95. }
  96. else if(userinput == "/quit")
  97. {
  98. quit = true;
  99. }
  100. else
  101. {
  102. sendingpacket << userinput;
  103. mutex1.lock();
  104. socket1.send(sendingpacket);
  105. mutex1.unlock();
  106.  
  107. }
  108. }
  109. }
  110.  
  111. return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement