Is it possible to convert between Socket and TcpClient objects?

╄→гoц情女王★ 提交于 2019-12-10 02:09:55

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/203147/is-it-possible-to-convert-between-socket-and-tcpclient-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!