467,077 Members | 1,026 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,077 developers. It's quick & easy.

Async Sockets

Ben
I've written a fair amount of sockets code using the Winsock2 API, but I am having some trouble converting to the .Net Sockets API, specifically asynchronous sockets.

What I have is a form that is both a client and a server. When the form starts I create a listening socket and call Socket.BeginAccept(). When a client connects my accept function is called, and it is on a separate thread. After I accept the client connection I connect to another server (no async there).

Now both sockets call Socket.BeginReceive() and wait for data. When data from one arrives, I log it, then pass it to the other. This seems to work well, however things break down when the client disconnects.

First when a client disconnects I have no way to know about it (in winsock I could get the FD_CLOSE event). So currently I can do nothing in my form, so I added a "Reset" button which seems to cause real unpleasantness.

After a client has disconnected and I click "Reset" in the form, how do I stop all the async receives?

Currently I am calling EndReceive with the IAsyncResult I got from the BeginReceive. But when a new client attempts to connect I get a "Fatal Execution Engine Exception" from code that should be fine. Plus I seem to accumulate more threads than I can explain.

Can anyone recommend a good C# async server example? Or MSDN article?

Thanks!
Ben
Jul 21 '05 #1
  • viewed: 2094
Share:
1 Reply
www.vbip.com
Ben wrote:
I've written a fair amount of sockets code using the Winsock2 API, but I am having some trouble converting to the .Net Sockets API, specifically asynchronous sockets.

What I have is a form that is both a client and a server. When the form starts I create a listening socket and call Socket.BeginAccept(). When a client connects my accept function is called, and it is on a separate thread. After I accept the client connection I connect to another server (no async there).

Now both sockets call Socket.BeginReceive() and wait for data. When data from one arrives, I log it, then pass it to the other. This seems to work well, however things break down when the client disconnects.

First when a client disconnects I have no way to know about it (in winsock I could get the FD_CLOSE event). So currently I can do nothing in my form, so I added a "Reset" button which seems to cause real unpleasantness.

After a client has disconnected and I click "Reset" in the form, how do I stop all the async receives?

Currently I am calling EndReceive with the IAsyncResult I got from the BeginReceive. But when a new client attempts to connect I get a "Fatal Execution Engine Exception" from code that should be fine. Plus I seem to accumulate more threads than I can explain.

Can anyone recommend a good C# async server example? Or MSDN article?

Thanks!
Ben


Jul 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Trevor | last post: by
1 post views Thread by Ben | last post: by
reply views Thread by whizpop@gmail.com | last post: by
8 posts views Thread by Dinsdale | last post: by
4 posts views Thread by Greg Young | last post: by
4 posts views Thread by nyhetsgrupper@gmail.com | last post: by
4 posts views Thread by nyhetsgrupper@gmail.com | last post: by
11 posts views Thread by atlaste | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.