472,353 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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
1 2281
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Trevor | last post by:
Does the async. sockets implementation in .NET use IO Completion Ports (IOCP) internally or would I have to use P/Invoke to use IOCP in a C# async....
1
by: Ben | last post by:
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...
0
by: whizpop | last post by:
Hi, First of all, thanks for a great starter kit, now If I could just get it to work (fully). I am trying to compile and run the solution/services...
8
by: Dinsdale | last post by:
I am trying to write a Tcp "Server" that opens a class that wraps a tcp socket when a new connection is made (Listener.AcceptSocket()). Everything...
4
by: Greg Young | last post by:
Ok so I think everyone can agree that creating buffers on the fly in an async socket server is bad ... there is alot of literature available on the...
4
by: nyhetsgrupper | last post by:
Hi, I've written a async server app. This app start by connecting to a client and then send some data (BeginSend). When the data is sent, the...
4
by: nyhetsgrupper | last post by:
I'm writing a server application connection to multiple clients using sockets. I want one socket for each client, and the comunication needs to be...
11
by: atlaste | last post by:
Hi, In an attempt to create a full-blown webcrawler I've found myself writing a wrapper around the Socket class in an attempt to make it...
1
by: Ryan Liu | last post by:
Hi, I have a 100 clients/ one server application, use ugly one thread pre client approach. And both side user sync I/O. I frequently see the...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.