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
?
The TcpClient class provides simple methods for connecting, sending, and receiving stream data over a network in synchronous blocking mode. In order for TcpClient to connect and exchange data, a TcpListener or Socket created with the TCP ProtocolType must be listening for incoming connection requests.
You have to close the stream before closing the connection: tcpClient. GetStream(). Close(); tcpClient.
This code for the client will connect to that server (supposedly host.contoso.com will be the name of the server in your particular case most likely will be the IP of your computer or the IP of the computer were you are running your server on).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With