Solved

Difference between Pending, AcceptSocket , AcceptTcpClient

Posted on 2004-10-12
5
3,263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

707 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