473,796 Members | 2,864 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

AsyncCallback exception

Hi,

I am using an AsyncCallback to receive a TcpClient on incoming
connections. The code below works fine until I start the application and
no connection is received within the time limit.
I could easily try / catch the code causing the problem but I would like
to understand why this problem is happening.
I was unable to find any field on the TcpListener that would indicate
the Listenign socket was stopped.
http://pastebin.com/882324 or

static private TcpClient _connectedClien t;

static void AcceptClient(IA syncResult ar)
{
TcpListener server = (TcpListener)ar .AsyncState;

_connectedClien t = _listenSocket.E ndAcceptTcpClie nt(ar);
}

static void Main()
{
TcpListener _listenSocket = new TcpListener(800 5);
_listenSocket.S tart();

_listenSocket.B eginAcceptTcpCl ient(new AsyncCallback(A cceptClient),
_listenSocket);

//Wait for 5 seconds for a connection
Thread.Sleep(50 00);

if( _connectedClien t == null )
{
//Failed to get a connection
//This call to stop invokes a call to AcceptClient
//This cause AcceptClient to crash on the EndAcceptTcpCli ent
_listenSocket.S top();
}
else
{
//Application begins...
}
}

*** Sent via Developersdex http://www.developersdex.com ***
Feb 16 '07 #1
2 4914
If you get SocketException it is because the socket is closed and the async
request is cancelled and it shows up as an exception at EndAcceptTcpCli ent.

"Steven Blair" <st**********@b tinternet.comha scritto nel messaggio
news:eB******** ******@TK2MSFTN GP02.phx.gbl...
Hi,

I am using an AsyncCallback to receive a TcpClient on incoming
connections. The code below works fine until I start the application and
no connection is received within the time limit.
I could easily try / catch the code causing the problem but I would like
to understand why this problem is happening.
I was unable to find any field on the TcpListener that would indicate
the Listenign socket was stopped.
http://pastebin.com/882324 or

static private TcpClient _connectedClien t;

static void AcceptClient(IA syncResult ar)
{
TcpListener server = (TcpListener)ar .AsyncState;

_connectedClien t = _listenSocket.E ndAcceptTcpClie nt(ar);
}

static void Main()
{
TcpListener _listenSocket = new TcpListener(800 5);
_listenSocket.S tart();

_listenSocket.B eginAcceptTcpCl ient(new AsyncCallback(A cceptClient),
_listenSocket);

//Wait for 5 seconds for a connection
Thread.Sleep(50 00);

if( _connectedClien t == null )
{
//Failed to get a connection
//This call to stop invokes a call to AcceptClient
//This cause AcceptClient to crash on the EndAcceptTcpCli ent
_listenSocket.S top();
}
else
{
//Application begins...
}
}

*** Sent via Developersdex http://www.developersdex.com ***

Feb 16 '07 #2
Thanks for the reply.

Yeah, thats what I thought.
Any idea how I can check to see the Socket is closed?

*** Sent via Developersdex http://www.developersdex.com ***
Feb 16 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2431
by: Craig Vermeer | last post by:
Hi All, I noticed an interesting issue the other day, and thought someone here might be able to shed some light on things. I have a situation where I'm using asynchronous delegates, and an exception is getting thrown from within the AsyncCallback on the delegate. For some reason, this causes the AsyncCallback delegate to be executed twice. Now, I understand how to stop the situation from happening. However, what I didn't know was...
1
5228
by: Jim P. | last post by:
I'm having trouble returning an object from an AsyncCallback called inside a threaded infinite loop. I'm working on a Peer2Peer app that uses an AsyncCallback to rerieve the data from the remote peer. I have no problem connecting the peers and streaming Network Streams. When the incoming data is finished recieving, I act upon it. This works great as long as all of the code is inside my form. I want to build the networking code into a...
11
3790
by: Doug Thews | last post by:
I've been working on some samples that use BeginInvoke/EndInvoke. In one example, I call BeginInvoke and pass it an AsyncCallback function pointer. I was messing around with ReaderWriterLocks and noticed that if I did this, it worked (please ignore the lack of try ... catch blocks, because I cut down the code to be more brief - I have a try...catch surround the AcquireWriterLock method): ---------- myLock.AcquireWriterLock(1000);...
4
22891
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on when to use one over another? If anyone could provide any additional info, your comments, best practices, any good articles, specific examples, etc. Thank you
1
1522
by: Lenn | last post by:
Hello, Could you provide specific examples, when one would want to use AsyncCallback as opposed to just subscribing to events or passing a delegate to a function? I realize that AsyncCallback is a delegate type, but when it's absolutely necessary to use it. Thanks
2
2130
by: Jared | last post by:
Hello all, I have a web service that I am trying to retrieve data from asynchronously. According to the MSDN documentation (ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/vbcon/html/vbtskCallingWebServiceAsynchronously.htm) I should be able to call a webservice asynchronously using a delegate function. The callback seems to work, but, when I assign the value to an existing datagrid the program hangs. I don't quite understand, what do I...
1
6682
by: Alper AKCAYOZ | last post by:
Hello, I have developped asynchronous socket communication with blocking Socket commands (accept, connect, send, receive) by using threads on Windows .NET Forms. It is working properly. Now I want to code the similar program with Asynchronous Socket commands of .NET using Managed C++ on Windows .NET Forms. My problem is with delegates. I have to use "static" methods as parameter in delegate constructor like below:...
0
1209
by: gnani | last post by:
I have a Button Created in .NET Windows Form. In a AsyncCallBack Procedure, I need to change the enabled property of the button to false. I tried using Reflection.PropertyInfo. The Code goes like this Dim pi As System.Reflection.PropertyInfo = btnLogin.GetType.GetProperty("Enabled") pi.SetValue(btnLogin, False, Nothing)
3
1860
by: Artie | last post by:
Hi, We have an app which uses an AsyncCallback method to handle the return from a COM call. In this callback, if we try to do anything with a WebBrowser control, we get the following exception: "InvalidCastException was unhandled Specified cast is not valid"
0
9527
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
10453
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10172
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
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
9050
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
5441
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
4115
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
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
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.