Solved

Difference between Pending, AcceptSocket , AcceptTcpClient

Posted on 2004-10-12
5
3,118 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
ID: 12287207
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
ID: 12287717
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
ID: 12288733
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
ID: 12295760
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
ID: 12295897
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# Error - Add Failed 12 78
COnsume rest client 6 36
DateTimepicker 4 33
C# parent child form 5 18
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

920 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

15 Experts available now in Live!

Get 1:1 Help Now