Some Basics of TCP/IP ProtocolNotes for Building Your Own Sockets Application
Common Server ModelsCommon Client Models
Other
Dive into Socket Programming
LiLi
August 16, 2013
LiLi Dive into Socket Programming
Some Basics of TCP/IP ProtocolNotes for Building Your Own Sockets Application
Common Server ModelsCommon Client Models
Other
OutLine
I Some Basics of TCP/IP ProtocolI TIME WAIT CLOSE WAITI SO REUSEADDR SO LINGERI TCP NODELY TCP QUICKACKI Socket BufferI SIG PIPEI KeepAliveI close shudown
I Notes for Building Your Own Sockets ApplicationI Manage Your Own Application-Layer BufferI Async Connect/AcceptI Thundering Herd
LiLi Dive into Socket Programming
Some Basics of TCP/IP ProtocolNotes for Building Your Own Sockets Application
Common Server ModelsCommon Client Models
Other
OutLine
I Common Server ModelsI thread-per-connectionI reactorI reactor + worker threadI multiple reactors
I Common Client ModelsI connection poolI async io + event loop
I OthersI C10k C100k ...
LiLi Dive into Socket Programming
Some Basics of TCP/IP ProtocolNotes for Building Your Own Sockets Application
Common Server ModelsCommon Client Models
Other
TIME WAIT CLOSE WAIT
LiLi Dive into Socket Programming
Some Basics of TCP/IP ProtocolNotes for Building Your Own Sockets Application
Common Server ModelsCommon Client Models
Other
TIME WAIT CLOSE WAIT
I active close —— TIME WAIT
I passive close —— CLOSE WAIT
I read returns 0peer has closed connection, invoke close
I TIME WAIT will last 2MSL max segment lifetime
LiLi Dive into Socket Programming
Some Basics of TCP/IP ProtocolNotes for Building Your Own Sockets Application
Common Server ModelsCommon Client Models
Other
SO REUSERADDR SO LINGER
I SO REUSEADDRwhen server crashes, the port remains in TIME WAIT. Servercan’t restart immediately?set SO REUSEADDR
I SO LINGERCan set SO LINGER to prevent entering TIME WAIT. Thisalso discards any unsent data.don’t do this!!! why?
LiLi Dive into Socket Programming
Some Basics of TCP/IP ProtocolNotes for Building Your Own Sockets Application
Common Server ModelsCommon Client Models
Other
TCP NODELY TCP QUICKACK
I TCP NODELYNagle’s algorithm
I TCP CORKwhen transmit files
I TCP QUICKACKdelayed ackmust set the option every time
LiLi Dive into Socket Programming
Some Basics of TCP/IP ProtocolNotes for Building Your Own Sockets Application
Common Server ModelsCommon Client Models
Other
Socket Buffer
buffer size = 2 * Bandwidth * Delay
how to measure delay?
LiLi Dive into Socket Programming
Some Basics of TCP/IP ProtocolNotes for Building Your Own Sockets Application
Common Server ModelsCommon Client Models
Other
SIG PIPE
I Write a Closed Socket Twice:first time, ok, receive RSTsecond time, SIG PIPE, crash
I Ignore the Signal:signal(SIGPIPE,SIG IGN)
LiLi Dive into Socket Programming
Some Basics of TCP/IP ProtocolNotes for Building Your Own Sockets Application
Common Server ModelsCommon Client Models
Other
KeepAlive
I KeepAlive optionToo Long, 2 hours 11mins you know peer is dead
I Heartbeat in your own application
LiLi Dive into Socket Programming
Some Basics of TCP/IP ProtocolNotes for Building Your Own Sockets Application
Common Server ModelsCommon Client Models
Other
close shutdown
I shutdown can only shutdown one directionshutdown write,can still read
I shutdown does not close the file descriptor,still need close
LiLi Dive into Socket Programming
Some Basics of TCP/IP ProtocolNotes for Building Your Own Sockets Application
Common Server ModelsCommon Client Models
Other
Manage Your Own Application-Layer Buffer
I readcan read all one time ?
I writecan write all one time ?
I trigered from event loopread to your own bufferwrite from your own buffer
LiLi Dive into Socket Programming
Some Basics of TCP/IP ProtocolNotes for Building Your Own Sockets Application
Common Server ModelsCommon Client Models
Other
Async Connect/Accept
I Blocked connect/acceptMaybe Too Long
I Async acceptEWOULDBLOCKadd listen fd to event loop
I Async connectEINPROCESSadd connect fd to event loop
LiLi Dive into Socket Programming
Some Basics of TCP/IP ProtocolNotes for Building Your Own Sockets Application
Common Server ModelsCommon Client Models
Other
Thundering Herd
I acceptno longer a problem in kernel now
I epoll waitstill a problem
I how to solve?lighthttpdnginx
LiLi Dive into Socket Programming
Some Basics of TCP/IP ProtocolNotes for Building Your Own Sockets Application
Common Server ModelsCommon Client Models
Other
thread-per-connection
I create a new thread for each connection
I Before NIO,Java use this
LiLi Dive into Socket Programming
Some Basics of TCP/IP ProtocolNotes for Building Your Own Sockets Application
Common Server ModelsCommon Client Models
Other
reactor
I simple event loop
I Example:Redis
LiLi Dive into Socket Programming
Some Basics of TCP/IP ProtocolNotes for Building Your Own Sockets Application
Common Server ModelsCommon Client Models
Other
reactor+ worker thread
I event loop + worker thead pool
I Example:tbnet
LiLi Dive into Socket Programming
Some Basics of TCP/IP ProtocolNotes for Building Your Own Sockets Application
Common Server ModelsCommon Client Models
Other
multiple reactors
I event loop each thread/process
I Example:memcachednginxlighthttpd
LiLi Dive into Socket Programming
Some Basics of TCP/IP ProtocolNotes for Building Your Own Sockets Application
Common Server ModelsCommon Client Models
Other
connection pool
I create a set pf connection each client
I Example:Jedis
LiLi Dive into Socket Programming
Some Basics of TCP/IP ProtocolNotes for Building Your Own Sockets Application
Common Server ModelsCommon Client Models
Other
async io + event loop
I event loop
I Example:Minatbnet
LiLi Dive into Socket Programming
Some Basics of TCP/IP ProtocolNotes for Building Your Own Sockets Application
Common Server ModelsCommon Client Models
Other
Other
I C10k C100kepoll1000Mbit/s one event loop —by zeromq guide
I C1M C10M...I More Challenges:
softirqmemory...
I dive into kernel
LiLi Dive into Socket Programming