Hello, i recently started to learn SFML networking. I'm trying to create a client-server communication.
When i try to send a packet from the client, it says 'bad allocation'. I don't understand why i'm getting this error, thanks in advance.
Here's the code:
Before main:
class ClientT {
public:
int pp;
std::string name;
int frm;
private:
};
sf::Packet& operator << (sf::Packet& packet, const ClientT& character) {
return packet << character.name << character.pp << character.frm;
}
sf::Packet& operator >> (sf::Packet& packet, ClientT& character) {
return packet >> character.name >> character.pp >> character.frm;
}
On main:
std::vector<sf::TcpSocket*> clients;
char choice;
std::cin >> choice;
if (choice == 'S') {
sf::SocketSelector selector;
sf::TcpListener listener;
listener.listen(5000);
selector.add(listener);
while (1) {
if (selector.wait()) {
if (selector.isReady(listener)) {
sf::TcpSocket* socket = new sf::TcpSocket;
if (listener.accept(*socket) == sf::Socket::Done) {
std::vector<sf::TcpSocket*>::iterator it = clients.begin();
for (it; it != clients.end(); ++it) {
sf::TcpSocket* sockett = *it;
char in[128] = "A client connected!";
sockett->send(in, sizeof(in));
}
clients.push_back(socket);
selector.add(*socket);
}
else {
delete socket;
}
}
else {
std::vector<sf::TcpSocket*>::iterator it = clients.begin();
for (it; it != clients.end(); ++it) {
sf::TcpSocket* socket = *it;
}
}
}
}
}
else if (choice == 'C') {
sf::IpAddress address;
std::cout << "Enter the host's IP: ";
std::cin >> address;
sf::TcpSocket* socket = new sf::TcpSocket;
if (socket->connect(address, 5000) == sf::Socket::Done) {
std::cout << "Connected" << std::endl;
}
ClientT client;
client.name = "hoked12";
client.pp = 1;
client.frm = 0;
sf::Packet packet;
try {
packet << client;
}
catch (std::exception& e) {
std::cout << e.what() << std::endl;
}
// if (socket.send(packet) == sf::Socket::Done) std::cout << "Packet sent" << std::endl;
}