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

Author Topic: Recv Loop  (Read 3468 times)

0 Members and 1 Guest are viewing this topic.

starfruitinc

  • Newbie
  • *
  • Posts: 9
    • View Profile
Recv Loop
« on: September 29, 2011, 08:31:47 am »
Hello again everyone,

I have a question about how to operate a recv loop. I am making an IRC client and I would like to receive the first few messages, then send in credentials then continue receiving and sending.

Code: [Select]
void ServerHandler::connectToServer() {

    if (Client.Connect(6667, "irc.freenode.org") != sf::Socket::Done) {
        cout << "Error 1" << endl;
    }
    cout << "No Error 1" << endl;
}

void ServerHandler::sendAndRecv() {
    char Buffer[128];
    std::size_t Received;
    for (int i = 0; i < 2; i++) {
    if (Client.Receive(Buffer, sizeof(Buffer), Received) != sf::Socket::Done)
    {
        cout << "Error 2" << endl;
    }
    cout << Buffer << " " << Received <<endl;
    }
    cout << "End of Recv" << endl;

    char getpage[] = "NICK testing \n\rUSER testing 0 * :Testing";
    if (Client.Send(getpage, sizeof(getpage)) != sf::Socket::Done)
    {
        cout << "Error 1.5" << endl;
    }


    for (;;) {
    if (Client.Receive(Buffer, sizeof(Buffer), Received) != sf::Socket::Done)
    {
        cout << "Error 2" << endl;
    }
    cout << Buffer << endl;
    }

}

The problem with this code is that the first recv loop doesn't terminate for a while then abruptly terminates, moving on to the send bit. The second problem is that the socket does not send properly, as it doesn't get any reply, instead terminating the connection. I have already tested the command in telnet, so I know it works. Can anyone tell me what I did wrong?

I also have a few questions. What does sf::Socket::Done do? How do I make this socket be able to receive and send at irregular intervals (Such that it doesn't know when it will be receiving, and it will be sending at around 5-25 strings/second)

Edit: I found out what was the first problem, the IRC server sends 3 lines then waits for the credentials, but the client is also waiting for more... I changed the loop to only loop thru 2 times

Edit 2: I fixed the first recv loop, I'm not sure how it happened but now that problem is not there anymore.

Thanks

 

anything