Hey people. I just started learning networking today and I've been reading all day so my brain is a bit fried at the moment. I made a server and client with SFML. I can successfully connect to the server and send packets to the server, but when I put a receive function inside the client it crashes the client. What am I doing wrong here?
SERVER
#include <SFML/Network.hpp>
#include <iostream>
#include <string>
#include <list>
int main()
{
sf::TcpListener Listener;
Listener.listen(55001);
std::list<sf::TcpSocket*> Clients;
sf::SocketSelector Selector;
Selector.add(Listener);
while (1)
{
if (Selector.wait())
{
if (Selector.isReady(Listener))
{
sf::TcpSocket* ptrClient = new sf::TcpSocket;
if (Listener.accept(*ptrClient) == sf::Socket::Done)
{
Clients.push_back(ptrClient);
Selector.add(*ptrClient);
std::cout << "New client connected: " << ptrClient->getRemoteAddress() << std::endl;
// Receive a message from the client
char buffer[1024];
std::size_t received = 0;
ptrClient->receive(buffer, sizeof(buffer), received);
std::cout << "The client said: " << buffer << std::endl;
// Send an answer
std::string message = "Welcome, client";
ptrClient->send(message.c_str(), message.size() + 1);
}
else
{
// Error, we won't get a new connection, delete the socket
delete ptrClient;
}
}
else
{
// The listener socket is not ready, test all other sockets (the clients)
for (std::list<sf::TcpSocket*>::iterator it = Clients.begin(); it != Clients.end(); ++it)
{
sf::TcpSocket& client = **it;
if (Selector.isReady(client))
{
sf::Packet packetReceived;
if (client.receive(packetReceived) == sf::Socket::Done)
{
// Receive
std::string strReceived;
if (packetReceived >> strReceived) { std::cout << strReceived << std::endl; }
// Send
std::string strSent = "I heard you.";
sf::Packet packetSent;
packetSent << strSent;
client.send(packetSent);
}
}
}
}
}
}
return 0;
}
CLIENT
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>
#include <string>
int main()
{
sf::RenderWindow WINDOW(sf::VideoMode(800, 450), "SFML works!");
WINDOW.setFramerateLimit(20);
sf::TcpSocket Socket;
Socket.connect("localhost", 55001);
// Send a message to the connected host
std::string message = "Hi, I am a client";
Socket.send(message.c_str(), message.size() + 1);
// Receive an answer from the server
char buffer[1024];
std::size_t received = 0;
Socket.receive(buffer, sizeof(buffer), received);
std::cout << "The server said: " << buffer << std::endl;
while (WINDOW.isOpen())
{
sf::Event EVENT;
while (WINDOW.pollEvent(EVENT))
{
switch (EVENT.type)
{
case sf::Event::Closed: WINDOW.close(); break;
case sf::Event::KeyPressed:
if (EVENT.key.code == sf::Keyboard::Space)
{
std::string strSent = "I pressed Space.";
sf::Packet packetSent;
packetSent << strSent;
Socket.send(packetSent);
}
break;
}
}
sf::Packet packetReceived;
// This part crashes the program:
if (Socket.receive(packetReceived) == sf::Socket::Done)
{
std::string strReceived;
if (packetReceived >> strReceived) { std::cout << strReceived << std::endl; }
}
WINDOW.clear();
WINDOW.display();
}
return 0;
}