What are the benefits of using TcpClient over a Socket directly?

前端 未结 1 1428
迷失自我
迷失自我 2021-02-05 08:07

I understand that a TcpClient is a wrapper around the socket class, and I can access the underlying socket if using the TcpClient, but what exactly does the wrapper do?

1条回答
  •  时光说笑
    2021-02-05 08:21

    what exactly does the wrapper do?

    Let me explain this with an example. You have a method in C# File.ReadAllLines. It reads all lines in the file for you. Now you can also achieve same results via FileStream class or any other class which reads file .. BUT.. wrapper i.e. File.ReadAllLines, allows you to achieve the same with less lines of code. Wrappers always increase productivity by abstracting out the low level details

    When using the TCPClient do i need to keep calling Receive() like I do with a socket or does the wrapper ensure all my data appears?

    TCPClient don't have a Receive method like Socket but the idea is same. You will have to use methods like GetStream to read the data it won't automagically appear for you

    Can I use the TcpClient on both the server and the client to wrap the socket

    Yes, you can safely use it on both client and server side

    0 讨论(0)
提交回复
热议问题