Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Window.hpp>
- #include <SFML/Graphics.hpp>
- #include <SFML/Network.hpp>
- #include <string>
- #include <iostream>
- #include <windows.h>
- #include <ctime>
- #include <stdlib.h>
- using namespace sf;
- using namespace std;
- int main()
- {
- sf::IpAddress ip = sf::IpAddress::getLocalAddress();
- sf::TcpSocket socket;
- char connectionType, mode;
- char buffer[2000];
- size_t received;
- std::string text = "Connected to: ";
- cout<<"Enter (s) for Services, Enter (c) for client: "<<endl;
- cin>>connectionType;
- if(connectionType == 's')
- {
- TcpListener listener;
- listener.listen(2000);
- text+="Serwer";
- listener.accept(socket);
- mode = 's';
- }
- else if(connectionType == 'c')
- {
- socket.connect(ip, 2000);
- text+="Client";
- mode = 'r';
- }
- socket.send(text.c_str(), text.length() + 1);
- socket.receive(buffer, sizeof(buffer),received);
- std::cout<< buffer<<endl;
- bool done = false;
- while(!done)
- {
- if(mode == 's')
- {
- std::getline(cin, text);
- socket.send(text.c_str(), text.length()+1 );
- mode = 'r';
- }
- else if(mode == 'r')
- {
- socket.receive(buffer, sizeof(buffer), received);
- if(received > 0)
- {
- cout<<"received: "<<buffer<<endl;
- mode = 's';
- }
- }
- }
- system("Pause");
- return 0 ;
- }
Add Comment
Please, Sign In to add comment