最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

connection - how does closing a TCP socket cause the remote application to terminate? - Stack Overflow

programmeradmin8浏览0评论

For a test setup, I have two separate applications (buildt with gcc), in this instance running on the same physical machine. One opens a TCP socket, accepting incoming connections, and sends data on a regular interval. After establishing connection, the transmission happen in a while (1) loop. The second one is a GTK application, receiving and plotting the transferred data. For testing I run both on the same machine, started from separate terminals.

When I close the GTK application, either via the display window's close button or Ctrl-C in the terminal, the sender application in the other terminal terminates also. But how come ? Is this behavior specified ?

What I almost fot - host operating system is Linux. Specifically, Mint 20.3, an Ubuntu-based distro.

For a test setup, I have two separate applications (buildt with gcc), in this instance running on the same physical machine. One opens a TCP socket, accepting incoming connections, and sends data on a regular interval. After establishing connection, the transmission happen in a while (1) loop. The second one is a GTK application, receiving and plotting the transferred data. For testing I run both on the same machine, started from separate terminals.

When I close the GTK application, either via the display window's close button or Ctrl-C in the terminal, the sender application in the other terminal terminates also. But how come ? Is this behavior specified ?

What I almost fot - host operating system is Linux. Specifically, Mint 20.3, an Ubuntu-based distro.

Share Improve this question edited Mar 27 at 13:20 codis asked Mar 27 at 12:55 codiscodis 11 bronze badge 5
  • Was the sender application written by you? – grawity_u1686 Commented Mar 27 at 13:21
  • Yes. All my own source code. And since it just a test application to feed the GTK app, there is not much cleanup. It just calls send() in a "do {} while (1);" loop once a second. Except of printf()/sprintf(), the loop only calls send() and sleep(). – codis Commented Mar 27 at 14:36
  • What does your source code do with the result of send()? What exit status does the program give (e.g. echo $? in Bash)? When it exits, does it execute any code (e.g. a printf) which is after the loop? – grawity_u1686 Commented Mar 27 at 14:39
  • Being more at home with bare-metal embedded stuff, Linux is occasionally still a challenge ... – codis Commented Mar 27 at 16:34
  • "echo $?" returns "0" (zero). And I don't evaluate the return value of send(). This is just a straighforward ad-hoc test app, without much error handling. But running the test sender application with "ltrace", it ends the following way : ... send(4, 0x7ffe1c9c03b0, 53, 0 <no return ...> --- SIGPIPE (Broken pipe) --- +++ killed by SIGPIPE +++ – codis Commented Mar 27 at 16:41
Add a comment  | 

1 Answer 1

Reset to default 0

.... --- SIGPIPE (Broken pipe) --- +++ killed by SIGPIPE +++

Your comment basically is the answer: The call to send fails since the TCP connection has been closed - which is handled automatically by the OS kernel if the process owning the TCP connection exits. Since the TCP connection is closed by the peer an attempt to send more data (or the close with still unread data) cause a TCP RST, i.e. "Connection reset". This will cause a SIGPIPE, unless the application has specifically handled this case, like using MSG_NOSIGNAL flag for send. And since SIGPIPE is not catched by your application the application will be killed by the OS.

发布评论

评论列表(0)

  1. 暂无评论