java - What is the best practice on socket programming -- do I do a close every time or leave it open? -
i haven't found clear answer on one.
i have client/server application in java 7. server , client on seperate computers. client has short (1 line of 10 characters) command issue server , server responds (120 character string). repeated every x seconds--where x rate in configuration file. short 1 second integer.max_value seconds.
every time i've created client/server application, philosophy has been create connection, business, close connection , whatever else data. seems way things should done--especially when using try resources programming.
what hiccups leaving socket connection hanging out there x seconds? best practice close down , restart or better practice socket remain connected , send command every x seconds?
i think answer depends bit on number of clients expect have.
if never have many client connections open, i'd leave connection open , call good, if latency issue - on lans, i've seen connections take several milliseconds initialize. if expect hundreds or thousands of clients connect , this, however, reconnect every time. others have said, leaving non-blocking sockets open mean have thread left running, can take several megabytes of stack space on per-thread basis. several thousand times , have big problem on machines.
another issue port space. because tcp/ip stack gives 65535 total ports doesn't mean usable - in fact, local firewalls prohibit being used, if had enough memory run thousands of simultaneous threads, run out of ports if leave lot of connections open simultaneously.
Comments
Post a Comment