I need to have periodically communication with plc ( on every 1 sec ), I send message and I receive message. I use Socket class for this communication. Do I need to every 1 sec to open connection ( socket=new Socket(ipaddress, port) ), send messagethen socket.close() and so on , or to hold socket opet all time ?
I need to have periodically communication with plc ( on every 1 sec ),
Share
I’ll assume you’re talking about TCP sockets here…
Apart from the obvious inefficiencies involved in setting up a TCP connection every second you’re also likely to end up accumulating sockets in
TIME_WAIT(hopefully on your client).I’ve written about
TIME_WAITand the problems it causes with regards to server scalability and stability here on my blog: http://www.serverframework.com/asynchronousevents/2011/01/time-wait-and-its-design-implications-for-protocols-and-scalable-servers.htmlGiven the rate that you are opening and closing sockets (once a second would result in 240 (60*4) sockets sitting in
TIME_WAITin the normal (4 minute) 2MSLTIME_WAITperiod) this shouldn’t prove too much of a problem assuming theTIME_WAITsockets ARE ending up on the client and not on the server and assuming you’re not connecting to lots of servers every second, but… If you have many clients connecting to your server every second and you are not making sure that your server doesn’t accumulate sockets inTIME_WAITstate then you may limit your server’s scalability.The alternative is to hold the socket connection open and only reopen it if and when it gets disrupted. This may prove slightly more complex for you to initially program but pooling the connection in this way is likely to be considerably more efficient (when you DO need to send data you just send the data and don’t need to go through the TCP handshake to set the connection up) and much more resource efficient on the client; you’re not perpetually holding 240 sockets in
TIME_WAIT…