Guest User

Untitled

a guest
Apr 10th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include <SFML/Window.hpp>
  2. #include <SFML/Graphics.hpp>
  3. #include <SFML/Network.hpp>
  4. #include <string>
  5. #include <iostream>
  6. #include <windows.h>
  7. #include <ctime>
  8. #include <stdlib.h>
  9. using namespace sf;
  10. using namespace std;
  11.  
  12. int main()
  13. {
  14.     sf::IpAddress ip  = sf::IpAddress::getLocalAddress();
  15.     sf::TcpSocket socket;
  16.     char connectionType, mode;
  17.     char buffer[2000];
  18.     size_t received;
  19.     std::string text = "Connected to: ";
  20.  
  21.     cout<<"Enter (s) for Services, Enter (c) for client: "<<endl;
  22.     cin>>connectionType;
  23.     if(connectionType == 's')
  24.     {
  25.         TcpListener listener;
  26.         listener.listen(2000);
  27.         text+="Serwer";
  28.         listener.accept(socket);
  29.         mode = 's';
  30.  
  31.     }
  32.     else if(connectionType == 'c')
  33.     {
  34.         socket.connect(ip, 2000);
  35.         text+="Client";
  36.         mode = 'r';
  37.     }
  38.     socket.send(text.c_str(), text.length() + 1);
  39.     socket.receive(buffer, sizeof(buffer),received);
  40.     std::cout<< buffer<<endl;
  41.     bool done = false;
  42.     while(!done)
  43.     {
  44.         if(mode == 's')
  45.         {
  46.             std::getline(cin, text);
  47.             socket.send(text.c_str(), text.length()+1 );
  48.             mode = 'r';
  49.         }
  50.         else if(mode == 'r')
  51.         {
  52.             socket.receive(buffer, sizeof(buffer), received);
  53.             if(received > 0)
  54.             {
  55.             cout<<"received: "<<buffer<<endl;
  56.             mode = 's';
  57.             }
  58.  
  59.         }
  60.  
  61.     }
  62.  
  63.     system("Pause");
  64.     return 0 ;
  65. }
Add Comment
Please, Sign In to add comment