Search code examples
network-programmingposix

Should I close TCP connection manually after terminate process?


Assume I write a program ,and the program create tcp after start up ,my question is ,should I write code to close tcp connections before I close the program .Or,os can close TCP connections belong to terminated program automatically?


Solution

  • While the operating system will eventually close the connection, doing so in your code has the advantages of expediting the closure (e.g. making the other end aware of it sooner) and releasing buffers and other TCP state info earlier, preventing lack of such space blocking subsequent connections (for other programs).