Untitled
user_1601351
plain_text
2 years ago
1.2 kB
13
Indexable
//network.h
#ifndef NETWORK_H
#define NETWORK_H
#include <SFML/Network.hpp>
#include <string>
class Network {
public:
Network();
~Network();
bool createServer(int port);
bool connectToServer(const std::string& ipAddress, int port);
bool sendData(const std::string& data);
bool receiveData(std::string& data);
private:
sf::TcpSocket socket;
};
#endif
//network.cpp
#include "network.h"
Network::Network() {
//
socket.setBlocking(false);
}
Network::~Network() {
//
socket.disconnect();
}
bool Network::createServer(int port) {
sf::TcpListener listener;
if (listener.listen(port) != sf::Socket::Done) {
return false;
}
if (listener.accept(socket) != sf::Socket::Done) {
return false;
}
return true;
}
bool Network::connectToServer(const std::string& ipAddress, int port) {
if (socket.connect(ipAddress, port) != sf::Socket::Done) {
return false;
}
return true;
}
bool Network::sendData(const std::string& data) {
sf::Packet packet;
packet << data;
if (socket.send(packet) != sf::Socket::Done) {
return false;
}
return true;
}
bool Network::receiveData(std::string& data) {
sf::Packet packet;
if (socket.receive(packet) != sf::Socket::Done) {
return false;
}
packet >> data;
return true;
}
///Editor is loading...