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

Author Topic: server can only receive 1 message  (Read 2127 times)

0 Members and 1 Guest are viewing this topic.

lorence30

  • Full Member
  • ***
  • Posts: 124
    • View Profile
    • Email
server can only receive 1 message
« on: November 01, 2015, 07:17:56 am »
here is my server: http://pastebin.com/rSqupQBm
here is the client: http://pastebin.com/ncnfsMu3

server can only receive one message, after that the send function of the socket's client keeps returning sf::Socket::Disconnected

lorence30

  • Full Member
  • ***
  • Posts: 124
    • View Profile
    • Email
Re: server can only receive 1 message
« Reply #1 on: November 01, 2015, 10:02:24 am »
ive fixed it by putting the server.accept(socket) outside the loop, but that will only able to a single client since its outside the loop. its still not fixed

Varonth

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: server can only receive 1 message
« Reply #2 on: November 22, 2015, 05:54:45 pm »
On the serverside:

The TcpSocket you create in your while loop lifetime expires at the end of the loop. Since that one is initialised on the stack, it will only live as long as it's scope is active (so until the while finished its current loop). Once the loop is done, it will get destroyed and the space on the stack will be freed for the next loop iteration. The destructor will clean up the TcpSocket just fine, by disconnecting it properly. The loop will then iterate and accept another client for a single message.

You need to have the client sockets on the heap (using "new"), and keep track of them with some container.

You might want to start looking at the SocketSelector class. It also has an example in it:
http://www.sfml-dev.org/documentation/2.0/classsf_1_1SocketSelector.php