SFML community forums

Help => Network => Topic started by: dntjdals0513 on May 11, 2014, 01:04:23 pm

Title: Server does not receive packets
Post by: dntjdals0513 on May 11, 2014, 01:04:23 pm
client has successfully connected to server,
sends "Hi, I'm client" to the server without error.

But the server does not receive the packet.

What the problem is?

Server source using socket selector :
#include "SFML/Network.hpp"
#include <iostream>
#include <vector>
#include <list>

int main(){

       
        sf::TcpListener listener;
        listener.listen(55001);
       
        std::list<sf::TcpSocket*> clients;
       
        sf::SocketSelector selector;
       
        selector.add(listener);
       

        bool running = true;

        std::cout << "server started" << std::endl;

        while (running)
        {
               
                if (selector.wait())
                {
                       
                        if (selector.isReady(listener))
                        {
                               
                                sf::TcpSocket* client = new sf::TcpSocket;
                                if (listener.accept(*client) == sf::Socket::Done)
                                {
                                        std::cout << "new connection received from " << client->getRemoteAddress() << std::endl;

                                        sf::Packet receivedPacket;
                                        if (client->receive(receivedPacket) == sf::Socket::Done)
                                        {
                                                std::string message;
                                                receivedPacket >> message;
                                                std::cout << message << std::endl;

                                        }

                                       
                                        clients.push_back(client);
                                       
                                       
                                        selector.add(*client);
                                }
                                else
                                {
                                       
                                        std::cout << "error : server has no connection" << std::endl;
                                        delete client;
                                }
                        }
                        else
                        {
                               
                                for (std::list<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end(); ++it)
                                {
                                        sf::TcpSocket& client = **it;
                                        if (selector.isReady(client))
                                        {
                                               
                                                sf::Packet packet;

                                                switch (client.receive(packet))
                                                {

                                                        case sf::Socket::Done :
                                                                {
                                                                        //...
                                                                        std::string message;
                                                                        packet >> message;
                                                                        std::cout << client.getRemoteAddress() << " said: " << message << std::endl;

                                                                        break;
                                                                }
                                                        case sf::Socket::Disconnected :
                                                                {
                                                                        std::cout << "disconnect from " << client.getRemoteAddress() << std::endl;
                                                                        selector.remove(client);
                                                                        client.disconnect();
                                                                        delete(&client);
                                                                        clients.erase(it);
                                                                        it--;
                                                                        break;
                                                                }      

                                                }
                                        }
                                }
                        }
                }
        }

        return 0;
}
 
and this is my client source
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>

#include <iostream>

int main()
{
        sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
        sf::CircleShape shape(100.f);
        shape.setFillColor(sf::Color::Green);

        sf::TcpSocket socket;

        if (socket.connect("192.168.0.8", 55001) != sf::Socket::Done){
                std::cout << "An error ocurred connecting to server" << std::endl;
        }
        else
        {
                std::cout << "connected" << std::endl;
        }

        socket.setBlocking(false);

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed){
                                socket.disconnect();
                                window.close();
                        }
                        if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Return)
                        {
                                std::cout << "a" << std::endl;
                                std::string message = "Hi, I am a client";
                                socket.send(message.c_str(), message.size() + 1);
                        }
                }

                window.clear();
                window.draw(shape);
                window.display();
        }

        return 0;
}
 
Title: Re: Server does not receive packets
Post by: pinko64 on May 15, 2014, 11:02:52 am
Didn't read through but your client isn't sending a sf::Packet but a raw data...

Maby send a packet that has the string?
Title: Re: Server does not receive packets
Post by: Charsmud on June 24, 2014, 08:36:14 pm
You're not sending a packet, you're sending a string:

                std::string message = "Hi, I am a client";
                socket.send(message.c_str(), message.size() + 1);
 

Instead, send a Packet. 

sf::Packet packet;
packet << message;
socket.send(packet, sizeofpacket);
 

Of course, that's only pseudo code, but that's the jist of how it would work.