Solved

Difference between Pending, AcceptSocket , AcceptTcpClient

Posted on 2004-10-12
5
3,227 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
"Emulate" TAB key when press Enter Key 3 92
C# Windows Form Navigation - Total Beginner 9 92
Need help with a query 14 53
SQL State HYT00. Timeout expired proplem 8 42
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

734 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