Difference between TCP Listener and Socket

瘦欲@ 提交于 2019-11-30 03:18:27

They're just different classes that do the same thing, written at different levels. Under the hood the TCPListener undoubtedly calls something very like your first Socket-based code. It;s just there to hide you from some of the gory details.

TcpListener is a convenient wrapper for TCP communications. This allows you to use TcpClient for accepted connections--although you can accept sockets instead of clients to use Socket instead of TcpClient. You can do the same thing with Socket; but you have to deal with some of the TCP-specifics (like SocketType.Stream, ProtocolType.Tcp). TCP is a stream-based protocol and TcpClient recognizes that by letting you do stream communications by providing a stream with TcpClient.GetStream(). Socket is at a higher different level and needs to support many different protocols like UDP that aren't stream based.

TcpClient.GetStream returns a NetworkStream object that is suitable for SslStream; so, it should be much less work than using Socket directly. The documentation for SslStream details using TcpListener and TcpClient for SSL communications.

A TcpListener wraps a socket, and is the server-side analog to the TcpClient (which also, of course, wraps a socket).

The TcpListener is preconfigured with TCP (as opposed to the Socket, which can be used with UDP, pure IP, non-IP protocols, etc.) and gives you a TcpClient when handling a connection.

If you're not sure if you need a Socket, and are using TCP - I'd strongly suggest starting with TcpListener/Client as it's a much easier to use interface.

I'm not really answering the question, but you seem to like TcpClient better because it has GetStream() which you can use with an SslStream, but you can get a NetworkStream out of a Socket by passing the Socket as a constructor to NetworkStream

i.e. NetworkStream myStream = new NetworkStream(mySocket);

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