Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TcpListener vs Socket

Tags:

c#

sockets

Hello i would like to ask what is difference between using this :

public TcpListener Listener;
public TcpClient Client;

Listener = new TcpListener(DeafultPort);
Client = default(TcpClient);
Listener.Start();

and this :

serverSocket = new Socket(AddressFamily.InterNetwork, 
                                      SocketType.Stream, 
                                      ProtocolType.Tcp);

IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 1000);

and also i would like to know if i use first option what is difference between

Listener.BeginAcceptSocket()

and

Listener.Server.BeginAccept()

and if i use second option what exactly does these two lines ?

serverSocket.Bind(ipEndPoint);
serverSocket.Listen(4);

Thanks for answers

like image 804
Vacko Avatar asked Dec 28 '13 02:12

Vacko


People also ask

What is a TcpListener?

The TcpListener class provides simple methods that listen for and accept incoming connection requests in blocking synchronous mode. You can use either a TcpClient or a Socket to connect with a TcpListener. Create a TcpListener using an IPEndPoint, a Local IP address and port number, or just a port number.

How does TcpListener work?

The TcpListener type is used to monitor a TCP port for incoming requests and then create either a Socket or a TcpClient that manages the connection to the client. The Start method enables listening, and the Stop method disables listening on the port.

Is socket using TCP?

A socket programming interface provides the routines required for interprocess communication between applications, either on the local system or spread in a distributed, TCP/IP based network environment.

What is TCP client?

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.


1 Answers

The difference between Socket and TcpListener/TcpClient is that TcpListener/TcpClient is easier to use than Socket. Socket can do everything what TcpListener/TcpClient can do. If you new to network programming, TcpListener/TcpClient is recommended. For most task, TcpClient/TcpListener perform similar in performance. Only when you have problems or not enough functionality, should you consider Sockets.

I know that my answer is not technically correct, but in this context, that would suffice.

like image 117
Syaiful Nizam Yahya Avatar answered Sep 20 '22 06:09

Syaiful Nizam Yahya