Fine tuning TCP sockets

Based on this article.

TCP communications is optimized the following way: sent data is accummulcated, until a sufficient size packet is generated - and only then the data is actually sent over the network. Sometimes, however, you want to send the data straight away, ignoring its size. You can change the socket's properties to make it work as you expect:

#include <netinet/tcp.h>
#include "ace/SOCK_Connector.h"
#include "ace/INET_Addr.h"

ACE_SOCK_Stream stream;
ACE_SOCK_Connector connector;
ACE_INET_Addr remoteAddr;

if ( -1 == connector.connect( stream, remoteAddr ) ) {
LOG4CXX_ERROR( m_logger, "Could not connect to '"<<
m_remoteAddr.get_host_name()<<":"<<m_remoteAddr.get_port_number()<<"'. With error "<<errno );
return -1;
}

int32 setOptData = 1;
stream.set_option( SOL_TCP, TCP_NODELAY, &setOptData, sizeof(setOptData) );

You can also use the "oposite" attribute of TCP_NODELAY: TCP_CORK which collects as much data as possible, before sending it via TCP.