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

Author Topic: TCP fail to send data  (Read 1719 times)

0 Members and 1 Guest are viewing this topic.

ILostMyAccount

  • Newbie
  • *
  • Posts: 22
    • View Profile
TCP fail to send data
« on: January 21, 2015, 03:16:14 pm »
What's wrong with this? :
        while (!done)
        {
                std::cout << "Creating connection" << std::endl;
                sf::TcpSocket socket;
                sf::Socket::Status status = socket.connect("192.168.0.5", 53000);
                if (status != sf::Socket::Done)
                {
                        std::cout << "Client Error" << std::endl;
                }
                std::cout << "Created" << std::endl;
                char data[100] = { 'H', 'e', 'l', 'l', 'o', '\0' };
                system("pause");
                if (socket.send(data, 100) != sf::Socket::Done)
                {
                        std::cout << "Client Error : Couldn't send data" << std::endl;
                }
        }

Basically it sends the data the first time, then fail the second, and work the third, and fail the fourth and... you get what I mean...

How do I fix it?

(Ignore the "system("pause")" it's just to test and it's faster than typing something everytime.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: TCP fail to send data
« Reply #1 on: January 21, 2015, 06:42:56 pm »
Don't declare & connect the socket inside the loop. Ports remain unavailable for a short while after being disconnected, and since your code keeps on connecting to the same port, it fails regularly.
Laurent Gomila - SFML developer