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

Author Topic: [SOLVED] Reusing sockets  (Read 3927 times)

0 Members and 1 Guest are viewing this topic.

starfruitinc

  • Newbie
  • *
  • Posts: 9
    • View Profile
[SOLVED] Reusing sockets
« on: September 30, 2011, 05:33:11 am »
Hello everyone,

I have a problem with TCP sockets. I can only get the socket to send once, then all calls to Send() afterward don`t work.

Code: [Select]
sf::SocketTCP* Client = static_cast<sf::SocketTCP*>(UserData);
    char Buffer[1024];
    std::size_t Received;
    char * buf2;


    for (int i = 0; i < 2; i++) {
    Client->Receive(Buffer, sizeof(Buffer), Received);
    Buffer[Received] = '\0'; //Nulling the last+1 cell
    cout << Buffer;
    }

    cout << "End of Recv" << endl;

    char getpage[] = "NICK lmno \n\rUSER lmno 0 * :lmno\n\rJOIN #mytest\n\rPRIVMSG #mytest :qwertyuiop\0";
    Client->Send(getpage, sizeof(getpage)); //This one works

    char message[] = "PRIVMSG #mytest :La-di-da\n\r\0";
    while (1) {

        Client->Receive(Buffer, sizeof(Buffer), Received);
        Buffer[Received] = '\0'; //Nulling the last+1 cell
        cout << Buffer <<endl;

        Client->Send(message, sizeof(message)); // This one does not
        cout << "Sent ladida" << endl;
 




    }


Help is very much appreciated.

edit: I tried out something new, and it works, but it has a useless Send()
Code: [Select]
sender(*Client);
I put this in the while loop
Code: [Select]
void sender(sf::SocketTCP Client) {
    char pong[1024];
    memset (pong, '0', (size_t)1024);
    strcpy(pong, "PRIVMSG #mytest :qwerty");
    //strcat(pong, ping);
    strcat(pong, "\n\r");
    cout << pong << endl;

    Client.Send(pong, sizeof(pong));
    Client.Send(pong, sizeof(pong));

    char msg[128];
    strcpy(msg, "PRIVMSG #mytest :La-Di_da\n\r");
    Client.Send(msg, sizeof(msg));
    cout << "Sent from sender" << endl;
}


The extra Send() must be there to work. Does anyone know why?

Thanks,
SFI

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SOLVED] Reusing sockets
« Reply #1 on: September 30, 2011, 08:00:41 am »
I don't think it's a problem with SFML sockets, but rather with the IRC protocol.
Laurent Gomila - SFML developer

starfruitinc

  • Newbie
  • *
  • Posts: 9
    • View Profile
[SOLVED] Reusing sockets
« Reply #2 on: September 30, 2011, 09:09:43 am »
Hmm... I'm using commands that I have tested in telnet. I think there may be something wrong with the sockets because the IRC server would give verbose errors if you typed something wrong. I know this because some of the char arrays (somehow) are not sanitized and send accented characters, which the IRC server would then respond "Unknown Command"

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SOLVED] Reusing sockets
« Reply #3 on: September 30, 2011, 09:23:56 am »
Can you show a complete and minimal code that reproduces the problem?
Laurent Gomila - SFML developer

starfruitinc

  • Newbie
  • *
  • Posts: 9
    • View Profile
[SOLVED] Reusing sockets
« Reply #4 on: October 01, 2011, 01:21:06 am »
Code: [Select]
void sendAndRecv(void * UserData) {

    sf::SocketTCP* Client = static_cast<sf::SocketTCP*>(UserData);
    char Buffer[1024];
    std::size_t Received;
    char * buf2;

    //while (Received > 0) {
    for (int i = 0; i < 2; i++) {
    Client->Receive(Buffer, sizeof(Buffer), Received);
    Buffer[Received] = '\0'; //Nulling the last+1 cell
    cout << Buffer;
    }


    char getpage[] = "NICK lmno \n\rUSER lmno 0 * :lmno\n\rJOIN #mytest\n\rPRIVMSG #mytest :qwertyuiop\0";
    Client->Send(getpage, sizeof(getpage));



    char message[] = "PRIVMSG #mytest :La-di-da\n\r";
    while (1) {

        Client->Receive(Buffer, sizeof(Buffer), Received);
        Buffer[Received] = '\0'; //Nulling the last+1 cell
        cout << Buffer <<endl;

        if (strstr(Buffer, "ha!")) {
            Client->Send("ABCDEFG", (size_t)7); //These don't seem to work...
            Client->Send(message, sizeof(message));
            Client->Send(message, sizeof(message));
            Client->Send("ABCDEFG", (size_t)7);
            cout << "ha! triggered" << endl;
        }



        if (strstr(Buffer, ":+i")) {

            char joinchan[] = "JOIN #mytest\n\r\0";
            Client->Send(joinchan, sizeof(joinchan));
            cout << "Sent" << endl << joinchan <<endl;
        }
    }
}

int main() {
    sf::SocketTCP Client;

    connectToServer(Client);

    sf::Thread Thread(&sendAndRecv, &Client);
    Thread.Launch();
}



TCPSocket is passed thru void * UserData, as this is part of a thread.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SOLVED] Reusing sockets
« Reply #5 on: October 01, 2011, 10:33:27 am »
This code is not complete. Where is the connectToServer function?
Laurent Gomila - SFML developer

starfruitinc

  • Newbie
  • *
  • Posts: 9
    • View Profile
[SOLVED] Reusing sockets
« Reply #6 on: October 01, 2011, 08:28:05 pm »
Whoops!
The connectToServer function
Code: [Select]

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

starfruitinc

  • Newbie
  • *
  • Posts: 9
    • View Profile
[SOLVED] Reusing sockets
« Reply #7 on: October 02, 2011, 12:41:27 am »
I found the problem. You are right Laurent, it is a problem regarding to the IRC protocol, it does not like \0 at the end of every transmission. A simple -1 to the second argument of the Send function fixes the problem.

Thank you for your help :D