Sockets Question

G

Guest

Hello All,

My question is about the proper way to setup a listening socket. I have the
following code that works fine:
IPAddress localAddr = Dns.GetHostEntry(strHostName).AddressList[0];

Int32 port = 25001;
TcpListener tcpServerListener = new TcpListener(port);
tcpServerListener.Start();
Socket serverSocket = tcpServerListener.AcceptSocket();

However, I've seen other code out there that looks like this:
this.listenerSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
this.listenerSocket.Bind( new IPEndPoint(this.serverIP , this.serverPort));
this.listenerSocket.Listen(200);

Is it necessary to specify the AddressFamily and SocketType or is my way ok?

Thanks,
John
 
D

Dave Sexton

Hi John,

TcpListener wraps a listening, streaming Tcp Socket and it aggregates some of
the more common operations into single method calls, so it's much easier to
code against.

The code you have supplied that uses TcpListener does something different than
the code you have supplied that uses Socket directly:
Socket serverSocket = tcpServerListener.AcceptSocket();

This line blocks until it can acquire the first client connection, returned as
a different Socket than the one TcpListener uses internally

And the Socket code has something different as well:
this.listenerSocket.Bind( new IPEndPoint(this.serverIP , this.serverPort));

Binds the Socket to a specific IP as well as a specific port


Check out the TcpListener.AcceptTcpClient() method. Instead of AcceptSocket()
returning a Socket, it returns TcpClient, which wraps the client Socket
connection making it easier to work with.

--
Dave Sexton

John F said:
Hello All,

My question is about the proper way to setup a listening socket. I have the
following code that works fine:
IPAddress localAddr = Dns.GetHostEntry(strHostName).AddressList[0];

Int32 port = 25001;
TcpListener tcpServerListener = new TcpListener(port);
tcpServerListener.Start();
Socket serverSocket = tcpServerListener.AcceptSocket();

However, I've seen other code out there that looks like this:
this.listenerSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
this.listenerSocket.Bind( new IPEndPoint(this.serverIP ,
this.serverPort));
this.listenerSocket.Listen(200);

Is it necessary to specify the AddressFamily and SocketType or is my way ok?

Thanks,
John
 
G

Guest

Thanks Dave. I will check out the AcceptTcpClient method.
--
John F


Dave Sexton said:
Hi John,

TcpListener wraps a listening, streaming Tcp Socket and it aggregates some of
the more common operations into single method calls, so it's much easier to
code against.

The code you have supplied that uses TcpListener does something different than
the code you have supplied that uses Socket directly:
Socket serverSocket = tcpServerListener.AcceptSocket();

This line blocks until it can acquire the first client connection, returned as
a different Socket than the one TcpListener uses internally

And the Socket code has something different as well:
this.listenerSocket.Bind( new IPEndPoint(this.serverIP , this.serverPort));

Binds the Socket to a specific IP as well as a specific port


Check out the TcpListener.AcceptTcpClient() method. Instead of AcceptSocket()
returning a Socket, it returns TcpClient, which wraps the client Socket
connection making it easier to work with.

--
Dave Sexton

John F said:
Hello All,

My question is about the proper way to setup a listening socket. I have the
following code that works fine:
IPAddress localAddr = Dns.GetHostEntry(strHostName).AddressList[0];

Int32 port = 25001;
TcpListener tcpServerListener = new TcpListener(port);
tcpServerListener.Start();
Socket serverSocket = tcpServerListener.AcceptSocket();

However, I've seen other code out there that looks like this:
this.listenerSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
this.listenerSocket.Bind( new IPEndPoint(this.serverIP ,
this.serverPort));
this.listenerSocket.Listen(200);

Is it necessary to specify the AddressFamily and SocketType or is my way ok?

Thanks,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top