Detect why a socket closed: FIN vs RST












2















Is it possible to detect why a socket closed in Python, i.e. whether the other side sent a FIN or an RST?



The only way I know to detect if the other side has senta FIN or RST, is to read from the socket, and if you get the empty (byte) string, then we have received either a FIN or an RST. But how to know which one?



import asyncio
import socket

async def async_main(loop):

server_sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM,
proto=socket.IPPROTO_TCP)
server_sock.setblocking(False)
server_sock.bind(('', 8080))
server_sock.listen(socket.IPPROTO_TCP)

loop = asyncio.get_event_loop()
sock, adddress = await loop.sock_accept(server_sock)

while True:
data = await loop.sock_recv(sock, 1024)
if data == b'':
# Socket closed, but how?
break
print(data)

loop = asyncio.get_event_loop()
loop.run_until_complete(async_main(loop))









share|improve this question


















  • 2





    I would expect an normal EOF condition(data == b'') only when receiving a FIN. If you receive a RST I'd expect an exception to be raised of the form "Connection reset by peer".

    – James K Polk
    Nov 17 '18 at 4:30


















2















Is it possible to detect why a socket closed in Python, i.e. whether the other side sent a FIN or an RST?



The only way I know to detect if the other side has senta FIN or RST, is to read from the socket, and if you get the empty (byte) string, then we have received either a FIN or an RST. But how to know which one?



import asyncio
import socket

async def async_main(loop):

server_sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM,
proto=socket.IPPROTO_TCP)
server_sock.setblocking(False)
server_sock.bind(('', 8080))
server_sock.listen(socket.IPPROTO_TCP)

loop = asyncio.get_event_loop()
sock, adddress = await loop.sock_accept(server_sock)

while True:
data = await loop.sock_recv(sock, 1024)
if data == b'':
# Socket closed, but how?
break
print(data)

loop = asyncio.get_event_loop()
loop.run_until_complete(async_main(loop))









share|improve this question


















  • 2





    I would expect an normal EOF condition(data == b'') only when receiving a FIN. If you receive a RST I'd expect an exception to be raised of the form "Connection reset by peer".

    – James K Polk
    Nov 17 '18 at 4:30
















2












2








2








Is it possible to detect why a socket closed in Python, i.e. whether the other side sent a FIN or an RST?



The only way I know to detect if the other side has senta FIN or RST, is to read from the socket, and if you get the empty (byte) string, then we have received either a FIN or an RST. But how to know which one?



import asyncio
import socket

async def async_main(loop):

server_sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM,
proto=socket.IPPROTO_TCP)
server_sock.setblocking(False)
server_sock.bind(('', 8080))
server_sock.listen(socket.IPPROTO_TCP)

loop = asyncio.get_event_loop()
sock, adddress = await loop.sock_accept(server_sock)

while True:
data = await loop.sock_recv(sock, 1024)
if data == b'':
# Socket closed, but how?
break
print(data)

loop = asyncio.get_event_loop()
loop.run_until_complete(async_main(loop))









share|improve this question














Is it possible to detect why a socket closed in Python, i.e. whether the other side sent a FIN or an RST?



The only way I know to detect if the other side has senta FIN or RST, is to read from the socket, and if you get the empty (byte) string, then we have received either a FIN or an RST. But how to know which one?



import asyncio
import socket

async def async_main(loop):

server_sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM,
proto=socket.IPPROTO_TCP)
server_sock.setblocking(False)
server_sock.bind(('', 8080))
server_sock.listen(socket.IPPROTO_TCP)

loop = asyncio.get_event_loop()
sock, adddress = await loop.sock_accept(server_sock)

while True:
data = await loop.sock_recv(sock, 1024)
if data == b'':
# Socket closed, but how?
break
print(data)

loop = asyncio.get_event_loop()
loop.run_until_complete(async_main(loop))






python python-3.x sockets tcp python-asyncio






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 20:56









Michal CharemzaMichal Charemza

20k972106




20k972106








  • 2





    I would expect an normal EOF condition(data == b'') only when receiving a FIN. If you receive a RST I'd expect an exception to be raised of the form "Connection reset by peer".

    – James K Polk
    Nov 17 '18 at 4:30
















  • 2





    I would expect an normal EOF condition(data == b'') only when receiving a FIN. If you receive a RST I'd expect an exception to be raised of the form "Connection reset by peer".

    – James K Polk
    Nov 17 '18 at 4:30










2




2





I would expect an normal EOF condition(data == b'') only when receiving a FIN. If you receive a RST I'd expect an exception to be raised of the form "Connection reset by peer".

– James K Polk
Nov 17 '18 at 4:30







I would expect an normal EOF condition(data == b'') only when receiving a FIN. If you receive a RST I'd expect an exception to be raised of the form "Connection reset by peer".

– James K Polk
Nov 17 '18 at 4:30














1 Answer
1






active

oldest

votes


















2














Based on James K Polk's comment, which I believe to be correct, you would distinguish FIN from RST by catching the appropriate exception:



    while True:
is_rst = False
try:
data = await loop.sock_recv(sock, 1024)
except IOError as e:
if e.errno == errno.ECONNRESET:
data = b''
is_rst = True
else:
raise
if data == b'':
# Connection closed - use is_rst to distinguish
# between RST and FIN
break
print(data)





share|improve this answer


























  • Ah thanks, and thanks to @james-k-polk : I was completely wrong in my assumptions.

    – Michal Charemza
    Nov 17 '18 at 8:22













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345265%2fdetect-why-a-socket-closed-fin-vs-rst%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Based on James K Polk's comment, which I believe to be correct, you would distinguish FIN from RST by catching the appropriate exception:



    while True:
is_rst = False
try:
data = await loop.sock_recv(sock, 1024)
except IOError as e:
if e.errno == errno.ECONNRESET:
data = b''
is_rst = True
else:
raise
if data == b'':
# Connection closed - use is_rst to distinguish
# between RST and FIN
break
print(data)





share|improve this answer


























  • Ah thanks, and thanks to @james-k-polk : I was completely wrong in my assumptions.

    – Michal Charemza
    Nov 17 '18 at 8:22


















2














Based on James K Polk's comment, which I believe to be correct, you would distinguish FIN from RST by catching the appropriate exception:



    while True:
is_rst = False
try:
data = await loop.sock_recv(sock, 1024)
except IOError as e:
if e.errno == errno.ECONNRESET:
data = b''
is_rst = True
else:
raise
if data == b'':
# Connection closed - use is_rst to distinguish
# between RST and FIN
break
print(data)





share|improve this answer


























  • Ah thanks, and thanks to @james-k-polk : I was completely wrong in my assumptions.

    – Michal Charemza
    Nov 17 '18 at 8:22
















2












2








2







Based on James K Polk's comment, which I believe to be correct, you would distinguish FIN from RST by catching the appropriate exception:



    while True:
is_rst = False
try:
data = await loop.sock_recv(sock, 1024)
except IOError as e:
if e.errno == errno.ECONNRESET:
data = b''
is_rst = True
else:
raise
if data == b'':
# Connection closed - use is_rst to distinguish
# between RST and FIN
break
print(data)





share|improve this answer















Based on James K Polk's comment, which I believe to be correct, you would distinguish FIN from RST by catching the appropriate exception:



    while True:
is_rst = False
try:
data = await loop.sock_recv(sock, 1024)
except IOError as e:
if e.errno == errno.ECONNRESET:
data = b''
is_rst = True
else:
raise
if data == b'':
# Connection closed - use is_rst to distinguish
# between RST and FIN
break
print(data)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 17 '18 at 8:27

























answered Nov 17 '18 at 7:55









user4815162342user4815162342

61.8k592145




61.8k592145













  • Ah thanks, and thanks to @james-k-polk : I was completely wrong in my assumptions.

    – Michal Charemza
    Nov 17 '18 at 8:22





















  • Ah thanks, and thanks to @james-k-polk : I was completely wrong in my assumptions.

    – Michal Charemza
    Nov 17 '18 at 8:22



















Ah thanks, and thanks to @james-k-polk : I was completely wrong in my assumptions.

– Michal Charemza
Nov 17 '18 at 8:22







Ah thanks, and thanks to @james-k-polk : I was completely wrong in my assumptions.

– Michal Charemza
Nov 17 '18 at 8:22




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345265%2fdetect-why-a-socket-closed-fin-vs-rst%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings