Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problems with sf::SocketSelector  (Read 3007 times)

0 Members and 2 Guests are viewing this topic.

Brodal

  • Newbie
  • *
  • Posts: 35
    • View Profile
    • Email
Problems with sf::SocketSelector
« on: January 05, 2013, 12:25:36 pm »
The problem I'm having with the socket selector is that it for some reason doesn't find that i have messages waiting to be received in the socket. I'm connecting to IRC and it works perfectly well to join channels and send messages, but for some reason I am getting no messages back to my own client. ( I know that it is working to join channels and all that because i have another client open on the side that my own made client sends private messages and such to ) I have tried for many hours now to get it working but for some reason it just wont work. I'm hoping that someone can help me. Here is a minimal example

int main()
{
        sf::TcpSocket client;
        sf::SocketSelector selector;

        queue<string> msgQueue;

        msgQueue.push("NICK brewdal \n\rUSER testing 0 * :browdal\n\rJOIN #testthis\n\r");
        msgQueue.push("PRIVMSG #testthis :hallo\n\r");
        msgQueue.push("PRIVMSG Brodal :Hello you\n\r");
       
        selector.add(client);

        sf::Socket::Status status = client.connect("irc.freenode.net", 6667);
        if ( status != sf::Socket::Done )
                cout << "Failed to connect to the specified server" << endl;

        while ( true )
        {
                if ( selector.wait(sf::seconds(0.1f)) )
                {
                        if ( selector.isReady(client) )
                        {
                                char buffer[512];
                                size_t received = 0;
                                client.receive(buffer, 512, received);
                                buffer[received] = '\0';
                                cout << buffer << endl;
                        }
                }
                else if ( !msgQueue.empty() )
                {
                        status = client.send(msgQueue.front().c_str(),msgQueue.front().size());
                        if ( status != sf::Socket::Done )
                        {
                                cout << "Failed to send message" << endl;
                        }
                        else
                        {
                                msgQueue.pop();
                        }
                }
        }

    return EXIT_SUCCESS;
}

I have also tried using a wide variety of different times in the if ( selector.wait() ) and I have also tried infinite time but I still am not getting any messages.
« Last Edit: January 05, 2013, 12:54:36 pm by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Problems with sf::SocketSelector
« Reply #1 on: January 05, 2013, 12:56:20 pm »
You add the client before it is connected; at this time it has an invalid internal handle, which is what the selector stores.
Laurent Gomila - SFML developer

Brodal

  • Newbie
  • *
  • Posts: 35
    • View Profile
    • Email
Re: Problems with sf::SocketSelector
« Reply #2 on: January 05, 2013, 01:04:32 pm »
Ahaaa... Didn't think that mattered, but now I know, thanks alot for the quick reply! :)

Brodal

  • Newbie
  • *
  • Posts: 35
    • View Profile
    • Email
Re: Problems with sf::SocketSelector
« Reply #3 on: January 05, 2013, 01:07:22 pm »
While I'm allready asking things, would anyone happen to know why IRC is sending me infinite amounts of empty messages after I've connected?

Ruckamongus

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: Problems with sf::SocketSelector
« Reply #4 on: January 06, 2013, 11:07:04 am »
Keep alive maybe?

 

anything