PDA

Click to See Complete Forum and Search --> : C# TCP Client polling for listening server


Andy_P
Nov 23rd, 2008, 05:43 AM
I have recently dipped my toe into the TCP water. :sick:

My .NET app creates a TCPClient that sends data to a server that is running on another app.

Of course, first I have to establish a connection to this server before I can send anything. The other app may or may not be running, so any connection attempts may fail.

I have a timer running that calls .Connect() on my client and catches the SocketException if it cannot. (Error code 10061, WSAECONNREFUSED.) At this point, I allow the timer to keep running, and poll the connection again next time.

Is there a better way to do this, rather than continually polling and catching exceptions? Seems a bit inefficient to me.

Any advice very much appreciated.