Need help in efficient Socket program RRS feed

  • Question

  • Hi All,
     I want to develop a TCP based client/server application. Where A is the server and B,C can be the client. The objective is A can request for data from either B or C. B can request data from A but not from C. C can request data from A but not from B.

     My question is if A want some data from B, should it connect, send the command get the response and close the connection immediately or should it keep the connection open so that in future it can send the command/response using the same socket insted of creating new.

    If it keeps the connection open there may be N number of clients which can connect to A and there will be lot of open connections is it efficient?

    Thanks in advance


    • Edited by Jeevanand Tuesday, October 7, 2008 9:15 AM
    • Moved by Peter RitchieModerator Tuesday, October 7, 2008 6:22 PM Networking in .NET question (Moved from Visual C# Language to .NET Framework Networking and Communication)
    Tuesday, October 7, 2008 9:09 AM

All replies

  • It would generally be better to keep a TCP connection open if you are going to use it again.

    It takes relatively large amount of time to open a connection (a couple of round-trip times), which will have most effect if you're just going to send a small amount of information (DNS which sends small amounts of information infrequently uses UDP for these reasons).  Or if you're sending lots of information, TCP is careful at startup to measure how capacity the network has so it doesn't overload it, so high throughtput is only received after some amount of transfers, so its good to keep a connection open for that reason.

    However as you note having lots of connections open is bad.  So perhaps keep a cache and close connections when you get too many open (choosing which to close by LRU or just a random one -- LRU can be bad in some cases).

    http://www.alanjmcf.me.uk/ Please follow-up in the newsgroup. If I help, mark the question answered
    Tuesday, October 7, 2008 6:37 PM
  • I would suggest you to close the connections because if you have efficient iocp server than you don't care for round trips and time they take, you will process connections on completion.

    Overhead on tcp stack and hardware is almost not noticable.
    For an example of IOCP server look here http://code.msdn.microsoft.com/IOCPServer
    Wednesday, October 8, 2008 5:12 PM