As far as I know I can create a server using both TCPListener and Socket, so what is the difference between the two of them?
Socket
private Socket MainSock; MainSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); MainSock.Bind(new IPEndPoint(IPAddress.Any, port)); MainSock.Listen(500); MainSock.BeginAccept(AcceptConnections, new Wrapper());
TCPListener
Int32 port = 13000; IPAddress localAddr = IPAddress.Parse("127.0.0.1"); TcpListener server = new TcpListener(localAddr, port); server.Start();
I'm really confused. The two of them listen for connections, so what is the difference between them?
Updated Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net; using System.Net.Sockets; using System.Net.Security; using System.Security.Authentication; using System.Security.Cryptography.X509Certificates; using System.IO; public class Wrapper { public byte[] buffer; public SslStream sslStream; public object connector; } public class Sock { private Dictionary<string, byte> Connections; public event Action<Wrapper> AnnounceNewConnection; public event Action<Wrapper> AnnounceDisconnection; public event Action<byte[], Wrapper> AnnounceReceive; private Socket _sock; private X509Certificate certificate = X509Certificate.CreateFromCertFile("exportedcertificate.cer"); public Sock(int port) { try { Connections = new Dictionary<string, byte>(); _sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _sock.Bind(new IPEndPoint(IPAddress.Any, port)); _sock.Listen(500); _sock.BeginAccept(AcceptConnections, new Wrapper()); } catch (Exception e) { Console.WriteLine(e); } } private void AcceptConnections(IAsyncResult result) { Wrapper wr = (Wrapper)result.AsyncState; try { wr.sslStream = new SslStream(new NetworkStream(_sock.EndAccept(result), true)); wr.sslStream.BeginAuthenticateAsServer(certificate, AcceptAuthenticate, wr); _sock.BeginAccept(AcceptConnections, new Wrapper()); } catch (Exception e) { Console.WriteLine(e); } } private void AcceptAuthenticate(IAsyncResult result) { Wrapper wr = (Wrapper)result.AsyncState; try { wr.sslStream.EndAuthenticateAsServer(result); if (wr.sslStream.IsAuthenticated == true) { AnnounceNewConnection.Invoke(wr); } } catch (Exception e) { Console.WriteLine(e); } } private void ReceiveData(IAsyncResult result) { Wrapper wr = (Wrapper)result.AsyncState; try { AnnounceReceive.Invoke(wr.buffer, wr); } catch (Exception e) { Console.WriteLine(e); AnnounceDisconnection.Invoke(wr); } } }
A TCP listener provides TCP server socket support at a specific port within the node. The socket will accept connections and receive messages from a TCP client application. The TCP client application will send messages to the TCP listener in an XML format and ASCII delimited messages.
Listener or server sockets open a port on the network and then wait for a client to connect to that port. Although other network address families and protocols exist, this example shows how to create remote service for a TCP/IP network.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With