0x49D1
0x49D1

Reputation: 8704

Socket object life cycle

I'm using Socket object for sending commands to other program over tcp.

I wonder what is a better decision:

  1. To initialize Socket object on every command send/ receive session and then close it.
  2. Or to initialize one global Socket object, use it to send all commands/receive responses and close on program close.

So the question is how often should i close the socket object for "same" operations?

Upvotes: 1

Views: 1097

Answers (1)

Waqar
Waqar

Reputation: 2591

It depends on you application requirements. e.g. let say you are writing an smtp client then the server would not allow you to connect for ever and this also is request/response type protocol (e.g. you send request to check any mail and receive response of it) then you must close your socket once your request is completed. but if you are writing some chat like application then you might dont want to close your socket. Because chat app server can send you other's chatter message any time as received at server so in this case server will also push you data without your request.

Upvotes: 2

Related Questions