1. Some Basics of TCP/IP Protocol
Notes for Building Your Own Sockets Application
Common Server Models
Common Client Models
Other
Dive into Socket Programming
LiLi
August 16, 2013
LiLi Dive into Socket Programming
2. Some Basics of TCP/IP Protocol
Notes for Building Your Own Sockets Application
Common Server Models
Common Client Models
Other
OutLine
Some Basics of TCP/IP Protocol
TIME WAIT CLOSE WAIT
SO REUSEADDR SO LINGER
TCP NODELY TCP QUICKACK
Socket Buffer
SIG PIPE
KeepAlive
close shudown
Notes for Building Your Own Sockets Application
Manage Your Own Application-Layer Buffer
Async Connect/Accept
Thundering Herd
LiLi Dive into Socket Programming
3. Some Basics of TCP/IP Protocol
Notes for Building Your Own Sockets Application
Common Server Models
Common Client Models
Other
OutLine
Common Server Models
thread-per-connection
reactor
reactor + worker thread
multiple reactors
Common Client Models
connection pool
async io + event loop
Others
C10k C100k ...
LiLi Dive into Socket Programming
4. Some Basics of TCP/IP Protocol
Notes for Building Your Own Sockets Application
Common Server Models
Common Client Models
Other
TIME WAIT CLOSE WAIT
LiLi Dive into Socket Programming
5. Some Basics of TCP/IP Protocol
Notes for Building Your Own Sockets Application
Common Server Models
Common Client Models
Other
TIME WAIT CLOSE WAIT
active close —— TIME WAIT
passive close —— CLOSE WAIT
read returns 0
peer has closed connection, invoke close
TIME WAIT will last 2MSL max segment lifetime
LiLi Dive into Socket Programming
6. Some Basics of TCP/IP Protocol
Notes for Building Your Own Sockets Application
Common Server Models
Common Client Models
Other
SO REUSERADDR SO LINGER
SO REUSEADDR
when server crashes, the port remains in TIME WAIT. Server
can’t restart immediately?
set SO REUSEADDR
SO LINGER
Can set SO LINGER to prevent entering TIME WAIT. This
also discards any unsent data.
don’t do this!!! why?
LiLi Dive into Socket Programming
7. Some Basics of TCP/IP Protocol
Notes for Building Your Own Sockets Application
Common Server Models
Common Client Models
Other
TCP NODELY TCP QUICKACK
TCP NODELY
Nagle’s algorithm
TCP CORK
when transmit files
TCP QUICKACK
delayed ack
must set the option every time
LiLi Dive into Socket Programming
8. Some Basics of TCP/IP Protocol
Notes for Building Your Own Sockets Application
Common Server Models
Common Client Models
Other
Socket Buffer
buffer size = 2 * Bandwidth * Delay
how to measure delay?
LiLi Dive into Socket Programming
9. Some Basics of TCP/IP Protocol
Notes for Building Your Own Sockets Application
Common Server Models
Common Client Models
Other
SIG PIPE
Write a Closed Socket Twice:
first time, ok, receive RST
second time, SIG PIPE, crash
Ignore the Signal:
signal(SIGPIPE,SIG IGN)
LiLi Dive into Socket Programming
10. Some Basics of TCP/IP Protocol
Notes for Building Your Own Sockets Application
Common Server Models
Common Client Models
Other
KeepAlive
KeepAlive option
Too Long, 2 hours 11mins you know peer is dead
Heartbeat in your own application
LiLi Dive into Socket Programming
11. Some Basics of TCP/IP Protocol
Notes for Building Your Own Sockets Application
Common Server Models
Common Client Models
Other
close shutdown
shutdown can only shutdown one direction
shutdown write,can still read
shutdown does not close the file descriptor,still need close
LiLi Dive into Socket Programming
12. Some Basics of TCP/IP Protocol
Notes for Building Your Own Sockets Application
Common Server Models
Common Client Models
Other
Manage Your Own Application-Layer Buffer
read
can read all one time ?
write
can write all one time ?
trigered from event loop
read to your own buffer
write from your own buffer
LiLi Dive into Socket Programming
13. Some Basics of TCP/IP Protocol
Notes for Building Your Own Sockets Application
Common Server Models
Common Client Models
Other
Async Connect/Accept
Blocked connect/accept
Maybe Too Long
Async accept
EWOULDBLOCK
add listen fd to event loop
Async connect
EINPROCESS
add connect fd to event loop
LiLi Dive into Socket Programming
14. Some Basics of TCP/IP Protocol
Notes for Building Your Own Sockets Application
Common Server Models
Common Client Models
Other
Thundering Herd
accept
no longer a problem in kernel now
epoll wait
still a problem
how to solve?
lighthttpd
nginx
LiLi Dive into Socket Programming
15. Some Basics of TCP/IP Protocol
Notes for Building Your Own Sockets Application
Common Server Models
Common Client Models
Other
thread-per-connection
create a new thread for each connection
Before NIO,Java use this
LiLi Dive into Socket Programming
16. Some Basics of TCP/IP Protocol
Notes for Building Your Own Sockets Application
Common Server Models
Common Client Models
Other
reactor
simple event loop
Example:
Redis
LiLi Dive into Socket Programming
17. Some Basics of TCP/IP Protocol
Notes for Building Your Own Sockets Application
Common Server Models
Common Client Models
Other
reactor+ worker thread
event loop + worker thead pool
Example:
tbnet
LiLi Dive into Socket Programming
18. Some Basics of TCP/IP Protocol
Notes for Building Your Own Sockets Application
Common Server Models
Common Client Models
Other
multiple reactors
event loop each thread/process
Example:
memcached
nginx
lighthttpd
LiLi Dive into Socket Programming
19. Some Basics of TCP/IP Protocol
Notes for Building Your Own Sockets Application
Common Server Models
Common Client Models
Other
connection pool
create a set pf connection each client
Example:
Jedis
LiLi Dive into Socket Programming
20. Some Basics of TCP/IP Protocol
Notes for Building Your Own Sockets Application
Common Server Models
Common Client Models
Other
async io + event loop
event loop
Example:
Mina
tbnet
LiLi Dive into Socket Programming
21. Some Basics of TCP/IP Protocol
Notes for Building Your Own Sockets Application
Common Server Models
Common Client Models
Other
Other
C10k C100k
epoll
1000Mbit/s one event loop —by zeromq guide
C1M C10M...
More Challenges:
softirq
memory
...
dive into kernel
LiLi Dive into Socket Programming