Determine if TcpClient is still connected to socket server

  • Thread starter Thread starter D. Wichert
  • Start date Start date
D

D. Wichert

Hi folks,

I use a TcpClient object to connect to a local (asynchron) socket server.
Once the connection is established, I ask every second if there is a new
entry in the socket queue.
If the connection was established successfully at the beginning and ends for
some reason after a certain time, I have to reconnect to the socket server.

So, what I need therefore is a TcpClient attribute such as "Connected" that
returns true if connection is still available, otherwise false.
I read some examples on the internet with TcpClients where an attribute
"Connected" is used, but it does not belong to the TcpClient Class (maybee
it has been removed in the last .NET framework).

There is also an attribute "Active" that only tells you if the connection
has been established at the beginning, but does not track the connection
state.

How can I solve this problem?

Thanks for any help!!

Best regards,
David
 
To do this you should need to create a derivative of the TcpClient
class and expose the Client.Connected property. (The Client (socket)
is a public property in the .NET Framework 2.0 so this is only required
for 1.1 and bellow.)



public class MyTcpClient : TcpClient {

public MyTcpClient(Socket Client):base() {
this.Client=Client;

}

public Socket GetClient
{
get
{
return this.Client;
}
}
public bool Connected{
get {
return this.Client.Connected;
}
}

}
}



--Michael Davidov
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top