# [.net] Async Sockets Questions

This topic is 4842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

##### Share on other sites
First of all, only after writing this post did I notice that you are actually using VB.NET, so there may be some code that is C# in this post.

1. Well, if you look closely at the source, you'll notice that it retrieves the socket object in the read handler with this code:
StateObject state = (StateObject) ar.AsyncState;Socket handler = state.WorkSocket;

But you are still right, this example seems to assume that you are just handling one request that you will receive on the socket. Normally, you'd definitely store your sockets somewhere (although I'd use TcpClients instead, but whatever).

2. Again, I am pretty sure they used that just as an example to also show the usage of ManualResetEvents.. Obviously, there's no need at all to do it this way, you could just as well call BeginAccept again after having received a connection.

3. I guess this may look weird, but if you look at the documentation, it's actually not so much.
Socket listener = (Socket) ar.AsyncState;

This works because AsyncState will contain whatever you pass to BeginAccept as the last parameter, so not a lot of magic involved. So basically, this is just the original socket on which you are listening for new connections. EndAccept will just give you the socket for this new connection, that's why you have two sockets in there.

In my opinion, all this isn't really that hard once you get used to it. It takes care of a lot of things that you'd have to do yourself if you want to use blocking calls in separate threads yourself. If you do decide you want to do that, just take a look at the System.Net.Sockets.Socket class, it has simple Listen, Receive, Send etc. methods that you can use for this purpose.

Hope this helps. :) Either way, 100 connections shouldn't be any problem at all.

##### Share on other sites
Thanks, for some reason I was too wrapped up in being lost to understand the parameter thing, helps a lot. I'll try it out and probably be back when I fall on my face again.

 fairly unrelated but...
Dim bytes() As Byte = New [Byte](1024) {}

why the [] around byte, and why the {} at the end, New Byte(1024) is what I would put and I'm curious if that is bad in some way.

[Edited by - Stukey on August 18, 2005 5:16:19 AM]

1. 1
Rutin
48
2. 2
3. 3
4. 4
5. 5

• 10
• 28
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633409
• Total Posts
3011718
• ### Who's Online (See full list)

There are no registered users currently online

×