Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3608
  • Last Modified:

Difference between Pending, AcceptSocket , AcceptTcpClient

Hi,

I'm new in the area of networking in C# and I need your help

Concerning TcpListener, I've read the following in  http://www.microsoft.com/mspress/books/sampchap/6510.asp

"There are two ways to detect a request. One way is to use the Pending method to detect incoming connection
requests. You can also use the AcceptSocket or AcceptTcpClient method to block until a connection request arrives"

When to use each of these ways (1-Pending, 2-AcceptSocket , 3-AcceptTcpClient) ?

i.e. in a chat application what is the best way to use?
0
names
Asked:
names
  • 3
  • 2
1 Solution
 
AvonWyssCommented:
Pending is just a flag which is set to true if there are connections waiting. It does not accept the connection itself, for that you need one of the Accept* methods. But the Accept* methods will block if there is no connection waiting, therefore, if you do poll using a timer or a loop and only do an Accept* after checking for Pending, your code will not block.

AcceptSocket will return a Socket, which you can then use however it suits you. AcceptTcpClient will accept the socket and create a TcpClient with this socket. Therefore, this method is a shortcut if you want to use the TcpClient class anyways. If not, use the Socket.

For a Chat application, I'd use neither of those, but rather directly use a Socket and call the Listen and use the async methods to receive new connections (BeginAccept and EndAccept), because theser are the most performant methods available. And because I'm a big fan of async programming... ;-)
0
 
namesAuthor Commented:
Thanks a lot for these info but let me ask for more,

When it is more suitable to return Socket and when to return TcpClient ? in addition, I think all the applications will be more efficient if it uses the async methods, so, when AcceptSocket  or AcceptTcpClient will be more efficient??
0
 
AvonWyssCommented:
The TcpListener and TcpClient classes are easier to use, they are trade simplicity for performance (not a big hit, but still some). So, if one wants to do high-performance programming, he will not use any of those classes. But for low-performance apps, or for proof of concept applications, they are just fine.

However, since I have been programming native sockets long before .NET was even in the works, I do have gook background knowledge and therefore I find it even easier to use the normal Socket classes, since I'm just used to it. And as a plus, I can get better performance.
0
 
namesAuthor Commented:
Do you have reference(s) illustrating more the difference for me? cause I would like to build a client server application like chat application but
1- The client sends to the server just requests for something (not big data)
2- The server sends huge data to the clients every 2 seconds

So I'm searching for the best way to do that, for example if I use TcpListener.AcceptSocket or TcpListener.AcceptTcpClient or whatever else, I would like to know EXACTLY why I chosen that way
0
 
AvonWyssCommented:
Well, have a look at the help files. If you look at the Socket class, you'll see that is is quite rudimentary. But it allows you to do mostly anything possible on Windows sockets. On the other hand, the TcpListener and TCPClient are nice wrappers, and easy to use.

For instance, when you work with TCPClient, you need one thread for each concurrent connection. Imagine a server with 1000 simultaneous connections; the thread overhead will kill the machine's performance. On the other hand, when using Sockets and async methods, there will be some 20 threads from the threadpool which handle all the requests, thus generating way less overhead.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now