Friday, June 18, 2010

TCPClient.Connected property

I was writing TCP server application which is written in Microsoft Visual C# 2008. I need to accept TCPClient which sent connection request to my server. One of the features that I need to implement is to keep track of the client status whether it was connected to my server or it had already been disconnected from my server.

I found the TCPClient.Connected property in .NET and so I used it to keep track of the client status. But it was totally unreliable. I still could receive TCPClient.Connected property as true even if the client had already been shutdown. I scratched my head to find out what was the cause of the problem. As usual, I searched Internet to find out if other people had encountered same problem before.

To my surprise, TCPClient.Connected property was not actually reflecting the current status of the client. It would only show the status that last time when the server was trying to communicate to client. So it can consider unreliable.

I hate to do it but I need to implement additional logic to keep track of my client. I need to keep record of the time when the client was trying to connected to my server and compare those value with timeout.

Raising Event from DLL

I am currently developing a new software for a new machine. I need to interface my .NET program to COM DLL to control the hardware. I was writing one DLL for common methods which I can reuse in future applications. I was also writing Windows Application for the methods specific to that machine.

The COM DLL that I am calling from my DLL is written by "ADLink" technology which interfaced to Motion Control module and I/O modules. I was trying to drive servo motors from that DLL. I could successfully drive the servo but it took 10 seconds for my Windows application to receive motion complete event from Servo. I was trying to troubleshoot servo and motion control card but I have no clue what was actually happening.

Finally, I found the problem when I carefully log every line of code in my program. It took 10 seconds to receive the event from DLL to my Windows Application. I tried to move the event raising to Windows application and the problem was solved.