dive into network_programming

21
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

Upload: tunixiong

Post on 17-May-2015

830 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Dive into network_programming

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

Page 2: Dive into network_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

Page 3: Dive into network_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

Page 4: Dive into network_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

Page 5: Dive into network_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

Page 6: Dive into network_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

Page 7: Dive into network_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

Page 8: Dive into network_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

Page 9: Dive into network_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

Page 10: Dive into network_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

Page 11: Dive into network_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

Page 12: Dive into network_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

Page 13: Dive into network_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

Page 14: Dive into network_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

Page 15: Dive into network_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

Page 16: Dive into network_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

Page 17: Dive into network_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

Page 18: Dive into network_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

Page 19: Dive into network_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

Page 20: Dive into network_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

Page 21: Dive into network_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