Solved

Difference between Pending, AcceptSocket , AcceptTcpClient

Posted on 2004-10-12
5
3,081 Views
Last Modified: 2008-01-09
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
Comment
Question by:names
  • 3
  • 2
5 Comments
 
LVL 14

Expert Comment

by:AvonWyss
Comment Utility
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
 
LVL 1

Author Comment

by:names
Comment Utility
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
 
LVL 14

Expert Comment

by:AvonWyss
Comment Utility
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
 
LVL 1

Author Comment

by:names
Comment Utility
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
 
LVL 14

Accepted Solution

by:
AvonWyss earned 125 total points
Comment Utility
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now