Use TCP_QUICKACK and SO_LINGER to eliminate the 3rd packet in tcp handshake

Find an insterest project, to remove last packet in 3-way tcp handshake, https://github.com/tevino/tcp-shaker

Disable TCP_QUICKACK(linux 2.4.4+) will prevent connect to sending ack(3 packet) immediatelly, this could make place for no 4 way termination(sync -> ack+sync -> rst). Haproxy use this strategy to do tcp health check

默認(rèn)情況connect完成即三次握手結(jié)束掀抹,設(shè)置TCP_QUICKACK為0(默認(rèn)1)的話,connect完了還會(huì)等一段時(shí)間才會(huì)發(fā)ack(個(gè)人理解)tcp-3way-handshak

The last ACK in the TCP handshake can already contain a payload. But, this is usually not done since the application first calls connect and then will either wait for the server to reply or send its first data. Since the kernel does not know what the application will do next it will already send out the ACK within the connect so that the server knows as fast as possible that the connection is established.
Depending on your OS it might be possible to change this behavior and send the ACK together with the first data. In Linux this can be achieved by explicitly disabling quick ack before connecting:
int off = 0;
setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &off, sizeof(off));
connect(fd,...)

https://stackoverflow.com/a/6440364

Turn the SO_LINGER socket option on and set the linger time to 0 seconds. This will cause TCP to abort the connection when it is closed, flush the data and send a RST. See section 7.5 and example 15.21 in UNP.

This option specifies how the close function operates for a connection-oriented protocol , By default, close returns immediately, but if there is any data still remaining in the socket send buffer, the system will try to deliver the data to the peer.
if l_onoff is nonzero and l_linger is zero, TCP aborts the connection when it is closed (pp. 1019–1020 of TCPv2). That is, TCP discards any data still remaining in the socket send buffer and sends an RST to the peer, not the normal four-packet connection termination sequence

import socket
import select
import struct
import time

s = socket.socket()
s.setblocking(0)
s.setsockopt(socket.SOL_TCP, socket.TCP_QUICKACK, 0)
s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER,
                         struct.pack('ii', 1, 0)) # when socket close, discard pending writes and send rst
try:
    s.connect(("10.197.128.110", 8081))
except: # EInprogress
    pass
select.select([s], [], [], 0.1)
s.close() # with the help of linger, close will send rst packet
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌狮暑,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,602評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異绍撞,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)得院,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,442評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門傻铣,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人祥绞,你說(shuō)我怎么就攤上這事非洲。” “怎么了蜕径?”我有些...
    開(kāi)封第一講書(shū)人閱讀 152,878評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵两踏,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我兜喻,道長(zhǎng)梦染,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,306評(píng)論 1 279
  • 正文 為了忘掉前任朴皆,我火速辦了婚禮弓坞,結(jié)果婚禮上隧甚,老公的妹妹穿的比我還像新娘。我一直安慰自己渡冻,他們只是感情好戚扳,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,330評(píng)論 5 373
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著族吻,像睡著了一般帽借。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上超歌,一...
    開(kāi)封第一講書(shū)人閱讀 49,071評(píng)論 1 285
  • 那天砍艾,我揣著相機(jī)與錄音,去河邊找鬼巍举。 笑死脆荷,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的懊悯。 我是一名探鬼主播蜓谋,決...
    沈念sama閱讀 38,382評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼炭分!你這毒婦竟也來(lái)了桃焕?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 37,006評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤捧毛,失蹤者是張志新(化名)和其女友劉穎观堂,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體呀忧,經(jīng)...
    沈念sama閱讀 43,512評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡师痕,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,965評(píng)論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了而账。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片七兜。...
    茶點(diǎn)故事閱讀 38,094評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖福扬,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情惜犀,我是刑警寧澤铛碑,帶...
    沈念sama閱讀 33,732評(píng)論 4 323
  • 正文 年R本政府宣布,位于F島的核電站虽界,受9級(jí)特大地震影響汽烦,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜莉御,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,283評(píng)論 3 307
  • 文/蒙蒙 一撇吞、第九天 我趴在偏房一處隱蔽的房頂上張望俗冻。 院中可真熱鬧,春花似錦牍颈、人聲如沸迄薄。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,286評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)讥蔽。三九已至,卻和暖如春画机,著一層夾襖步出監(jiān)牢的瞬間冶伞,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,512評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工步氏, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留响禽,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,536評(píng)論 2 354
  • 正文 我出身青樓荚醒,卻偏偏與公主長(zhǎng)得像芋类,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子腌且,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,828評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容