Should sockets be kept open or torn down [duplicate]
Asked Answered
B

3

10

Possible Duplicate:
Should I close a socket (TCPIP) after every transaction?

Lets say I have some type of interprocess communication that works using sockets.

Should my processes establish a connection and hold it open (1 thread per client or similar) sending data when it needs; or is a better approach to simply establish a connection, send the data I wish, close it and enter my wait state again?

What is usually the approach to this problem?

Blake answered 13/4, 2011 at 5:9 Comment(3)
Just out of curiosity, why sockets over named pipes?Menefee
In my case I am building a bridge between an Android application and C# so I don't think pipes would be appropriate (or possible)?Blake
Interprocess refers to communication between different processes on same machine, so I asked, if they are on different machines then client server or peer to peer is correct word.Menefee
J
7

I asked this same question and all three responses said to leave it open. I went with that, and it seems to be working for me.

Should I close a socket (TCPIP) after every transaction?

Jacquesjacquet answered 13/4, 2011 at 5:14 Comment(0)
G
3

Local sockets do not have much overhead since they skip the TCP/IP Stack and are implemented using named pipes. Keeping a socket open and close wouldn't make much of difference.

Glen answered 13/4, 2011 at 5:13 Comment(1)
Local sockets skips the TCP/IP stack only if you specifically open a Unix domain socket - where that is available.Billow
M
2

Any resource, be it file, socket, database connection or hardware device, take time and uses processor and memory to open because it has to fetch resources, compute security access and do some bookkeeping.

Opening and closing between every message will just waste CPU and memory resources.

Keeping it open for long time is also dangerous as well but you have to leave it open and decide best timeout value to close it automatically when any one end has died.

Menefee answered 13/4, 2011 at 5:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.