I have a simple P2P connection between my peers on a TCP socket. My client and server both are running on Linux. I have turned on TCP keep_alive functionality on my TCP sockets on both sides. I am using boost::asio to connect, read ane write data on my tcp sockets on both sides.
The tcp keep_alive expamples use IPPROTO_TCP
and SOL_SOCKET
which is confusing. Following are properties I set on my socket. But I am confused whether to use IPPROTO_TCP
or SOL_SOCKET
because both of them compile well on both the platforms.
Code:
int on = 1;
setsockopt(socketNativeHandle, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(int)
int timeOut = 3; //seconds
setsockopt(socketNativeHandle, IPPROTO_TCP, TCP_KEEPIDLE, &timeOut, sizeof(int)
setsockopt(socketNativeHandle, IPPROTO_TCP, TCP_KEEPINTVL, &timeOut, sizeof(int))
int unackCount = 1;
setsockopt(socketNativeHandle, IPPROTO_TCP, TCP_KEEPCNT, &unackCount, sizeof(int))
OR
int on = 1;
setsockopt(socketNativeHandle, SOL_SOCKET, TCP_NODELAY, &on, sizeof(int)
int timeOut = 3; //seconds
setsockopt(socketNativeHandle, SOL_SOCKET, TCP_KEEPIDLE, &timeOut, sizeof(int)
setsockopt(socketNativeHandle, SOL_SOCKET, TCP_KEEPINTVL, &timeOut, sizeof(int))
int unackCount = 1;
setsockopt(socketNativeHandle, SOL_SOCKET, TCP_KEEPCNT, &unackCount, sizeof(int))
Question:
Should I use SOL_SOCKET
on both sides or should I use IPPROTO_TCP
on both sides? Is there a way to decide this at runtime? Note that I have very simple peer to peer wifi connection.
SOL is abbreviation of socket_level, which designed for SO_*
options, such as SO_REUSEPORT
IPPROTO_*
is designed for options of a specific network protocol, such as IPPROTO_IP, IPPROTO_TCP
More: