Neil C. Obremski
Neil C. Obremski

Reputation: 20244

Is it possible to convert between Socket and TcpClient objects?

Here's another C#/.NET question based merely on curiousity more than an immediate need ...

If you had a Socket instance and you wanted to wrap it in the higher-level TcpClient class, is that possible and how would you do it?

Conversely if you have an instance of TcpClient, is it possible to get the underlying Socket?

Upvotes: 13

Views: 12479

Answers (2)

Jorge Ferreira
Jorge Ferreira

Reputation: 97839

If you had a Socket instance and you wanted to wrap it in the higher-level TcpClient class, is that possible and how would you do it?

Socket socket = ...;
TcpClient client = new TcpClient();
client.Client = socket;

Conversely if you have an instance of TcpClient, is it possible to get the underlying Socket?

Get the underlying Socket using TcpClient.Client property.

Upvotes: 31

Jobi Joy
Jobi Joy

Reputation: 50028

From TcpClient to Socket is very easy. tcpClientInstance.Client is the underlying Socket instance.

Upvotes: 3

Related Questions