Q: I am lost, should i use TCPClient or Socket?

  • Thread starter Thread starter Martin Arvidsson
  • Start date Start date
M

Martin Arvidsson

Hi!

I am just beginning to learn about the TcpListener and TcpClient and Socket.

I have figured out the TcpListener, it is used for booth in and outgoing
requests, right?

But when to use the TcpClient and when to use the Socket?

Regards
Martin
 
Martin,

I would always use TcpClient/TcpListener. You really don't have a
reason to not use TcpClient, since it exposes the underlying Socket through
the Client property. So if you need to change something on the Socket that
TcpClient is using, you can do so directly.

Hope this helps.
 
Hi Martin,
I am just beginning to learn about the TcpListener and TcpClient and Socket.

I have figured out the TcpListener, it is used for booth in and outgoing requests, right?

TcpListener is used to establish incoming connections. From a TcpListener you can accept either a Socket or TcpClient (your choice)
that you can use for two-way communication.
But when to use the TcpClient and when to use the Socket?

You can use either a TcpClient or Socket on a client machine to connect to a host that is using a TcpListener. On the client, you
create the TcpClient or Socket. On the server, the TcpListener creates one for each incoming connection. Just accept the
implementation that you want to use.

As Nicholas said, use should probably use a TcpClient, on both the client and server. There really is no reason not to since it
exposes the Socket anyway and it wraps some common implementation details too.

HTH
 
Back
Top