Example init for linux may look like this:
int flags;
s = socket(PF_INET, SOCK_STREAM, IPPROTO_IP) // ret 5
setsockopt(s, SOL_TCP, TCP_NODELAY, [1], 4)
setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, [1], 4)
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, [1], 4)
flags = fcntl(s,F_GETFL,0);
assert(flags != -1);
fcntl(s, F_SETFL, flags | O_NONBLOCK);
connect(s, {sa_family=AF_INET, sin_port=htons(5001), sin_addr=inet_addr("192.168.0.68")}, 16)
Basic white paper from sun:
sun asych net
On windows You use Overlapped IO sockets to get non blocking networking.
Look here and in MSDN how to write code with OVERLAPPED structures
On Linux use epoll().
On solaris socket().
Be aware to read or write to a socket when it's not ready.
(select-output) Because You may receive EAGAIN error.
Great cross platform library (but C++ and new c++ standard candidate) is boost::asio.
It uses native system asynchronous methods.
boost::asio
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…