TCP will never "overflow" the receive buffer with data. When the buffer is full TCP
congestion flow control will kick in and implicitly inform the sender that the destination system can't accept any more data. What that will result in is the sending side filling up as well. Once the sending side is filled up, the send() from your application should fail with an error or NotReady in SFML and indicate you should wait a bit and try again. Trust me, the TCP network stack is built in a very robust way. Even over really really crappy connections TCP will never silently drop data. The only place where data can be lost is in your code if you don't listen to what TCP is trying to tell you. If data loss is unavoidable TCP will at least try to notify you of it by closing the connection. In this case, which is also the only case where data loss can occur, you are informed of the error and should take the required action.
Here is some code showing what happens when the receiver doesn't remove the data from it's queue. It also shows how much data can be queued up on the system running the receiver.
Sender:
#include <iostream>
#include <string>
#include <SFML/Network.hpp>
int main() {
sf::TcpSocket socket;
socket.connect( "12.34.56.78", 4422 );
socket.setBlocking( false );
std::size_t count = 0;
sf::Clock clock;
while( true ) {
sf::Packet packet;
std::string s( "0", 2048 );
packet << s;
sf::Socket::Status status = socket.send( packet );
if( status == sf::Socket::Done ) {
count += 2048;
std::cout << "Sent " << count << " bytes.\n";
} else if( status == sf::Socket::Error ) {
std::cout << "Error\n";
} else if( status == sf::Socket::Disconnected ) {
std::cout << "Disconnected\n";
} else if( status == sf::Socket::NotReady ) {
std::cout << "NotReady\n";
}
if( clock.getElapsedTime() > sf::seconds( 5 ) ) {
return 0;
}
}
return 0;
}
Receiver:
#include <iostream>
#include <SFML/Network.hpp>
int main() {
sf::TcpListener listener;
listener.listen( 4422 );
sf::TcpSocket socket;
listener.accept( socket );
sf::sleep( sf::seconds( 10 ) );
sf::Packet packet;
std::size_t count = 0;
while( !socket.receive( packet ) ) {
count += packet.getDataSize();
}
std::cout << "Dequeued " << count << " bytes.\n";
return 0;
}
Edit: If you need to take a break from coding you should watch a few good lecture videos about TCP. Here's one:
I had the opportunity to attend lectures and learn from my professors first hand
.