1
Network / server/client works the first time, but then extracted variable isnt updating
« on: October 22, 2016, 08:10:06 pm »
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:
and my client sending the information:
whenever i run
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();
}
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;
}
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