473,804 Members | 3,018 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.BeginAcc ept(). 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.BeginRec eive() 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 2429
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.BeginAcc ept(). 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.BeginRec eive() 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
6500
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. socket server?
1
313
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 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...
0
2096
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 all on a local dev box. I am able to successfully run most of the SampleApp features (Register,Feedback,Error report, version check) The only method I can not execute is the BuyNow example. I have tried leveraging the Sharewarestarterkit.com web service to rule out locality, to no avail.
8
11022
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 is going swimmingly except when I try to close the socket during a read and I get the following error: <error_msg> An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in system.dll Additional information: The I/O operation has been aborted because of
4
3561
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 problems this will cause with the heap. I am looking at a few options to get around this. 1) Have a BufferPool class that hands out ArraySegment<byteportions of a larger array (large enough that it would be in the LOH). If all of the array is used create another big segment. 2) Create a...
4
2144
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 server is starting to listen for incomming data. (BeginRecieve). In the receive callback I always call BeginRecieve again to keep listen for more data. The server is continualy listening, and from time to time sending (but if it is sending more data when there is already a listening socket, I don't...
4
4581
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 async. I could use the async sockets methods (beginsend, beginrecv...) or I could start a new thread for each client and run the comunication using the sync mehtods (send,recv). What is the best aproach? This is a performance critical application.
11
8623
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 completely async, supporting timeouts and some scheduling mechanisms. I use a non-blocking approach for this, using the call to 'poll' to support the async mechanism - rather than the 'begin' and 'end' functions. I already found that connecting doesn't set the "isconnected" variable correctly...
1
20649
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 error on server side(client side code is same, but I don't see the error): "System.IO.IOException: Unable to read data from the transport connection:A blocking operation was interrupted by a call to WSACancelBlockingCall"
0
9711
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9591
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10087
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9166
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.