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

Author Topic: server/client works the first time, but then extracted variable isnt updating  (Read 2087 times)

0 Members and 1 Guest are viewing this topic.

shawnku

  • Newbie
  • *
  • Posts: 1
    • View Profile
I am trying to setup a simple client/server where the client sends data (a string) over a TCP socket to the server and the server displays it. The first time I do this it works perfect, the server receives the data from the client and displays the string as it should . However, even though I have my .receive in a loop always awaiting new data it seems to not update the sentence variable I extract from it . I have checked and the client is definitely sending new information, and the servers seems to be receiving the packet but it is not updating.

Here is my server code:

int main() {
    std::string sentence;
    sf::Packet packet;
    sf::TcpListener listener;
    sf::TcpSocket client;
   

    // bind the listener to a port
    if (listener.listen(53000) != sf::Socket::Done)
    {
        cout << "error listening";
    }

    // accept a new connection

    if (listener.accept(client) == sf::Socket::Done)
    {
        cout << "accepted and working :";
    }

    while (1) {
        client.receive(packet);
        packet >> sentence;
        cout << sentence;
    }
cin.get();

}

and my client sending the information:

sf::TcpSocket socket;

sf::Packet packet;
std::string sentence;



 if (...) {

                            packet << sentence;
                            socket.send(packet);
                            cout << sentence;

                        }

whenever i run
socket.Error()
on my client it reads 0 -- no errors, however if i do the same on my server when it receives, it gives me "4" meaning unexpected error
« Last Edit: October 22, 2016, 08:17:04 pm by shawnku »

WithoutBrain

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Use selectors.