Friday, August 1, 2014

Debugging a socket

I am quickly getting a taste of the following quote:

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." --Brian Kernighan
In other words, if you barely understood the code you were writing when you wrote it, you are going to have an extremely hard time trying to figure out issues with that code further down the road.  The iServer notification program I rewrote to alert users of readings of temperature and  humidity that were outside of excepted ranges is a good example.  My supervisor had me rewrite the program, taking out the threading that was used because he was concerned the threading might be causing some of the problems.  I did that and it mostly works.  (I still have an issue with socket connections timing out.)  I did, however, quickly realize why Gary used threads.  Connecting to a socket is very time intensive and it locks the program while it sits there waiting for the socket response.  Each iServer socket connection takes 10-15 seconds and with 32 iServers that equates to about 6-8 minutes for the whole process.  Gary's other program which monitors the iServers can get the readings for all the iServers in about 10-15 seconds. In other words, Gary is able to use threads to query all the iServers at the same time and keep his program from becoming unresponsive.  My program works, but I was concerned that it becoming unresponsive while it was waiting for all the socket connections would eventually cause problems.  So I found an example on the Microsoft developer network for using an Asynchronous Client Socket. It looked to be straight forward, but when I tried to implement the code, I got no benefits.  My code was taking just as long and was still locking up the application.  Well..., in order to make asynchronous calls to the socket, the example is using threading.  Obviously, I didn't understand enough about threading and where I needed to use it in my programming.  I believe the issue was that I was using the threads in my server class, when I needed to be using the threads outside the server class in the main class.  The thread needs to allow each server to try and make a connection with the socket and get the needed readings. While one server is waiting for a response, the thread needs to sleep and give a chance for the next server to make a connection.  That can't be done inside the server class. It has to be done from the outside.  Once again grasshopper, you still have much to learn!

No comments:

Post a Comment